C Programming Files help *will give medal and fan*
I am trying to get the read file to display stuff to the screen, but instead, it only displays the first 3 printf statements within the file. I have attached the "studentdata.txt", which includes the data from the write file. Can you tell me how do I get to display the information in the txt file to the screen using the read function?
I have this so far: void class_rep() { FILE *studentdataPtr; studentdataPtr = fopen("studentdata.txt", "r"); if (studentdataPtr == NULL) { printf("There has been a file error."); exit(0); } else { printf("\n\n------------------------------------------------------------------------------------------------"); printf("\n\nROLL #\tName\t\t\tM\tG\tC\tG\tS\tBS\tAvg"); for (student_counter=1; student_counter <= tot_students; student_counter++) { printf("\n\n------------------------------------------------------------------------------------------------"); fscanf(studentdataPtr, "\n%2d\t%s\t\t\t%4.1f\t%4.1f\t%4.1f\t%4.1f\t%4.1f\t%4.1f\t%4.1f", &student_counter, s[student_counter].name, &s[student_counter].maths, &s[student_counter].grammar, &s[student_counter].composition, &s[student_counter].gen_studies, &s[student_counter].science, &s[student_counter].basic_span, &s[student_counter].student_average); printf("\n%2d\t%s\t\t\t%4.1f\t%4.1f\t%4.1f\t%4.1f\t%4.1f\t%4.1f\t%4.1f", student_counter, s[student_counter].name, s[student_counter].maths, s[student_counter].grammar, s[student_counter].composition, s[student_counter].gen_studies, s[student_counter].science, s[student_counter].basic_span, s[student_counter].student_average); } } fclose(studentdataPtr); }
Have you tried simplifying to just try and read and count? This would see if there was trouble reading in the information in.
@e.mccormick I don't think I fully get what you mean
Well, right now you have a problem in either getting data in or out. Sometimes it can be good to isolate the input and the output to see where the problem is.
Alrighty..I will try that..thank you!
But I think that's what I did in my code? o.o
When I put it outside of the for loop..everything shows up as 0.0 with the roll no as 1..which is totally different from what
I have in the txt file
Right now you have formatted input and formatted output. If you use something more basic for the output, like cout, you can see if you are getting things in properly and able to do unformatted output. However, if cout also fails to output more, then it is likely something is tripping up the input.
But cout is C++ and I ought to do the assignment in C
Thought that was in c too. Part of iostream.h.
I just tried cout with #include<iostream.h> and it said no such file directory :/
Hmmm... Well, I am thinking the issue is somewhere on the input. studentdata.txt is what you are taking in. You seem to be treating it as if it is the formatted string all the time. However, the first few lines are not formatted that way. That should cause it to assign nothing and move on. But what if it is not properly assigning anything? You could try assigning the output of fscanf to a variable. See, fscanf returns how many things it successfully matched and assigned. If it always says 0, then it is either not matching or not assigning.
Is &s[student_counter] some global it has access to? If not, how is that being included in the scope of this function?
That iacts as the roll number of the student
Sorry, I misinterpreted the question..it acts as a struct that holds the specific record for the student
Yes, but how is it declared for this function? Is it a global?
yes, i think
This is my entire code..just to clear up any misunderstandings
OK. Then yah, I would use that fact about fscanf returning an int to see how many things it is assigning and just print that out. That way you know if the fscanf is actually taking things in or not. It may not like something about the formatting of the input.
How do I set up an fscanf statement that returns an int?
It always does. You are just not capturing it. ``` void class_rep() { FILE *studentdataPtr; int testFscanf = 0; ``` and then later: ``` testFscanf = fscanf(studentdataPtr, "\n%2d\t%s\t\t\t%4.1f\t%4.1f... bla bla etc. printf("\nScanned in: %i", testFscanf); ``` or something like that.
Should %i be %d instead since it is an int?
I think both do ints.
My output was Scanned in: 0
http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html#printf d, i Type signed int. o Type unsigned int printed in octal. u Type unsigned int printed in decimal.
So it is not getting things in. Something is wrong in the `fscanf(studentdataPtr, "\n%2d\t%s\t\t\t%4.1f\t%4.1f\t%4.1f\t%4.1f\t%4.1f\t%4.1f\t%4.1f", &student_counter, s[student_counter].name, &s[student_counter].maths, &s[student_counter].grammar, &s[student_counter].composition, &s[student_counter].gen_studies, &s[student_counter].science, &s[student_counter].basic_span, &s[student_counter].student_average);` line if it never takes in data and assigns it. I don't know if it is an assignment or a read problem. Hmmm. You could do test data types nd see if it assigns to those. If so, then it is is failing on assignment to the structure. If not, then it is failing to read from the file.
Just copy the ``` char name[25]; float maths, grammar, composition, science, gen_studies, basic_span, student_average; ``` into the top of the void class_rep() function to make them local. Then use those directly in the fscanf and printf to see if it works. Like I said, if that also fails, then it is not reading things from the file properly and that is where you need to focus. On the other hand, if that does work, then it does not like that structure assignment for some reason.
Well, that didn't work
Then it really soinds like it is not geting the data from the file properly.
Apparently. Thank you!
Join our real-time social learning platform and learn together with your friends!