
December 2nd, 2012, 12:01 PM
|
 |
Contributed User
|
|
|
|
Step 1 is make sure you post code within [code][/code] tags, so that people can stomach reading the code.
Step 2 is check for errors. Don't assume that the file was opened successfully.
Code:
file_in=fopen("test.txt", "r");
if ( file_in ) {
// do stuff
fclose( file_in );
} else {
perror("Can't open file");
}
Step 3 - what is your input data format. If it's characters devoid of any white space at all, then %s will just overflow your buffer if you let it.
It's safer to use fgets, like
Code:
file_in=fopen("test.txt", "r");
if ( file_in ) {
if ( fgets( message, sizeof(message), file_in ) != NULL ) {
// do something with it
}
fclose( file_in );
}
Or more usually,
Code:
while ( fgets( message, sizeof(message), file_in ) != NULL ) {
// do something with each line
}
Step 4, this will read from the beginning of the file each time. Do you perhaps want to read from where you got to last time?
Step 5, use sleep() to allow more time for data to accumulate in the file.
|