FEOF Error

This is a discussion on FEOF Error within the C Programming forums, part of the General Programming Boards category; Hello, I am attempting to write a program that reads some text from a file, formats it, and then writes ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    4

    FEOF Error

    Hello, I am attempting to write a program that reads some text from a file, formats it, and then writes it to a different file. I originally wrote the program on windows and it ran fine. When I tried to run it on unix, however, reading the text file caused an infinite loop. I am using !feof to test if it has reached the end of file and getc to read the characters 1 at a time.

    I am new to unix and used pico to create a text file. Is there a certain eof character I need to add to the end or something?

    Any help would be greatly appreciated.....here is the code for reference (Sorry if its messy >.<)

    Code:
    #include <stdio.h>
    #include <assert.h>
    
    
    int main()
    {
            FILE *fp; //Input File
            FILE *fp2; //Output File
            char input_filename[50];
            char output_filename[50];
            
            char text[10000];  //Holds formatted text
            char buffer[100]; //Holds 1 line at a time
            char c;
            
            int i=0;
            int j=0;
            int k=0;
            int l=0;  
            
            int pos=0; //Position in the text array
            int max=30; //Width of text 
            int words=0; //Number of words in buffer array
            int difference=0; //Max-line_len
            int spaces=0; //Difference/words  
            int word_len=0;
            int line_len=0;
            int adjust=0; //Number of spaces added to line        
            int paragraph=0; //1 if new paragraph, 0 if not
            
            printf("Please enter the input filename\n");
            scanf("%s", &input_filename);
            fp = fopen(input_filename, "r");
            
            printf("Please enter the output filename\n");
            scanf("%s", &output_filename);
            fp2 = fopen(output_filename, "w");
            
            printf("Please enter the width\n");
            scanf("%d", &max);   
    
            max++;
            while(!feof(fp))
            {                      
                          c=fgetc(fp); //Reads from text file 1 character at a time
                          if (feof(fp))
                          {
                              
                              if (line_len+word_len>max)  //Checks if the final line is too long                        
                                     c=' ';                          
                          }
                          else if (c=='\n') //Handles new line formatting
                          {
                              c=fgetc(fp);   
                              if (c=='\n')  //Handles new paragraph formatting
                              {
                                 if (line_len+word_len>max)
                                 {
                                     c=' ';
                                     fseek( fp , -2 , SEEK_CUR );
                                 }
                                 else   
                                 {   
                                     line_len=line_len+word_len;
                                     word_len=0;                             
                                     words++;                                                 
                                     paragraph=1; 
                                     adjust=1;                                  
                                 }                
                              }
                              else //Adjusts location in file stream if not new paragraph
                              {                                                     
                                 c=' ';                                                 
                                 fseek( fp , -1 , SEEK_CUR );
                              }
                           }
                           
                           
                           if (c!=' '&&paragraph!=1) //Forms a word
                           {                      
                               buffer[i]=c;      
                               word_len++;
                               i++;
                           }                                         
                           else if (word_len+line_len<max&&paragraph!=1) //Checks if the next word will fit on the line
                           {                             
                               line_len=line_len+word_len+1;
                               word_len=0;
                               buffer[i++]=' ';
                               words++; 
                           }
                           else
                           {                           
                               difference=max-line_len; 
                               if (paragraph==1)
                                  difference--;
                               spaces=difference/(words);  
                               for(j=0;j<max-adjust;++j)
                               {                                                                                                                          
                                   text[pos]=buffer[j];
                                   pos++;   
                                    
                                   if (buffer[j]==' ') 
                                   {
                                      words--;
                                      if (words==1) //Checks if it is the last word in the line
                                      {                                            
                                         for(k=0;k<difference;k++) //fill in remainding spaces
                                         {
                                              text[pos]=' ';
                                              pos++; 
                                              adjust++;                                         
                                         }                                      
                                         for(j++;j<max-adjust;++j) //Copies remaining word
                                         {
                                              text[pos]=buffer[j];
                                              pos++;                                                        
                                         }
                                          
                                       } 
                                       else
                                       {                                       
                                           
                                          if (spaces!=0)
                                          {
                                              for (k=0;k<spaces;k++) //Distributes spaces evenly
                                              {
                                                  text[pos]=' ';
                                                  difference--;
                                                  pos++;
                                                  adjust++;
                                              }
                                          }
                                          else if (difference>0)                                      
                                          {
                                              text[pos]=' ';
                                              difference--;
                                              pos++;
                                              adjust++;
                                          }
                                        
                                        }       
                                      
                                   }   
                               }
                               
                               
                               if (paragraph==1)
                               {                  
                                   text[pos]='\n';
                                   pos++;
                                   text[pos]='\n';      
                                   pos++; 
                                   for (l=0;l<max+15;l++) //Clears the entire array                                                 
                                       buffer[l]='\0';
                                   line_len=0;
                                   i=0;
                                   word_len=0;
                                   words=0;
                                   adjust=0;
                                   paragraph=0;   
                               }          
                               else
                               {
                                   for (l=0;l<word_len;l++)  //Shifts the remaining word to the beginning of the array
                                   {        
                                        if (l==0&&buffer[line_len]==' ')
                                        {                                 
                                           line_len++;
                                           l--;
                                        }
                                        else
                                        {
                                             buffer[l]=buffer[line_len];
                                             line_len++;                                     
                                        }
                                   }           
                                   for (;l<max+15;l++) //Clears the remainder of the array                                                 
                                       buffer[l]='\0';
                                           
                                                                                 
                                   text[pos]='\n';      
                                   pos++;                             
                                   line_len=word_len+1;                           
                                   i=word_len;
                                   buffer[i++]=' ';                          
                                   word_len=0;           
                                   words=1;
                                   adjust=0;
                               }
                               
                           }
                   
            }
    
    
            fclose(fp);
            fclose(fp2);
    }
    Last edited by kitaman27; 02-14-2009 at 02:27 AM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > char c;
    Declaring c as an int will likely fix your problem. Also a better way to control the loop is:
    Code:
            while((c=fgetc(fp)) != EOF)
            {

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Thank you for your reply.

    Unfortunately after changing what you suggested, I still get a segmentation fault when running the program. >.<

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    You should avoid using feof() to control a loop: it is usually incorrect, unless reading from the stream is the last thing you do before going to the next iteration.

    However, that may not be the current problem. I suggest that you indent your code more consistently and separate the code in that huge while loop into functions that are well named.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    I also tried using while(c=fgetc(fp)) != '\0') to control the loop, but had the same problem with an infinite loop.

    (And I appologize for the messy code)

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Quote Originally Posted by kitaman27
    I also tried using while(c=fgetc(fp)) != '\0') to control the loop, but had the same problem with an infinite loop.
    As swoopy pointed out, it should be:
    Code:
    while((c = fgetc(fp)) != EOF)
    where c is an int
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I just can't stop shaking my head at this. First, because a file is formatted when it's created, so it displays correctly in the software, and on the system, intended.

    Taking a file and "formatting" it, is very likely to goof it up. Only exception would be plain text, and even then, different editors and users, have different width of line preferences. Even with plain text, the file will be likely less properly formatted, not more.

    Second, the program here looks like somebody is building a piano. This is a simple task. Why all the frigging code?

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    4
    Errm well it isn't for any practical use, just an assignment. My first c program actually, which may be why it is so complicated.

    In addition to making lines a certain length, words couldn't be cut in half, it had to be left and right justified, and spaces had to be distributed cleanly.

    while((c = fgetc(fp)) != EOF) where c is an int

    still doesn't work for some reason. I'm not sure why it would work fine on windows, but not on unix.

    Thank you all for you're suggestions

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Quote Originally Posted by kitaman27
    while((c = fgetc(fp)) != EOF) where c is an int

    still doesn't work for some reason. I'm not sure why it would work fine on windows, but not on unix.
    Test with a simpler example, e.g.,
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void getInputFilename(char *input_filename, size_t size);
    
    int main(void)
    {
        FILE *fp;
        char input_filename[50];
    
        getInputFilename(input_filename, sizeof(input_filename));
    
        fp = fopen(input_filename, "r");
        if (fp)
        {
            int c;
            printf("The content of '%s' is:\n", input_filename);
            while ((c = fgetc(fp)) != EOF)
            {
                putchar(c);
            }
            fclose(fp);
        }
        else
        {
            printf("Could not open '%s'\n", input_filename);
        }
        return 0;
    }
    
    void getInputFilename(char *input_filename, size_t size)
    {
        char *newline;
        printf("Please enter the input filename: ");
        fgets(input_filename, size, stdin);
        newline = strchr(input_filename, '\n');
        if (newline)
        {
            *newline = '\0';
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21