Thread: help with f I/O

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    help with f I/O

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc , char *argv[] )
    {
        FILE *fPtr;
        char s;
    
    
        if ( argc == 1 ){
           printf("Usage: [Option] <Filename.txt>\n"
                  "-r Read the content of a textfile\n"
                  "-w Write to a Testfile\n");
                  
    
           }
       else {
                 if ( strcmp( argv[1], "-r" ) == 0 ){
                      if ( ( fPtr = fopen(argv[2],"r")) != NULL ){
    
                            s = getc( fPtr );
                            fputc(s, stdout);
                            while( !feof( fPtr ) ){
                              s = getc( fPtr );
                              fputc(s, stdout);
                            }
                      }
    
                      else
                           printf("****ERROR*****\nCould not open %s\n", argv[2] );
    
                 }
    
                 else if ( strcmp(argv[1],"-w" ) == 0 ){
                      if ( ( fPtr = fopen(argv[2],"w")) != NULL ){
    
    
                         while ( !feof( stdin ) ){
                               s = fgetc(stdin);
                               fputc(s,fPtr);
                         }
                      }
                      else
                          printf("*****ERRORR*****\nCould not open %s\n", argv[2] );
    
                 }
    
                else
                    printf("\nWrong option\n");
                fclose(fPtr);
    
                }
    
    
    
    
    
    
    
    
    
    
          return 0;
    }
    First of all sorry if my code is a bit messy .Well at the movement i am not my self had alil to much gin .Okay so to the point ..I just did this programm in not my sence i cant find the problem in the code like when i use the option -w it should write to a file but it does not .Some times it does ?? and when i use the -r option to read it does print the content of the file but also prints Wrong option Why ???
    Thanks alot guys

    DaIn
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Never use feof.

    Code:
    s = getc( fPtr );
    fputc(s, stdout);
    while( !feof( fPtr ) ){
        s = getc( fPtr );
        fputc(s, stdout);
    }
    Do this instead:

    Code:
    while( (s=fgetc( fPtr )) != EOF )
    {
        fputc( s, stdout );
    }
    Do the same for your read.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. asynchronized I/O == multiplexing I/O?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 07-24-2006, 10:06 AM
  2. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  3. Nonblocking I/O
    By fnoyan in forum Linux Programming
    Replies: 4
    Last Post: 11-29-2005, 04:37 PM
  4. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM
  5. WSAAsyncSelect I/O Mode :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 05-12-2002, 03:23 PM