|
| ||||||||||
| Tags: ascii, c language, code, command, fgets, sdl |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Read one line at a time using fgets in C
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;
} |
|
#2
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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); ?> 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;
?> PHP is more popular |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |