Program hangs

This is a discussion on Program hangs within the C Programming forums, part of the General Programming Boards category; This is supposed to reverse every word in a file. From the command line output looks like its stuck somewhere. ...

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Question Program hangs

    This is supposed to reverse every word in a file. From the command line output looks like its stuck somewhere.

    Code:
    /*Reverse every word in a file*/
     
    #include<stdio.h>
    int main()
    {
          FILE *fin,*fout;
          int i=0, flag=0;
          char temp[20],ch;
       
          fin=fopen("/home/abhishek/Desktop/lol.txt","r");
          if(fin==NULL)
          {
              printf("\nError opening input file\n");
              return 1;
          }
          fout=fopen("/home/abhishek/Desktop/rofl.txt","w");
          if(fout==NULL)
          {
              printf("\nError opening output file\n");
             return 2;
         }
         ch=fgetc(fin);
         if(ch==EOF)
              flag=1;
          while(ch!=EOF && flag==0 )
          {
             i=0;
             while(ch!=EOF && ch!=' ' && ch!='\t' && ch!='.')
             {
                  temp[i]=ch;
                  printf("\nreading....&#37;d",i);
                  i++;
                 ch=fgetc(fin);
                 if(ch==EOF)
                      flag=1;
             }
              i--;
             while(i>=0)
             {
                 printf("\nwriting....%d",i);
                 fputc(temp[i],fout);
                 i--;
            }
        }
           printf("\nDone!\n");
           fclose(fin);
           fclose(fout);
           return 0;
    }
    CLI output

    abhishek@dreamcatcher:~/Projects/C$ ./reverse

    reading....0
    reading....1
    reading....2
    reading....3
    reading....4
    reading....5
    reading....6
    writing....6
    writing....5
    writing....4
    writing....3
    writing....2
    writing....1
    | <-------cursor keeps waiting here for don't know what
    Thanks for the help
    Last edited by abh!shek; 05-24-2008 at 05:39 PM.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    fgetc() returns an int not a char (can cause EOF problems).

    Flag is useless,
    Code:
    int ch;
    
    /* ... */
    
    while((ch = fgetc(fin)) != EOF)
    {
        /* blah */
    }
    Is suffice

    For a speed up perhaps read x bytes at a time (making sure you stop on the end or start of a word).

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    I wonder if I declare ch as an int, how will these things react:
    ch!=' ' && ch!='\t' && ch!='.'

    temp[i]=ch;
    because temp[i] is char and ch is int...

    I did change ch to int but the program still behaves the same for some reason :-/
    Last edited by abh!shek; 05-24-2008 at 10:28 PM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by abh!shek View Post
    I wonder if I declare ch as an int, how will these things react:
    All non-zero values are true, zero is false. L'\0', and '\0' are false, all other character literals are true.

  5. #5
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Got it working

    Code:
    #include<stdio.h>
    int main()
    {
       FILE *fin,*fout;
       int ch,flag=0,count;
       char temp[20];
       fin=fopen("/home/abhishek/Desktop/lol.txt","r");
       if(fin==NULL)
       {
          printf("\nError opening input file!");
          return 1;
       }
       fout=fopen("/home/abhishek/Desktop/rofl.txt","w");
       if(fout==NULL)
       {
          printf("\nError opening output file!");
          return 2;
       }
       ch=fgetc(fin);
       if(ch==EOF)
          flag=1;
       while(flag==0)
       {
          count=0;
          while(flag==0 && ch!=' ' && ch!='.' && ch!=',')
          {
         temp[count]=ch;
         count++;
         
         ch=fgetc(fin);
         if(ch==EOF)
            flag=1;
          }
         
          count--;
         
          while(count>=0)
          {
         
           fputc((int)temp[count],fout);
             count--;
          }
          fputc(' ',fout);
          ch=fgetc(fin);
       }
       fclose(fin);
       fclose(fout);
       printf("\nDone!\n");
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-12-2007, 12:42 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


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