Input text files into programs...

This is a discussion on Input text files into programs... within the C Programming forums, part of the General Programming Boards category; How can I have the user input a text files into a c program? Basically, instead of having a static ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    10

    Input text files into programs...

    How can I have the user input a text files into a c program?

    Basically, instead of having a static file such as: fp = fopen("text.txt", "r");...

    ... how can I have the user input their own file?

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Just prompt the user to enter the filename. Save it as a string and then use fopen. Read how to get a line of text FAQ.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Instead of using a literal string as your filename ("inputFile.txt"), have a char array of appropriate size (bigger is better than smaller), and allow them to enter their own filename, into it. scanf("%s, filename) or fgets(filename, sizeof(filename), stdin), will work.

    then

    Code:
    FILE *fp = fopen(filename, "r");
    
    //and check that fp is not null:
    
    if(!fp) {
      printf("\nError opening the %s file\n", filename);
      //etc.
    }

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    2
    I would suggest you to use command line arguments.
    Code:
    int main(int argc , char *argv[])
    {
    int *fp;
    fp = fopen(argv[1] , "r");
    .
    .
    .
    .
    return 0;
    }

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sasi Srinivas View Post
    I would suggest you to use command line arguments.
    Code:
    int main(int argc , char *argv[])
    {
    int *fp;
    fp = fopen(argv[1] , "r");
    .
    .
    .
    .
    return 0;
    }
    That wasn't the question....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using multiple .cpp files for programs
    By DevEight in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2007, 08:56 AM
  2. Input for windows programs
    By darealnash in forum C++ Programming
    Replies: 7
    Last Post: 08-31-2004, 01:31 PM
  3. Text Programs
    By KrAzY CrAb in forum C Programming
    Replies: 7
    Last Post: 06-07-2002, 12:45 AM
  4. How do i use .ico files with programs?
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 04-18-2002, 07:43 PM
  5. C++ Programs -- Any input appreciated
    By Kyoto Oshiro in forum Game Programming
    Replies: 0
    Last Post: 02-27-2002, 11:22 PM

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