简体   繁体   English

fgets() 问题和外部文件最后一行的分段错误

[英]Having an issues with fgets() and segmentation faults on last line of external file

Here is a snippet of code from my program that is causing an segmentation error on the last line.这是我程序中的一段代码,它导致最后一行出现分段错误。 My program is supposed to take input from a file (if that is specified by the user) or from manual input from the console.我的程序应该从文件(如果用户指定)或从控制台的手动输入中获取输入。 When the user inputs the sentences by the console, it works.当用户通过控制台输入句子时,它就起作用了。 However, when I take the sentences from an external file, I get a segmentation fault on the last line (the line where when it is completed EOF will be reached).但是,当我从外部文件中取出句子时,我在最后一行(将到达 EOF 完成的行)出现分段错误。 Assume the file is closed and the memory freed outside of this snippet .假设文件已关闭,并且在此代码段之外释放了内存

Here is the snippet:这是片段:

if(inputExists == 1) {
        char *input = malloc(256);
        ip = fopen(inFile, "r");
        if(ip) {
            while(fgets(input, 256, ip) != NULL) {
                printf("%s", input);
            }
        }
    }

Here is what is on the external file:这是外部文件中的内容:

bob is working.
david is a new hire.
alice is bob's boss.
charles doesn't like bob.

This is the output that I get when the full program (where the option for the user taking input from an external file is selected).这是我在完整程序时得到的输出(选择了用户从外部文件获取输入的选项)。

bob is working.
david is a new hire.
alice is bob's boss.
Segmentation fault

If you think you need more code to find the problem let me know and I will add the full program (though to be honest it is very ugly, and messy).如果您认为需要更多代码来查找问题,请告诉我,我将添加完整程序(尽管说实话,它非常丑陋且混乱)。

There are multiple problems.有多个问题。 To start with, free allocated memory.首先, free分配的内存。

free(input);

Also, file needs to be closed.此外,文件需要关闭。

fclose(ip)

Ok guys I found out what the problem was.好的,我发现了问题所在。 I had my pointer malloc'd outside of the loop where it receives the character sequences from the input file.我的指针 malloc 在循环之外,它从输入文件接收字符序列。 Because of this it was referencing the same location over, and over again....apparently this was causing issues when I tried to reference more than one line of code, as the *input variable was only pointing to the last line of the code.因此,它一遍又一遍地引用相同的位置......显然,当我尝试引用多行代码时,这会导致问题,因为 *input 变量仅指向代码的最后一行. When I changed it to this:当我把它改成这样:

else if(inputExists == 1) {
        //First open the file
        inputFile = fopen(inFile, "r");
        //If said file exists
        if(inputFile) {
            while(!feof(inputFile)) {
            char *temp2 = malloc(500);
            fgets(temp2, 500, inputFile);
            if((strlen(temp2)>0) && (temp2[strlen (temp2) - 1] == '\n')) {
                temp2[strlen (temp2) - 1] = '\0';
            }
            root = insert(root, temp2);
            }
        }else {
            printf("The file/directory you specified does not exist or won't open.\n");
        }
        fclose(inputFile);
    }

the code worked.代码有效。 Thanks for the help though guys/gals, it was greatly appreciated and I learned alot more about pointers and fgets in general感谢伙计们/女孩们的帮助,非常感谢,我学到了更多关于指针和fgets的知识

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM