Thread: Reading file specified by user

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

    Reading file specified by user

    In a program I'm writing I need to prompt the user to input the name of a file to read from.

    With all the examples I have found, the file name is pre-defined.

    Example:
    FILE *in;
    in = fopen("file.txt", "r");
    I need to replace "file.txt" with a string defined by the user, how could I do this?
    Help is appreciated.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Get the string from the keyboard in the usual way with scanf() then substitue your string variable for the literal name in fopen().

    As in fopen(MyStrVar,"r");

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    That solved my problem. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help Reading a file...
    By RoRo in forum C Programming
    Replies: 7
    Last Post: 11-23-2010, 08:18 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

Tags for this Thread