Thread: Get a file location in C so the program can read files from this location

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    7

    Get a file location in C so the program can read files from this location

    Hello, I'm currently working with a program where I ask the user to input a file path, such as C:\Users\Documents\Data. I take the input from the user and store it in a character array. This file path is a folder that contains several text files that I need to read data from.

    I also have file pointers created so that the program can open and read each file and store the data in an array. For instance (I believe this is correct):

    Code:
    Data1Ptr = fopen( "Data1.txt", "r" );
                    i = 0;
                    while (!feof(Data1Ptr)) {
                        fscanf(Data1Ptr, "%d", &name);
                        Name[i] = name;
                        i++;
                    }
                    fclose(Data1Ptr);
    (if this is incorrect, could y'all please let me know?)

    How do I get the program to recognize that the char array with the user's input is a file location and to go to that location so that it can start opening and reading files? I'm not sure what functions there are that can do this. I read about stdin and stdout but I don't really know how to use them and I am not sure how they will help me here. I don't have very much experience with file IO's, and I've asked many people and consulted many places but I haven't found much that could help. Thank you in advance for the help!

  2. #2
    Registered User
    Join Date
    May 2015
    Posts
    228
    Do not use feof like that. Better to use fgets when reading in a file.

    Code:
    while(fgets(name, sizeOfName, Data1Ptr) != NULL)
    {
        // code goes here
    }

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Append the filename to the path, and pass that to "fopen".

    You can also change your programs "working directory", but this requires non-portable system-specific code.

    Quote Originally Posted by blh View Post
    (if this is incorrect, could y'all please let me know?)
    Don't use "feof" to control your loop: FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    You could also read directly into "Name[i]".

  4. #4
    Registered User
    Join Date
    Apr 2016
    Posts
    7
    Quote Originally Posted by Matticus View Post
    Append the filename to the path, and pass that to "fopen".
    Could you explain how to do that? I've never done that before.

    This is an assignment and the user (grader) would enter the file location (which could be anywhere on the computer) so I don't think changing the working directory helps here.

    Thank you both for the feof warning, I will change that. Also thank you for letting me know I can read directly into that array.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Look into a function called "strcat".

  6. #6
    Registered User
    Join Date
    Apr 2016
    Posts
    7
    Okay I looked it up and tried some stuff, thank you for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assignment of read-only location (vectors)
    By jlangfo5 in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2010, 09:20 AM
  2. Read an arbitrary memory location?
    By xuftugulus in forum Linux Programming
    Replies: 4
    Last Post: 02-29-2008, 12:21 PM
  3. Location of the kernel source files in FC-4
    By kris.c in forum Linux Programming
    Replies: 2
    Last Post: 05-22-2007, 05:36 AM
  4. How to get the location of the program?
    By maxorator in forum C++ Programming
    Replies: 7
    Last Post: 09-10-2005, 04:07 PM
  5. File Location - Please help
    By Khelder in forum Windows Programming
    Replies: 2
    Last Post: 04-17-2004, 04:03 AM