Results 1 to 7 of 7

Thread: Read one line at a time using fgets in C

  1. #1
    Join Date
    Nov 2011
    Posts
    27

    Read one line at a time using fgets in C

    I'm trying to make me a little game with SDL in C with different levels. I put each level line (ASCII) to a file that I named abc.lvl. My worry is that I do not know how to read the second line of the file abc.lvl to begin at 2 and not level 1.


    Code:
    int chargerNiveau (int level [] [NB_BLOC_HAUTEUR]) 
    { 
    FILE * file = NULL; 
    ligneFichier char [NB_BLOC_LARGEUR NB_BLOC_HAUTEUR * + 1] = {0}; 
    int i = 0, j = 0; 
    file = fopen ("/ media / Mac OS / prog / c / Marion Sokoban / abc.lvl", "r"); 
    if (file == NULL) 
    return 0; 
    / / We read the line niveau.lvl 
    fgets (ligneFichier, NB_BLOC_LARGEUR NB_BLOC_HAUTEUR * + 1, file); 
    for (i = 0; i <NB_BLOC_LARGEUR; i + +) 
    { 
    for (j = 0, j <NB_BLOC_HAUTEUR j + +) 
    { 
    switch (ligneFichier [(i * NB_BLOC_LARGEUR) + j]) 
    { 
    case '0 ': 
    level [j] [i] = 0; 
    break; 
    case '1 ': 
    level [j] [i] = 1; 
    break; 
    case '2 ': 
    level [j] [i] = 2; 
    break; 
    case '3 ': 
    level [j] [i] = 3; 
    break; 
    case '4 ': 
    level [j] [i] = 4; 
    break; 
    } 
    } 
    } 
    fclose (file); 
    return 1; 
    }
    I hope to make myself understood.

  2. #2
    Join Date
    Nov 2010
    Posts
    76

    Re: Read one line at a time using fgets in C

    You can use fseek to move the file.

    Code:
    int fseek (FILE * stream, long offset, int whence);

  3. #3
    Join Date
    Nov 2010
    Posts
    89

    Re: Read one line at a time using fgets in C

    fseek does not allow you to move about in line, but offset only. If the row size is fixed, however, you can calculate the corresponding offset. However, in your example, you could instead load all the levels at startup (in an array of structures, for example) and then, nothing prevents you from start to the desired level, indicating the index of the corresponding table ( which is actually the level).

  4. #4
    Join Date
    Mar 2011
    Posts
    166

    Re: Read one line at a time using fgets in C

    fseek () is mostly used when opening the file in binary mode. Now here it is opened in text mode, since it is opened with "r" (by the way it would be clearer to write "rt"). To read the second line, the easiest way is to read the first and not to do so, then read the second line. It is not uncommon to repeat several times the same thing, not only because the machines are so speed, it has not always need to optimize treatment, but also because there are "caches" which in reality, there are already some optimizations.

  5. #5
    Join Date
    May 2011
    Posts
    111

    Re: Read one line at a time using fgets in C

    The fgets function reads a line of data from the file pointed to by the file handle of the file pointer. The filehandle must previously fopen have been created (see Example 2). The readout of the data ends at the line break (= a line) or the number of parameters in the "maximum length" specified character, whichever occurs first. Then the file pointer at the end of the read data is set so that the next call to fgets to read the following line.

  6. #6
    Join Date
    May 2011
    Posts
    99

    Re: Read one line at a time using fgets in C

    A line break at the end of the line is returned. If an error occurs (eg reaches end of file), it is false (or 0) is returned.

  7. #7
    Join Date
    May 2011
    Posts
    111

    Re: Read one line at a time using fgets in C

    here i have some example for this, so just check this:
    Example 1

    Code:
    <Php 
    $ Line = fgets ($ filehandle, 4096); 
    ?>
    Result:

    The variable "$ line" contains a line from the file on the file handle "$ file handle" shows.



    Example 2

    Code:
    / / The file "sample.txt" contains the following: 
    PHP is more popular, 
    not only the function fgets. 
    <Php 
    $ Filehandle = fopen ("sample.txt", "r"); 
    $ Line = fgets ($ filehandle, 4096); 
    echo $ line; 
    ?>
    Result:

    PHP is more popular

Similar Threads

  1. Replies: 4
    Last Post: 31-12-2010, 03:37 PM
  2. How to let Java read text file line by line.
    By Visala28 in forum Software Development
    Replies: 3
    Last Post: 04-08-2009, 11:30 PM
  3. Problem with fgets function
    By killerboy in forum Software Development
    Replies: 3
    Last Post: 01-05-2009, 11:20 PM
  4. Bash Script, read without new line
    By Kieran in forum Software Development
    Replies: 2
    Last Post: 06-02-2009, 08:59 PM
  5. Replies: 1
    Last Post: 31-12-2008, 12:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •