Reading from file

S

Scott Dabot

Im trying to read from a file but I want to take out the break
statements and not have an infinite loop that only stops on breaks.

for(;;)
{
testvariable=fscanf(infile, "%d\n", &totallibrarys);
if(testvariable==EOF)
break;

if (fgets(listoflibs[a], 50, infile)==0)
break;

n=strlen(listoflibs[a])-1;
listoflibs[a][n]='\0';



testvariable=fscanf(infile, "%d\n",&numOfBooks);
if (testvariable==EOF)
break;
for (j=0; j<numOfBooks; j++)
{

testvariable=fscanf(infile, "%s%d %d\n", &book,&numAvail, &numIn);

if (testvariable==EOF)
break;

T[a]=Insert(numAvail, numIn, book, T[a]);


}
a++;
}
 
N

Nick Austin

Im trying to read from a file but I want to take out the break
statements and not have an infinite loop that only stops on breaks.
Why?

how could i rewrite this so that i don't use EOF b/c when i did a while
loop for while(EOF) it wouldn't get all the information in the file.

It's already very close to the way I would have written it. There
are some minor portability problems e.g. some implementations may
at times set n to -1.

Nick.
 
B

Barry Schwarz

Im trying to read from a file but I want to take out the break
statements and not have an infinite loop that only stops on breaks.
Add
int done = 0;

Replace with
for (; done==0; )
or
while (!done)
{
testvariable=fscanf(infile, "%d\n", &totallibrarys);
if(testvariable==EOF)
break;

Replace with
done = 1;
else
{
if (fgets(listoflibs[a], 50, infile)==0)
break;

Replace with
done = 1;
else
{
n=strlen(listoflibs[a])-1;
listoflibs[a][n]='\0';



testvariable=fscanf(infile, "%d\n",&numOfBooks);
if (testvariable==EOF)
break;

Replace with
done = 1;
else
{
for (j=0; j<numOfBooks; j++)
{

testvariable=fscanf(infile, "%s%d %d\n", &book,&numAvail, &numIn);

if (testvariable==EOF)
break;

Replace with
j = INT_MAX-1;
else
T[a]=Insert(numAvail, numIn, book, T[a]);


}
a++;

Add
}
}
}

The problem with this approach is that for anything other than the
simplest test, your code ends up indented so far to the right that it
is almost unreadable.

Since EOF is a non-zero constant, your while statement is a never
ending loop. Where did you use it? In place of the for statement?
If so, the two appear to have the same effect.

Show the code.


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top