未分配值fscanf csv

我试图制作一个文件来读取包含CSV的文本文件。 但是由于某些原因,一些Fscanfs并没有返回一个值,但有些值得。

FILE* reader;
char OptionOne[30]="";
char OptionTwo[30]="";
char OptionThree[30]="";
char OptionFour[30]="";
char answer[1]="";
char CorrectAnswer[1];
char Question[51];

reader = fopen("C:UsersFinleyDesktopProgrammingQuizMachineQuizMachinefscanf_questions.txt", "r");

if (reader == NULL)
{
    printf("Unable to Open File: %sn", FileName);
}

fscanf(reader, "%[^,],", Question);
fscanf(reader, "%[^,],", OptionOne);
fscanf(reader, "%[^,],", OptionTwo);
fscanf(reader, "%[^,],", OptionThree);
fscanf(reader, "%[^,],", OptionFour);
fscanf(reader, "%[^,],", answer);

为什么第一个fscanf会返回一个值,但其他所有其他人都不会返回一个值。 我看不到任何东西,它们看起来都与相同的格式相同文件格式是这样的:

你用什么函数打开一个文件?,fscanf,fclose,fopen,main,3

下列哪一项不是变量类型?,int,float,char,string,4


  • 需要使用足够大小的缓冲区。 答案,作为一个字符串需要至少2个char 。@丹尼尔

  • fscanf()应限制输入以防止缓冲区损坏。 宽度说明符通常比缓冲区的大小小1。

    char Question[51];   
    fscanf(reader, "%50[^,],", Question);
    
  • 应该检查fscanf()的结果。

    if (fscanf(reader, "%50[^,],%29[^,],%29[^,],%29[^,],%29[^,],%1s", 
        Question, OptionOne, OptionTwo, OptionThree, OptionFour, answer) != 6) {
      Handle_Bad_input();
    }
    
  • 如果输入没有后面的',' ,则不要使用"%[^,]"作为最后的值,否则fscanf()将读取下一行。

  • 建议使用空格来引导每个"[^,]"以跳过前导空格。

    if (fscanf(reader, " %50[^,], %29[^,], %29[^,], %29[^,], %29[^,],%1s", 
    
  • 更好:用fgets()读取行,然后扫描缓冲区。

    char buf[200];
    if (fgets(buf, sizeof buf, reader) == NULL) Handle_IOErrorOrEOF();
    if (sscanf(buffer, "%50[^,],%29[^,],%29[^,],%29[^,],%29[^,],%1s", 
        Question, OptionOne, OptionTwo, OptionThree, OptionFour, answer) != 6) {
      Handle_Bad_input();
    }
    

  • 你的缓冲区的answer太小,所以fscanf写入太多导致腐败。

    你给它的大小只有一个字符,但fscanf会写入两个字符:你想要的字符3和空字符

    将缓冲区大小增加到2可解决问题。


    this line: fscanf(reader, "%[^,],", Question); (and those like it) 
    have a couple of problems. 
    1) should always check the returned value from a input I/O statement
       to assure the conversion was successful 
    2) the format string is missing the type conversion, in this case 's'
    this line: fscanf(reader, "%[^,],", answer); will fail if the line does not end in a ','
    
    
    "Why does the first fscanf return a value but all the others not return a value." 
    ans: 1) because the ' ' after the comma is not consumed
         2) use a leading ' ' in the format strings to consume white space
            this will also consume an trailing 'n' on each line
         3) a type specifier is NOT optional in a format string, use 's'
         3a) I would have read in the 'answer' as a char using '%c' for a format conversion
             and only created a char, not a char array.
         4) to avoid buffer overruns, 
            include the 'size-1' optional part of each format  conversion,
            just in case some string length is = or greater than the 
            receiving buffer
    
    "I cannot see anything and they all seem the same with the same formating
    File formatting is like this:"
    ans: where is the file formatting info?
    
    this line: printf("Unable to Open File: %sn", FileName); 
    the variable FileName is not set. 
    suggest setting char * FileName = C:UsersFinleyDesktopProgrammingQuizMachineQuizMachinefscanf_questions.txt‌​" 
    and using 'FileName' in the fopen() statement. 
    
    this line: printf("Unable to Open File: %sn", FileName); 
    after this line, should insert 'exit(EXIT_FAILURE);
    rather than continue in the program, trying to use that NULL file descriptor pointer 
    
    this line: char answer[1]=""; 
    will be a problem as the input is being read in as strings, 
    so even for a single char string, there still needs to be
    room for the trailing ''
    
    this line: fscanf(reader, "%[^,],", answer);
    1) fails if each line of the input file, including the last line,
       does not end in ','
    
    链接地址: http://www.djcxy.com/p/54567.html

    上一篇: fscanf csv in C. Value not assigned

    下一篇: Fastest way to check if a file exist using standard C++/C++11/C?