Thread: FEOF Error

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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, 08: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