0

I use the following code to read data from a txt file:

File2Open = "data.dat";


if((In=fopen(File2Open,"rt")) == NULL)
{
Application->MessageBox("Open data file failed.", NULL, MB_OK);
return;
}


handle = open(File2Open, O_RDONLY);


do
{
Readin1;


Readin2;
}while(!eof(handle));

Readin1 and Readin2 are two subroutines and consist of many lines of fscanf(In,"%s",&TTT) to read data from the data file. However, when EOF is reached, the iteration is still going back to call Readin1 and Readin2, which causes mismatching type problem because no more data left in the data file for reading. I also used while(fscanf(In,"%s",&TTT) != EOF), the result is not correct. Can any body tell me how to stop reading when the end of the file is met? Thank you!

Edited by happygeek: fixed formatting

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Salem
0

Why are you using open() and fopen() ?

> while(fscanf(In,"%s",&TTT) != EOF)
Post an attempt which uses this construct, because that would be my initial attempt. while ( fgets( buff, sizeof buff, In ) != NULL ) would be a better approach, as it protects against buffer overrun.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.