【问题标题】:c program terminates while reading from file [duplicate]c程序在从文件读取时终止[重复]
【发布时间】:2016-09-09 14:09:47
【问题描述】:

我正在尝试使用 fgetc()fgets()fscanf() 等不同的函数从文本文件中读取数据。执行期间程序在读取fgetc() 后终止。

#include <stdio.h>

void writeFile(FILE *, char *);
void readFile(FILE *,char *);

void main(void){
    FILE *file;
    char *path="temp/test.txt";
    printf("%s\n",path);
    writeFile(file,path);
    readFile(file,path);
    return;
}

void readFile(FILE *file, char *path){
    file = fopen(path , "r");
    if(file)
        printf("\n file opened");
    char *buff;

    char getc  = fgetc(file);
    printf("\n 1 char :: %c ",getc);

    getc  = fgetc(file);
    printf("\n 2 char :: %c ",getc);
    fgetc(file); 

    fgets(buff,25,file);
    printf("\n 3 gets :: %s ",buff);

    fgets(buff,255,file);
    printf("\n 4 gets :: %s ",buff);

    int fscan = fscanf(file,"%s", buff);
    printf("\n 5 fscan :: %s ",buff);

    int eof= fclose(file);
}

void writeFile(FILE *file, char *path){
    file = fopen(path , "w+");
    if(file)
        printf("\n file opened");
    char *fileStr= "this is not working";
    int putc = fputc('@',file);
    fputc('!',file);
    int puts = fputs("\nThis is test file.",file);
    int putf1 = fprintf(file, "\n Kinldy help to solve this");
    int putf2 = fprintf(file, "\n%s", fileStr);
    int eof= fclose(file);
}

注意:如果我在程序中注释writeFile(file,path); 行,它会正常执行。

【问题讨论】:

  • char *buff; --> char buff[255];
  • 如果你用255 字符声明buff,你还应该设置fscanf(file,"%254s", buff); 以确保你不会溢出(如果你碰巧有一个没有空格的完整字符序列会超过buff 的大小 - 不太可能,但为了防止未定义的行为,您应该)

标签: c fgets terminate file-read fgetc


【解决方案1】:

我对您的程序进行了一些小的更改,以便它读取文件并且不会收到警告。请尝试它是否适合您。它现在不会终止,希望这会对您有所帮助。

#include <stdio.h>

void writeFile(FILE *, char *);

void readFile(FILE *, char *);

void main(void) {
    FILE *file = NULL;
    char *path = "temp/test.txt";
    printf("%s\n", path);
    writeFile(file, path);
    readFile(file, path);
    return;
}

void readFile(FILE *file, char *path) {
    file = fopen(path, "r");
    if (file)
        printf("\n file opened");
    char buff[255];

    int getc = fgetc(file);
    printf("\n 1 char :: %c ", getc);

    getc = fgetc(file);
    printf("\n 2 char :: %c ", getc);
    fgetc(file);

    fgets(buff, 25, file);
    printf("\n 3 gets :: %s ", buff);

    fgets(buff, 255, file);
    printf("\n 4 gets :: %s ", buff);

    int fscan = fscanf(file,"%254s", buff);
    printf("\n 5 fscan :: %s ", buff);

    int eof = fclose(file);
}

void writeFile(FILE *file, char *path) {
    file = fopen(path, "w+");
    if (file)
        printf("\n file opened");
    char *fileStr = "this is not working";
    int putc = fputc('@', file);
    fputc('!', file);
    int puts = fputs("\nThis is test file.", file);
    int putf1 = fprintf(file, "\n Kinldy help to solve this");
    int putf2 = fprintf(file, "\n%s", fileStr);
    int eof = fclose(file);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-07
    • 1970-01-01
    相关资源
    最近更新 更多