C-为什么我的文件I/O程序会出现这个segfault



我正在制作一个练习程序,在学习C时对Makefile中的一个变量进行简单的修改。每当我运行这个程序时,我都会得到一个segfault,但我不知道为什么。我怀疑这与"r+"fopen模式或我使用fseek()有关。这是代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void rewind(FILE *f)
{   
    long start = 0;
    fseek(f, start, SEEK_SET);
}

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        printf("arguments too many or too few. use: setfile <filename> (minus .c extension)n");
        exit(1);
    }
    FILE *mfile = fopen("Makefile", "r+"); // note to self: r+ is for a file that already exists
    FILE *old_mfile = fopen("OLD.Makefile", "r+");  // w+ erases the file and starts in read-write mode with a fresh one
    char line[200];
    char *fn_ptr;
    char *name = argv[1];
    while(fgets(line, 199, mfile)) // first create the backup
    {   
        fputs(line , old_mfile); // before changing the line, write it to the backup
    }
    rewind(mfile); // reset the files to position 0
    rewind(old_mfile);
    puts("Makefile backed-up as 'OLD.Makefile'");
    while(fgets(line, 199, old_mfile)) // now lets loop again and rewrite with the new FNAME
    {
        if((fn_ptr = (strstr(line, "FNAME= "))))
        {
            fn_ptr += strlen("FNAME= ");
            int i;
            for(i = 0; i < strlen(name); i++)
            {
                *(fn_ptr+i) = *(name+i);
            }
            *(fn_ptr+i) = '';
        }
        // printf("%s", line); // for debugging
        fputs(line , mfile);
    }
    printf("FNAME is now: '%s'n", argv[1]);
    fclose(mfile);
    fclose(old_mfile);
    return 0;
}

再次检查此行:

FILE *old_mfile = fopen("OLD.Makefile", "r+");  // w+ erases the file and starts in read-write mode with a fresh one

您在注释中有正确的模式,但在fopen调用中没有。

除了改变模式之外,如何避免出现分割错误?始终检查返回值!如果fopen失败,它将返回NULL

这是一个工作版本。这里有几个要点需要注意,所以我将让您通过切换输入和输出来逐一检查它们。如果仔细阅读,被调用函数的手册页可能就足够了。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void rewind(FILE *f)
{
    long start = 0;
    fseek(f, start, SEEK_SET);
}

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        printf("arguments too many or too few. use: setfile <filename> (minus .c extension)n");
        exit(1);
    }
    FILE *mfile = fopen("Makefile", "r+"); // note to self: r+ is for a file that already exists
    FILE *old_mfile = fopen("OLD.Makefile", "w+");  // w+ erases the file and starts in read-write mode with a fresh one
    char line[200];
    char *fn_ptr;
    char *name = argv[1];
    while(fgets(line, 199, mfile)) // first create the backup
    {
        fputs(line , old_mfile); // before changing the line, write it to the backup
        memset(line,0x00,200);
    }
    rewind(mfile); // reset the files to position 0
    rewind(old_mfile);
    memset(line,0x00,200);
    puts("Makefile backed-up as 'OLD.Makefile'");
    fclose(mfile);
    mfile = fopen("Makefile", "w");
    while(fgets(line, 199, old_mfile)) // now lets loop again and rewrite with the new FNAME
    {
        if((fn_ptr = strstr(line, "FNAME=")) != NULL)
        {
            fn_ptr += strlen("FNAME=");
            int i;
            for(i = 0; i < strlen(name); i++)
            {
                *(fn_ptr+i) = *(name+i);
            }
            *(fn_ptr+i) = '';
        }
        // printf("%s", line); // for debugging
        fputs(line , mfile);
        fputs("n" , mfile);
        memset(line,0x00,200);
    }
    printf("FNAME is now: '%s'n", argv[1]);
    fclose(mfile);
    fclose(old_mfile);
    return 0;
}

相关内容

  • 没有找到相关文章

最新更新