Monday 20 May 2019
string - fscanf csv in C. Value not assigned
Answer
Answer
I have attempted to make a file to read a text file that contains CSVs. For some reason however some of the Fscanfs do not return a value but some do.
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:\\Users\\Finley\\Desktop\\Programming\\QuizMachine\\QuizMachine\\fscanf_questions.txt", "r");
if (reader == NULL)
{
printf("Unable to Open File: %s\n", FileName);
}
fscanf(reader, "%[^,],", Question);
fscanf(reader, "%[^,],", OptionOne);
fscanf(reader, "%[^,],", OptionTwo);
fscanf(reader, "%[^,],", OptionThree);
fscanf(reader, "%[^,],", OptionFour);
fscanf(reader, "%[^,],", answer);
Why does the first fscanf return a value but all the others not return a value. I cannot see anything and they all seem the same with the same formating
File formatting is like this:
What function do you use to open a file?,fscanf,fclose,fopen,main,3
Which of the following is not a variable type?,int,float,char,string,4
Answer
Need to use buffers of adequate size. The answer, as a string needs at least 2
char
.@Danielfscanf()
should limit input to prevent buffer corruption. The width specifier is typically 1 less than the size of the buffer.char Question[51];
fscanf(reader, "%50[^,],", Question);The result of
fscanf()
should be checked.if (fscanf(reader, "%50[^,],%29[^,],%29[^,],%29[^,],%29[^,],%1s",
Question, OptionOne, OptionTwo, OptionThree, OptionFour, answer) != 6) {
Handle_Bad_input();
}Do not use
"%[^,]"
for the last value if input does not have a following','
, elsefscanf()
will read the next line.Suggest leading each
"[^,]"
with a space to skip leading white-space.if (fscanf(reader, " %50[^,], %29[^,], %29[^,], %29[^,], %29[^,],%1s",
Even better: Read the line with
fgets()
and then scan the buffer.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();
}
php - file_get_contents shows unexpected output while reading a file
I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...
-
I have an app which needs a login and a registration with SQLite. I have the database and a user can login and register. But i would like th...
-
I would like to use enhanced REP MOVSB (ERMSB) to get a high bandwidth for a custom memcpy . ERMSB was introduced with the Ivy Bridge micro...
-
I am new to dask and I found so nice to have a module that makes it easy to get parallelization. I am working on a project where I was able ...
No comments:
Post a Comment