I started programing in language C, and I 开发者_如何学Pythonhave some problems with reading text files. Let me explain.
I have one file text which is organized like this :
Tony
12.23
John
09.45
Tayris
03.99
I would like to retrieve all notes less than ten and display them, but I can't...
Does anybody could help me?
Thanks a lot.
C provides four functions that can be used to read files from disk:
fscanf()
field oriented function.
fgets()
line oriented function.
fgetc()
character oriented function
fread()
block oriented function.
See this article for more information.
Check out the fgets
function. It will return until (and including) the end of string character (you can strip it from the destination string if you want).
http://people.cs.uchicago.edu/~iancooke/osstuff/ccc.html offers an example:
Here's a more complicated example. Readline() uses fgets() to read up to MAX_LINE - 1 characters into the buffer 'in'. It strips preceding whitespace and returns a pointer to the first non-whitespace character.
char *Readline(char *in) {
char *cptr;
if (cptr = fgets(in, MAX_LINE, stdin)) {
/* kill preceding whitespace but leave \n
so we're guaranteed to have something*/
while(*cptr == ' ' || *cptr == '\t') {
cptr++;
}
return cptr;
} else {
return 0;
}
}
That should be enough I think.
精彩评论