Thread: User inputted filename

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    13

    User inputted filename

    Hi, I have a string which stores the name of the file the user has inputted, but I have no clue how to input this into the opening of that file!

    I thought that fp=fopen("%s", filename, "r"); would work, but it obviosuly is incorrect as I get an error.

    How can I get the string stored in filename into the filename area of the opening code?

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    You should look at the prototype of fopen first

    Code:
    FILE * fopen ( const char * filename, const char * mode );
    the first paramters is then const char *, which mean it can take any char pointer. In your if your read the file name in to variable like

    Code:
    scanf("%s", filename);
    The file will be your char * pointer. Send that as your first argument to fopon literally. Like

    Code:
    fopen(filename,"r");
    PS: And ofcourse do all the error checking.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  2. timed user input
    By sainiabhishek in forum C Programming
    Replies: 4
    Last Post: 04-01-2009, 11:59 AM
  3. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM
  4. letting the user type in the filename.
    By doampofo in forum C++ Programming
    Replies: 4
    Last Post: 04-16-2003, 09:29 PM
  5. Inputted values into an array
    By Panther in forum C Programming
    Replies: 6
    Last Post: 04-11-2003, 10:15 AM