| | |
Reading a File Line By Line
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
While not terribly difficult, first timers may not know the functions to look up and use. Here is some basic code for reading a file one line at a time. The contents of the file are printed to the stdout.
#include <stdio.h> int main ( void ) { static const char filename[] = "file.txt"; FILE *file = fopen ( filename, "r" ); if ( file != NULL ) { char line [ 128 ]; /* or other suitable maximum line size */ while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */ { fputs ( line, stdout ); /* write the line */ } fclose ( file ); } else { perror ( filename ); /* why didn't the file open? */ } return 0; } /* file.txt This text is the contents of the file named, "file.txt". It is being used with some example code to demonstrate reading a file line by line. More interesting things could be done with the output, but I'm trying to keep this example very simple. */ /* my output This text is the contents of the file named, "file.txt". It is being used with some example code to demonstrate reading a file line by line. More interesting things could be done with the output, but I'm trying to keep this example very simple. */
0
•
•
•
•
I thinks instead of depending on the length of the line (which can be variable depeding on the type of editor and its settings).I think it'll be better to look for '\n' which is line separator.
0
•
•
•
•
•
•
•
•
I thinks instead of depending on the length of the line (which can be variable depeding on the type of editor and its settings).I think it'll be better to look for '\n' which is line separator.
0
•
•
•
•
hi guys
i just modified the one i got here: this will read line by line from a text file then stored every line to an array;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
int main ( void )
{
static const char filename[] = "data.txt";
FILE *file = fopen ( filename, "r" );
int i, j;
char arra[128][128];
char line[128]; /* or other suitable maximum line size */
for(i=0; i<128; i++)
for(j=0; j<128; j++)
arra[i][j] = '\0';
for(i=0; i<128; i++)
line[i] = '\0';
if ( file != NULL )
{
i=0;
while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{
strcpy(arra[i], line);
printf("array ----> %s ", &arra[i]);
i++;
}
fclose ( file );
}
else
{
perror ( filename ); /* why didn't the file open? */
}
return 0;
}
i just modified the one i got here: this will read line by line from a text file then stored every line to an array;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
int main ( void )
{
static const char filename[] = "data.txt";
FILE *file = fopen ( filename, "r" );
int i, j;
char arra[128][128];
char line[128]; /* or other suitable maximum line size */
for(i=0; i<128; i++)
for(j=0; j<128; j++)
arra[i][j] = '\0';
for(i=0; i<128; i++)
line[i] = '\0';
if ( file != NULL )
{
i=0;
while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{
strcpy(arra[i], line);
printf("array ----> %s ", &arra[i]);
i++;
}
fclose ( file );
}
else
{
perror ( filename ); /* why didn't the file open? */
}
return 0;
}
0
•
•
•
•
i want to Read a line from file then compare it to a sentense(GPS Sentence) and if its match(valid) then display this line else display Error
plz advice
Regards
plz advice
Regards
Similar Threads
- reading from a text file: not line by line but values seperated by a space (C++)
- fgets for reading files line by line (C++)
- Reading line from a file and then replacing it (Assembly)
- Reading a text file character by character and line by line (C++)
- Reading a file line by line problem (C)
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h




