Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Reading from file

Reply
Thread Tools

Reading from file

 
 
Scott Dabot
Guest
Posts: n/a
 
      10-28-2004
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++;
}
-----------
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.
 
Reply With Quote
 
 
 
 
Nick Austin
Guest
Posts: n/a
 
      10-29-2004
On Thu, 28 Oct 2004 14:37:18 -0500, Scott Dabot <(E-Mail Removed)>
wrote:

>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.
 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      11-01-2004
On Thu, 28 Oct 2004 14:37:18 -0500, Scott Dabot <(E-Mail Removed)>
wrote:

>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;

>for(;


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.

> }
>-----------
>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.


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>>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading of file by next of map file and by next of file descriptor. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 1 07-10-2007 02:46 AM
Reading a file and resuming reading. Karim Ali Python 2 05-25-2007 02:04 PM
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muņoz ASP .Net 3 06-04-2005 10:56 AM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM



Advertisments