Reading from file

Discussion in 'C Programming' started by Scott Dabot, Oct 28, 2004.

  1. Scott Dabot

    Scott Dabot Guest

    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.
     
    Scott Dabot, Oct 28, 2004
    #1
    1. Advertising

  2. Scott Dabot

    Nick Austin Guest

    On Thu, 28 Oct 2004 14:37:18 -0500, Scott Dabot <>
    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.
     
    Nick Austin, Oct 29, 2004
    #2
    1. Advertising

  3. On Thu, 28 Oct 2004 14:37:18 -0500, Scott Dabot <>
    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>>
     
    Barry Schwarz, Nov 1, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    689
    Kevin Spencer
    Nov 11, 2004
  2. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,421
    Scott Allen
    Jun 4, 2005
  3. Replies:
    0
    Views:
    799
  4. Karim Ali

    Reading a file and resuming reading.

    Karim Ali, May 25, 2007, in forum: Python
    Replies:
    2
    Views:
    383
    Hrvoje Niksic
    May 25, 2007
  5. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    810
    Alf P. Steinbach
    Jul 10, 2007
Loading...

Share This Page