Thread: File read

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    32

    Question File read

    Hi there,

    I am having some problems prompting the user to enter a command in.

    for example i want the command"print" to be recongised multiple time.

    Its supposed to behave like this: Enter command > print

    and then it prints out the contents of the file.

    However when my program does this: Enter Command > print

    print

    and then it prints

    i have to type print in 2 times for it to work. I been trying to figure what is the error but i cant see it.

    Code:
      char input[100];
       
       fp = fopen(argv[1],"r");
    
       while(fopen(argv[1],"r")){
    
            printf("Enter command > ");
    
            fgets(input,100,stdin);
    
            if(strcmp(fgets(input,100,stdin),"print\n") == 0){
            //i have another while loop with fgets to read stuff in file
    
           }else if(strcmp(fgets(input,100,stdin),"quit\n") == 0)
             
            fclose(fp);        //closes program   
    
          }
    
    }
         
     reutrn 0;
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    Code:
    fgets(input,100,stdin);
    
            if(strcmp(fgets(input,100,stdin),"print\n") == 0)
    Used fgets(..) twice, i hope this is pseudo-code.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    32
    Ok i got rid of the multiple fgets and it works but y does the command print not work after the first time??
    Also when i enter the command quit the program wont close itself?/ why is that?
    Code:
       char input[100];
       
       fp = fopen(argv[1],"r");
    
       while(fopen(argv[1],"r")){
    
            printf("Enter command > ");
           
            if(strcmp(fgets(input,100,stdin),"print\n") == 0)
    
           //i have loop to read stuff in from a file
    
           }else if(strcmp(fgets(input,100,stdin),"quit\n") == 0){
    
            fclose(fp); ///supposed to close the file
      
           }
    
      }
    
    return 0;
    
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while(fopen(argv[1],"r"))
    You keep re-opening the file.

    > if(strcmp(fgets(input,100,stdin),"print\n") == 0)
    Separate them into separate steps.
    fgets() can return NULL, which would hose your strcmp
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    32
    I have to keep the file open so that it can keep printing "Enter command >". Also i dont get what you mean by fgets hosing the strcmp. The print command only works once and the exit command doesnt work at all.I went through it logically and dont know why it is wrong.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The file is open until you close it.

    > Also i dont get what you mean by fgets hosing the strcmp
    If fgets() returns NULL, which it does at end of file, you have
    strcmp( NULL, "print\n")

    As soon as strcmp() dereferences NULL, it's bye bye program in a segmentation fault.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    32
    i got to keep printing Enter command > when the user has not finish issuing commands and then the program ends itself through the command quit. So how do i do this and at the same time compare it to a command i enter like "print". Also my quit command wont work. Any ideas why?
    I also corrected it to compare to NULL.

    Code:
    char input[100];
       
       fp = fopen(argv[1],"r");
    
       while(fopen(argv[1],"r")){
    
            printf("Enter command > ");
           
            if(strcmp(fgets(input,100,stdin),"print\n") == 0 && !NULL){
    
               //i have loop to read in the stuff from file
    
         }else if(strcmp(fgets(input,100,stdin),"quit\n") == 0){
    
            fclose(fp); //why does quit command not work
      
           }
    
    }
    
    return 0;
    
    }
    Last edited by compnub1; 05-16-2010 at 07:04 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This is what I mean by comparing with NULL
    Code:
    while ( fgets ( input, sizeof input, fp ) != NULL ) { /* This compares to NULL */
        if ( strcmp( input, "print\n") == 0 ) {
            FILE *fp = fopen("argv[1],"r");
            // read from fp here
            fclose( fp );
        } else
        if ( strcmp( input, "quit\n" ) == 0 ) {
            break;
        }
    }
    It doesn't print a prompt, but that's an exercise for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 02:01 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM