files

This is a discussion on files within the C Programming forums, part of the General Programming Boards category; hi im trying to read from a file that has been created by the user. my problem file = fopen("data/hello.txt", ...

  1. #1
    ,,,,,,,
    Guest

    files

    hi im trying to read from a file that has been created by the user.

    my problem

    file = fopen("data/hello.txt", "r");

    is that. I want the user to enter the name of a file he/she wants to read rather than specifying it in the code...

    how would i do that?
    thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You can use a string as the file name.
    Code:
    char a[] = "data/hello.txt";
    file = fopen(a, "r");
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    Or in your case you could request a filename from the user, placing it in a variable and then using the variable to open the file.
    Code:
    FILE *fd;
    char file[256];
    printf("What file would you like to open?\n ");
    gets( file );
    fd=fopen(file, "r");

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by _Cl0wn_
    Or in your case you could request a filename from the user, placing it in a variable and then using the variable to open the file.
    Code:
    gets( file );
    Bad Fido. No cookie. Never use gets. Always assume your user will do everything in their power to break your user interface. Always. As such, never ever use gets.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 07:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 04:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 05:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21