0

I have program that has the user entering the full path to the folder holding several datafiles.

for example So say the user enters:

\\psf\Home\Desktop\HealthScore\HealthScore\DataFiles

I need to change it within the program to:

\\psf\Home\Desktop\HealthScore\HealthScore\DataFiles\BT_1.txt

I've tried adding the string together with one a pointer, the other is an array and with both parts being pointers.

Here is a sample of my latest attempt:

char filepath[1000];
FILE* fp;
char BP1_ext [] = "\\BP1.txt";

printf("Enter the path to the file holding the data files:");

            fflush(stdin);
            scanf("%s", filepath);
            //////////////////////BLOOD PRESSURE//////////////////////
            if (bpSensors == 1)
            {
                filepath = (filepath + BP1_ext);
                fp = fopen(filepath, "r");
                if (fp == NULL)

                {
                    puts("ERROR OPENING FILES");
                    exit(EXIT_FAILURE);
                }

                else
                {
                    while (!feof(fp))
                    {
                        printf("\n\nREADING BP_1.txt...");
                        fgets(bp1_Line, 100, fp);
                        sscanf(bp1_Line, "%s\t%s\t%s", bp1_Val1, bp1_Val2, bp1_Val3);
                        printf("%s\t%s\t%s\n", bp1_Val1, bp1_Val2, bp1_Val3);
                    }
                    fclose(fp);

                }

            }
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

2 Answers2

2

You can make use of strcat() to concatenate strings. sample usage will be

strcat (filepath, BP1_ext);

provided, filepath has enough room to hold the concatenated string.

That said,

  1. fflush(stdin) is undefined behavior, don't use that.
  2. Please see Why is “while ( !feof (file) )” always wrong?
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Indeed on point 2, OP can make the loop like this: `while (fgets(bp1_Line, 100, fp) != NULL) {...}` and better move the `printf` cue outside the loop too. – Weather Vane Apr 26 '16 at 19:40
  • i had it all consolidated because there are 25 different conditions i have to test and open various files depending on them, i only posted one of them because i felt like people wouldn't want or need to read through a few hundred lines of code. but, i will try out the suggested while loop, but i think im going to leave printf in the loop for simplicity. – Jessi Brattain Apr 26 '16 at 19:57
0

The problem is here:

filepath = (filepath + BP1_ext);

Use strcat to from string.h to concatenate your strings (strcat(destionation, source)):

strcat(filepath, BP1_ext);
Günther Jena
  • 3,706
  • 3
  • 34
  • 49
  • okay. i did that. but would i still just use fp = fopen(filepath, "r")? and will i be able to change filepath multiple times then? for example can strcat(filepath, BP1_ext); strcat(filepath, BP2_ext); be used at the same time without effecting each other? – Jessi Brattain Apr 26 '16 at 19:49