C programming File I/O

C programming File I/O
0

#1

Hello guys. I know FCC does not really deal in questions like this but i really need your help. I have this assignment in school and i need to use a language i am not familiar with at all. C
I need to read in input from a file with this format:
Petroleum Engineering 94600 175500
Actuarial Mathematics 56400 131700
Actuarial Science 61200 130800
Nuclear Engineering 69000 127500
The file is tab delimited so each component( the string and the two int’s are separated by tabs but in the string its separated by white spaces). The file consists of majors in college and their predicted early career and mid career pay
So this is the code i have so far but it is not printing anything at all

FILE *in;

in = fopen("Salaries.txt", "r");
int early, mid;
char course[50];
if(in == NULL ){
	printf("ERROR! opening file");
	exit(1);
}
while(fscanf(in, "%s %d %e", course, early, mid) ==3){
	printf("%s %d %e\n", course, early, mid);
}

fclose(in);
return 0;	

Thank you for your help


#2

You say this is an assignment in school and you are supposed to use C which is a language you are not familiar with at all. Why would you have an assignment in a class where you must use C, but you have never been taught C in the class?

We are here to help campers with their challenges and projects. It sounds like you might want to ask the teacher to teach you some C if they expect you to use it for the class. That is unless you were supposed to learn C in your own time and then complete this assignment with what you have studied on your own.


#3

yeah, i was annoyed. we were never taught C so we have no choice but to teach ourselves if we dont know it


#4

Are you getting any error messages? Is this the entire code?


#5

yes. thats the thing. No error messages. It just does not print anything


#6

What were you trying to accomplish with the above line?


#7

i used a resource i saw online that said instead of using EOF, i should use ==3 so while we have 3 things on the line continue to read the file


#8

The file is tab delimited so each component( the string and the two int’s are separated by tabs but in the string its separated by white spaces).

I’m not sure what you meant by the last part of that.

Having said that, your fscanf format string has no references to tabs in at all! Were you thinking of something like:
fscanf(in, "%s\t%d\t%e", course, &early, &mid)?

mid should be declared as a double rather than an int.

If you’re wondering why it’s currently not printing anything, I suspect it’s reading the whole line into course without finding early or mid, resulting in an output of 1 rather than 3, thus never hitting the printf.

I haven’t tested any of this, that’s just my thoughts