Thread: help fing the end of file

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    9

    help fing the end of file

    I get a continuous loop running after the info in the file has finished. The loop is filled with funny y characters.

    Code:
    void open_batch()
    {
       int eof = FALSE;
       FILE *fp;
       char f[30];
       char c;
       int a = 0, b = 0;
       int d, e, g = 0, h = 0, i = 0, j = 0, k = 0, l = 0;
       printf("Please enter the file you wish to run: ");
       scanf("%s", &f);
       l = getchar();
       fp = fopen(f,"rt");
       if(fp == NULL)
       {
          printf("No such file\n");
          menu();
       }
    
       while(eof == FALSE)
       {
          c = fgetc(fp);
          while(c == 32)
          {
             c = fgetc(fp);
          }
          if((c == 'F') || (c == 'U') || (c == 'f') || (c == 'u'))
          {
             for(d = 0; d <= 3; d++)
             {
                u_type[d] = c;
                c = fgetc(fp);
                printf("%c", u_type[d]);
             }
             printf(" ");
             while(c == 32)
             {
                c = fgetc(fp);
             }
             for(e = 0; e <= 2; e++)
             {
                unit_no[e] = c;
                c = fgetc(fp);
                printf("%c", unit_no[e]);
             }
             printf(" ");
             while(c == 32)
             {
                c = fgetc(fp);
                printf("1", c);
             }
             printf(" ");
             sym = fgetc(fp);
             printf("%c", sym);
             c = fgetc(fp);
             while(c == 32)
             {
                c = fgetc(fp);
             }
             printf(" ");
          }
          for(g = 0; g < 3; g++)
          {
             h_no[g] = c;
             c = fgetc(fp);
             printf("%c", h_no[g]);
          }
          printf(" ");
          while(c == 32)
          {
             c = fgetc(fp);
          }
          while(c != 32)
          {
             st_name[h] = c;
             c = fgetc(fp);
             printf("%c", st_name[h]);
             h++;
          }
          printf(" ");
          while(c == 32)
          {
             c = fgetc(fp);
          }
          while(c != 32)
          {
             st_type[j] = c;
             c = fgetc(fp);
             printf("%c", st_type[j]);
             j++;
          }
          printf(" ");
          while(c == 32)
          {
             c = fgetc(fp);
          }
          while(c != 32)
          {
             sub_name[k] = c;
             c = fgetc(fp);
             printf("%c", sub_name[k]);
             k++;
          }
          printf(" ");
          while(c == 32)
          {
             c = fgetc(fp);
          }
          for(l = 0; l < 4; l++)
          {
             postcode[l] = c;
             c = fgetc(fp);
             printf("%c", postcode[l]);
          }
          while(c != '\n')
          {
             c = fgetc(fp);
          }
          if(c == EOF)
          {
             eof = TRUE;
          }
       }fclose(fp);
    }

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    First of all, what are you trying to do? Because that code is a mess.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    im reading in an file e.g. batch.txt then it is supposed to print the contents out to the screen
    but it runs continuously.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Code:
         while(c != '\n')
          {
             c = fgetc(fp);
          }
    how do you know that there is another '\n' in the file?
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Quote Originally Posted by ck1
    im reading in an file e.g. batch.txt then it is supposed to print the contents out to the screen
    If that's all you want to do, it's possible in about two lines of code.

    Code:
    while no file errors occur and EOF is not reached
      read in a character from the file
      print that character to the screen

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    char c;
    /* ... */
    c = fgetc(fp);
    FYI: fgetc returns an int.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    32
    what ck1 is getting at is what he is reading in is the following in a file:

    123 Fake St Fakeville 3092
    Flat 234 / 234 Greg Rd Melbit 2309[possibly space space space]
    Unit 123 , 213 Pie Cres Hewton 2102[space space space space space]


    EOF here....

    he wants to read the info and eliminate the spaces between the words which may vary and may occur at the end of the line before '\n'. (yes this can be done with fscanf but its too late to change it now).

    is there a way to read the chars to the end of the line and find the end of file, and not fill up the array with null characters (or get funny y's for nulls).

    thanks everyone.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by djwicks
    and not fill up the array with null characters (or get funny y's for nulls).
    The funny y's being the EOF?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    32
    the funny y's are the empty spaces in the array. however this problem occurs because he is unable to find the end of file marker and set it to true to break the while loop.

    HELP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Array of Structures
    By dmkanz07 in forum C Programming
    Replies: 12
    Last Post: 04-19-2007, 09:55 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. checking for end of file
    By linucksrox in forum C Programming
    Replies: 7
    Last Post: 06-01-2004, 01:41 AM