Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Read one line at a time using fgets in C

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 30-12-2011
Member
 
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.

Reply With Quote
  #2  
Old 30-12-2011
Member
 
Join Date: Nov 2010
Posts: 72
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);
Reply With Quote
  #3  
Old 30-12-2011
Member
 
Join Date: Nov 2010
Posts: 83
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).
Reply With Quote
  #4  
Old 30-12-2011
Member
 
Join Date: Mar 2011
Posts: 155
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.
Reply With Quote
  #5  
Old 30-12-2011
Member
 
Join Date: May 2011
Posts: 103
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.
Reply With Quote
  #6  
Old 30-12-2011
Member
 
Join Date: May 2011
Posts: 95
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.
Reply With Quote
  #7  
Old 30-12-2011
Member
 
Join Date: May 2011
Posts: 103
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Read one line at a time using fgets in C"
Thread Thread Starter Forum Replies Last Post
When playing video with media player on windows 7 I see read line running vertically in the middle of screen Achilleas Windows Software 4 31-12-2010 03:37 PM
How to let Java read text file line by line. Visala28 Software Development 3 04-08-2009 11:30 PM
Problem with fgets function killerboy Software Development 3 01-05-2009 11:20 PM
Bash Script, read without new line Kieran Software Development 2 06-02-2009 08:59 PM
IDrive Time-Line Restore, Sort of an Online 'Time Machine' for the Windows Platform ThoMas321 Windows Software 1 31-12-2008 12:08 PM


All times are GMT +5.5. The time now is 11:42 PM.