0

Hey guys so is there a way to write a piece of code that will allow the user to name the file being created? So for instance in my code i have

File * ftpr
ftpr = fopen("my path/filenameiwanthere.txt", "w");

so in the area i wrote "file name i want here" am i capable of including a string literal without any spaces so the user can create the name of the file? for instance i was thinking of something like

printf("What would you like to save this file under?);
scanf("%s", filename);

if they insert "car1" i want the above to basically read

ftpr = fopen("mypath/car1.txt.", "w");

So the idea here is I have a friend that own a car detailing business and everything they do is pen and paper. Customer names, car, license plate, how much its going to cost, what needs to be done, who worked on the car, blah blah blah. But its all so completely unorganized so in an effort to help/ and strengthen my programming skills I would like to write them something useful. I have an idea of what exactly I will be doing. I was just wondering if there was a way to save the file name as something they would prefer. So if they were on their desktop computer at work i could have a folder that said "Cars" or whatever and in that folder would be multiple files with their desired name and all the information for the car. Thanks guys.

  • But what exactly are you having trouble with? Your question is very unclear. – Bauss Nov 05 '17 at 18:01
  • You could look how to do string concatenation in C here https://stackoverflow.com/questions/308695/how-do-i-concatenate-const-literal-strings-in-c , and then concatenate filename with "mypath/" and handle eventual error if the file don't exist. If I could give you an advice, writing it a higher level like Java or Python could make it easier for you as a start, with a lot less frustration in the way to help your friend. – rkouye Nov 05 '17 at 18:03
  • Sorry for the confusion. Idk how to include the name of the file the user picks into the path I tried. Fopen(“mypath/%s”, “w”); but that gives me a file name of %s.txt. So I want to prompt the user “Hey what do u want to save this file as?” They write “car1” so after the program runs it writes to a file called “car1” –  Nov 05 '17 at 18:04
  • You can also use path = sprintf("mypath/%s", filename) and then fopen(path , "w"). Basically your problem is string concatenation. – rkouye Nov 05 '17 at 18:06
  • https://stackoverflow.com/questions/5262128/what-is-a-safe-way-to-join-strings-in-c – Antti Haapala -- Слава Україні Nov 05 '17 at 18:39
  • @semako sprintf doesn't work like that tho – Antti Haapala -- Слава Україні Nov 05 '17 at 18:39
  • @AnttiHaapala ah sorry my bad correct usage : sprintf(char *str, const char *format, ...) – rkouye Nov 06 '17 at 08:55

1 Answers1

-2

You can use the strcat() function to concatenate two char arrays:

char fileName[10];
char path[50] = "myFilePath/";
scanf("%s", &fileName);
char *finalFileName = (char*) malloc(strlen(fileName));

for(i=0; i < strlen(fileName); i++){
    finalFileName += fileName[i];
    finalFileName++;
}
strcat(path, finalFileName);

//Now your final path with the given file name is in path.
//i.e, path = "myFilePath/finalFileName.txt"

Hope this helps, feel free to ask.

Amit-Kumar-G
  • 184
  • 1
  • 12
  • I’m going to try to incorporate everything you guys just mentioned. I just starting learning malloc. And trust me I’ve took on some research opportunities here at school and everything runs better with python. So I’ve been slowly but surely teaching myself the language. Thanks guys –  Nov 05 '17 at 18:11
  • Glad I could help. Feel free to take it as a right answer to close the topic. – Amit-Kumar-G Nov 05 '17 at 18:13
  • Also would I use %d in the scanf &filename statement if the user wanted a filename of “car” or “lexus” ? I figure you’d have to use %s? –  Nov 05 '17 at 18:14
  • Oops, nice catch :) – Amit-Kumar-G Nov 05 '17 at 18:15
  • 4
    Almost every single line in your answer has problems. First of all, you're using `scanf("%s")` when you really ought to use `fgets`. The buffer is way too small for almost any filename. The allocated memory area is too small to have the contcatenated name etc. The for loop is totally wrong, the increment is wrong, you're concatenating a string to a string literal and so on. – Antti Haapala -- Слава Україні Nov 05 '17 at 18:36
  • @AnttiHaapala .b.bbut you must be wrong - it's been accepted:) – Martin James Nov 05 '17 at 19:08
  • @MartinJames indeed, while one could err, two newbies cannot be wrong at the same time. – Antti Haapala -- Слава Україні Nov 05 '17 at 19:17