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!

Recommended Answers

All 2 Replies

try while(fscanf(In,"%s",&TTT))
but i'm not sure that will work

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.