C File Input

EvanB

Senior member
Nov 3, 2001
268
0
0
OK, I am creating a program that will load a text file of CDs into a prgram and add them to a structure. The text file will look like this:
(Each block of three is 1 CD)

Rating1
Title1
Artist1

Rating2
Title2
Artist2
...

Is there some way I can use fscanf to find the number of CDs, because right now it loads my CDs, and all the other garbage data around them. If I could find the number of Cds, I could put that in variable VAR in my program and all would be fine and dandy. Any ideas?



Thanks in advance,
Evan
 

PrincessGuard

Golden Member
Feb 5, 2001
1,435
0
0
fscanf() returns EOF when it reaches the end of the file. So just keep looping until you reach that condition.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
What's "VAR"? What is its purpose? That has to be the most awful name for a variable ever. :p

There's too many variables at play to give you "the" answer. Do you need to parse the stuff at all? If you're reading the file at all, why would you want to get the number of albums and *not* their contents? How tolerant should this be of malformed data? And aren't scanf functions unsafe?
 

EvanB

Senior member
Nov 3, 2001
268
0
0
while ((foo = fscanf()) != EOF)
foo?
What do you mean by that? oh, just some random variable?
 

EvanB

Senior member
Nov 3, 2001
268
0
0
Lol, i just put VAR there because I wasnt sure what to use because I dont know the number of CDs in the file. What I am doing is finding the number of cds so that I can use the attached getLine function to get load the contents of the file into my structure. I dont need to be tolerant of errors, the file will not have any problems. I just need to tell my program how many lines to go down so it doesnt input any garbage data.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Just make sure that there is no garbage data after the last data record in the file.

By doing the test on EOF, the read will terminate when the end of file is detected.
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
I believe C also has a feof() function in the stream class which returns true on end of file, false otherwise. Or is this C++?
 

Firus

Senior member
Nov 16, 2001
525
0
0
You could also go:
while(c != '\377') as well, \377 is the eof character (i think)...
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Why use an obscure escape sequence when the standard library has (I believe multiple) ways to do it which are very readable and obvious?
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
You could also go:
while(c != '\377') as well, \377 is the eof character (i think)...

Even if it is on your system it may not be true on another and there's no reason to poke and hope here since there's already eof tests in the standard libraries.

A while back, I saw Larry Wall give a short talk about the current design of Perl 6. At some point he put up a list of all the operators - well over a hundred of them!

Did you see the periodic table of the operators?
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Originally posted by: Nothinman
A while back, I saw Larry Wall give a short talk about the current design of Perl 6. At some point he put up a list of all the operators - well over a hundred of them!

Did you see the periodic table of the operators?

Yeah, that's where I got the quote from. :)