pickpocket_123
pickpocket_123

Reputation: 15

How to get/use user input to specify outside file location in C? Will this work?

For context: this code is supposed to accept a value for a number of sensors, accept user input for the file path, and open up as many files as there are sensors. Each file has three columns and the for loop is supposed to put each column into a separate array and then open the next file and do the same.

I'm trying to get a file location specified by the user. The file location is outside the program files, somewhere else on the computer, so the length of the path is unknown.

I defined a character array to put the file location in, as a string; I'm going to put validation code there later since I know using a string to define a file path isn't all that elegant, if this is a valid way of obtaining the path.

char filelocation[100];

printf("Please enter the location of the files: \n");
scanf("%s", filelocation);

int BPsensors; 
printf("Please enter the amount of sensors used for blood pressure: ");
scanf("%d", BPsensors);
    do
    { 
        printf("This is not a valid value. Please enter a number greater than zero and less than or equal to five.");

    }while (BPsensors > 5 || BPsensors < 1);

char filename1[50];
for (int i = 0; i < BPsensors; i++)
{
    sprintf(filename1, "BP_%d.txt", i + 1); //this is where I edit the name of the file
    FILE * fpointer;
    fpointer = fopen(filename1, "r");
    if (fpointer != 0)
    {
        //store in the arrays
        int time1[10], BPD[10], BPS[10];
        if (fscanf(fpointer, " %d %d %d", &time1[i], &BPD[i], &BPS[i]) != 3)
        {
            printf("A problem occured with the file. ");
                // return to main menu
        }
        fclose(fpointer);
    }
}

I want to edit the file path before I open it, by putting the string before the file name, like this:

sprintf(filelocation, filename1, "%sBP_%d.txt", i + 1);

I am aware this is most likely not going to work, but I don't know enough C or programming, in general, to know what the correct thing to do here is. Will this work, and if so, what's the best way of writing the code for it?

Any help is extremely appreciated, and any criticism in general of this code is very very welcome!

Upvotes: 1

Views: 682

Answers (1)

Weather Vane
Weather Vane

Reputation: 34585

This is one way to enter a string of unknown length. I would perhaps #define CHUNKSIZE 16 but I restricted it for easy testing.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CHUNKSIZE   4
#define BPsensors   10

int main(void){
    char *filepath;
    char *filename;
    int maxlen = CHUNKSIZE;
    int index = 0;
    int ch;
    int i;

    // start with a small amount of memory
    filepath = malloc(CHUNKSIZE);                   // small amount of memory
    if(filepath == NULL)                            // check
        exit(1);
    printf("Enter your filepath:\n");

    // read path name char by char
    while((ch = getchar()) != EOF && ch != '\n') {
        filepath[index++] = ch;
        if (index >= maxlen) {                      // allow room for a terminator
            maxlen += CHUNKSIZE;                    // more string memory
            filepath = realloc(filepath, maxlen);   // get more memory   
            if(filepath == NULL)
                exit(1);
        }
    }
    filepath[index] = 0;                            // terminate string input
    printf("You entered: %s\n", filepath);


    // get enough memory for each file name
    filename = malloc(strlen(filepath) + 10);       // ensure enough for BPsensors etc
    if(filename == NULL)
        exit(1);

    // demonstrate the file names
    for (i = 0; i < BPsensors; i++) {
        sprintf(filename, "%sBP_%d.txt", filepath, i + 1);
        printf("Filename: %s\n", filename);
    }

    free(filepath);
    free(filename);    
    return 0;
}

Program session:

Enter your filepath:
C:\mytest\result\
You entered: C:\mytest\result\
Filename: C:\mytest\result\BP_1.txt
Filename: C:\mytest\result\BP_2.txt
Filename: C:\mytest\result\BP_3.txt
Filename: C:\mytest\result\BP_4.txt
Filename: C:\mytest\result\BP_5.txt
Filename: C:\mytest\result\BP_6.txt
Filename: C:\mytest\result\BP_7.txt
Filename: C:\mytest\result\BP_8.txt
Filename: C:\mytest\result\BP_9.txt
Filename: C:\mytest\result\BP_10.txt

Upvotes: 1

Related Questions