Thread: Need help, Asking user for the name of the input file? is it possible?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Need help, Asking user for the name of the input file and using it? possible?

    Hello,
    I am working on my first programming assignment, its more of a practice assignment.

    I have been trying for a while now and I cannot figure out how I would be able to use the name of the input file given by the user to OPEN and .txt file and read in data.

    Essentially, I need to ask "Please enter the name of the input file."
    User enters: "lottery.txt"

    and my program is suppose to use that information given by the user to FOPEN that exact .txt file....

    Can someone please give me some hints or advice as to how to go about solving this problem?

    THANK YOU
    Last edited by matthayzon89; 08-28-2010 at 08:44 AM.

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    you will find the answer in file io in c on the -how do i?- sections on the homepage, also you will learn how to collect string input. i cant post code at moment, am using phone
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    char filename[50]={"\0"};
    char buffer[100];
    FILE *fp;
    
    fgets(filename, sizeof(filename), stdin);
    if(filename[strlen(filename)-1]=='\n')
      filename[strlen(filename)-1]= '\0';  //remove the newline char
    
    
    fp=fopen(filename, "rt");
    if(fp==NULL) {
      printf("\n Error opening %s", filename);
      return 1;
    }
    
    while((fgets(buffer, sizeof(buffer), fp)) != NULL) {
      //do something with buffer which has one row of text
      //do something else maybe
    }
    
    fclose(fp);
    I haven't run the above, but it will be quite close. You'll need to include both stdio.h and string.h.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    OT posts moved -> Two's complement
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  3. Search file for user input
    By ch68813 in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2007, 02:24 PM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 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