我的终端在运行C程序后冻结

如何解决我的终端在运行C程序后冻结

我要做什么?

我正在尝试解决CS50中的recover问题,就像这样...

为您提供了一个损坏的文件,怎么办?好吧,该文件应该是多个JPG文件,而不是一个大文件。您的任务是将所述文件分成多个JPG,为此,您必须遍历原始文件中的字节并找到此0xff 0xd8 0xff和一个介于0xe00xef之间的数字...这些数字是什么意思?好了,它们指示了JPG的开始,因此您必须根据该信息来创建和关闭文件...

问题很长,因此请按this了解更多信息。

我做了什么?

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <cs50.h>

//create new data type BYTE
typedef uint8_t BYTE;

int main(int argc,char *argv[])
{
    //variables
    int counter = 0;
    char name[8];
    //create new array of BYTES,it is 512 because as you can see later I am reading the input file in 512 byte chunks
    BYTE buffer[512];
    int bytes = 512;
    //output image
    FILE* img;
    //input image
    FILE* f;

    //checks if the arguments passed are wrong
    if (argc != 2)
    {
        printf("Usage: ./recover <file>");
        return 1;
    }
    
    //opens the file passed in the arguments 
    f = fopen(argv[1],"r");
    
    //checks if file is null
    if (f == NULL)
    {
        printf("bruh");
        return 1;
    }
    
    //loop till you reach zero
    //also read in 512 bytes chunks from the input file and store to buffer
    while (fread(buffer,bytes,1,f))
    {
        //checks if this is a jpg,and if it is checks if it is the first one
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0 && counter == 0)
        {
            //assigns new value to name
            sprintf(name,"%03i.jpg",counter);
            //creates new file with the name we've just created
            img = fopen(name,"w");
            //write to that file
            fwrite(buffer,img);
            //increment number of files by 1
            counter++;
        }
        //checks if it encoutered another jpg,if it did it will close the last created file and create a new one
        else if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            //closes last file
            fclose(img);
            //assigns new value to name depending on how many images were printed
            sprintf(name,"w");
            //writes to that file
            fwrite(buffer,img);
            //increment number of files by one
            counter++;
        }

        //if no jpgs are encountered,continue to write to the last file
        else
        {
            fwrite(buffer,img);
        }
    }
    //close opened files
    fclose(f);
    fclose(img);
    
    return 0;
}

如果您认为我在不告诉您我做了什么的情况下向您转储了一段代码,那么我已注释掉每一位。

出了什么问题?

根据帖子的标题,当我运行程序并传递一些参数时...终端只是冻结,什么也没有出现...从来没有错误。

如果您想复制此问题:

  • 运行wget https://cdn.cs50.net/2019/fall/psets/4/recover/recover.zip 在您的终端中并解压缩文件...

  • 然后键入make recover进行编译(如果不起作用,请使用
    通常会发出叮当声)

  • 最后键入./recover card.raw来运行程序...应该生成大约50个文件,但同样不会

解决方法

即使没有合适的标题,您的最后一个else也可能发生 之前发现在这种情况下,您的img文件可能不是 在蜜蜂使用之前打开。

首先必须正确初始化

FILE* img=NULL; // you forgot this initialisation

然后,在最后一个else中,您可以测试文件是否真正打开。

if(img!=NULL) // you forgot this test
{
  fwrite(buffer,bytes,1,img);
}

在我的计算机上,您的初始程序仅在分段时停止 因为偶然,即使没有明确初始化, img恰好是NULL

我猜想如果fwrite()使用未初始化的img指向 到任意但可访问的内存,它可以偶然解释 此未定义/未初始化的FILE与标准相关 输入/输出;然后在标准I / O上发送任意字节会冻结 终端...
但是没有什么是不确定的...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-