开发者

How can I retrieve rows in a text file

开发者 https://www.devze.com 2023-02-01 00:41 出处:网络
I started programing in language C, and I 开发者_如何学Pythonhave some problems with reading text files. Let me explain.

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:

  1. fscanf()

    field oriented function.

  2. fgets()

    line oriented function.

  3. fgetc()

    character oriented function

  4. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消