Thread: Opening a file with stdin?

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Ghost Planet
    Posts
    7

    Opening a file with stdin?

    Greetings all, this board has been a great help so far, and i feel kind of sheepish asking this because I've seen so many post conserning it, but none of the solotions have worked for me.

    Anyway I thought was going to be the easiest part of my program: opening a file which the user specifies... i've reviewed the tutorials and past posts and come up with this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    main()
    {
    FILE *fp;
    char filename[40];
    fgets(filename,40,stdin);
    
    if  ( ( fp=fopen(filename, "a+" ) ) != NULL )
    {
    fprintf(fp, "Testing...\n");
    fclose(fp);
    }
    else printf("Could not open the file: %s", filename);
    }
    It simpy isnt working for reasons unknown to me (I've also tried the method using BUFSIZ, but still not having any luck); any advice would be appreciated!

    Thanks
    Last edited by PseudoSane; 09-05-2006 at 11:16 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Your code doesn't compile. This is what it might look like after passing through a beautifier.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    main()
    {
       FILE *fp;
       char filename[40];
       fgets(filename,40,stdin);
    
       if ( (fp=fopen(filename, "a+") != NULL){
            fprintf(fp, "Testing...\n");
          fclose(fp);
          }
          else printf("Could not open the file: %s", filename);
          }
    Assuming you just missed the closing paren, read this:
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    When you hit enter, it's storing that at the end in your fgets call. You'll want to remove it. To illustrate this, change the "Could not open..." line to have something after the %s, and you'll see that whatever is after it is on a new line.


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

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you change the
    Code:
    else printf("Could not open the file: %s", filename);
    to
    Code:
    else printf("Could not open the file: \"%s\"", filename);
    you'll see the problem:
    Code:
    $ ./program
    file
    Could not open the file: "file
    "
    $
    To remove the newline, see the FAQ mentioned; the strchr() part.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Location
    Ghost Planet
    Posts
    7
    Quote Originally Posted by quzah
    When you hit enter, it's storing that at the end in your fgets call. You'll want to remove it. To illustrate this, change the "Could not open..." line to have something after the %s, and you'll see that whatever is after it is on a new line.


    Quzah.

    ha, wow I would of never noticed that with out that illustration, thx a ton all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM