How To Append A Filename Within The Program After User Definition in C I have pr
ID: 3694583 • Letter: H
Question
How To Append A Filename Within The Program After User Definition in C
I have program that has the user entering the full path to the folder holding several datafiles.
for example So say the user enters: psfHomeDesktopHealthScoreHealthScoreDataFiles I need to change it within the program to: psfHomeDesktopHealthScoreHealthScoreDataFilesBT_1.txt
Ive 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:
I have program that has the user entering the full path to the folder holding several datafiles.
for example So say the user enters: psfHomeDesktopHealthScoreHealthScoreDataFiles I need to change it within the program to: psfHomeDesktopHealthScoreHealthScoreDataFilesBT_1.txt
Ive 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(" READING BP_1.txt..."); fgets(bp1_Line, 100, fp); sscanf(bp1_Line, "%s %s %s", bp1_Val1, bp1_Val2, bp1_Val3); printf("%s %s %s ", bp1_Val1, bp1_Val2, bp1_Val3); } fclose(fp); } }
Explanation / Answer
The below given program will concatenate two strings.
#include<stdio.h>
#include<string.h>
int main(){
char filepath[1000];
printf("Enter the path to the file holding the data files:");
scanf("%s", filepath);
char BP1_ext [] = "\BP1.txt";
strcat(filepath,BP1_ext);
printf("Appended filename is :%s",filepath);
}
strcat in string.h will concatenate two strings..
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.