0

In a C-program I am trying to create a textfile where the file name should be based on the input to one or two char arrays in a structure.

At the moment I query the filename like this:

printf("Type a filename:");
scanf("%s", &filename);
strcat(&filename, ".txt");
pFile=fopen(filename,"a");

…but let's say my input to my char array is John , how could this input be used to create the filename John.txt ?

..or even better: combine a name from two char arrays:

fgets(pIndex->name, 20, stdin);  //lets say input here is John
fgets(pIndex->country, 20, stdin);  //...and input here is England

to generate a filename like JohnEngland.txt

Thanks a lot !

-Espen

Espen
  • 147
  • 4
  • 16
  • 1
    What is the problem, where are you stuck? – Iharob Al Asimi Dec 29 '14 at 20:15
  • I am stuck in how to automatically create the filename based on the input to the arrays. I would rather not ask the user to write these to values a second time. So if the input to the first array was *John*, and to the second *England* - the file created should automatically be named JohnEngland.txt – Espen Dec 29 '14 at 20:19
  • Note: you don't need address of `&filename` for `scanf()` or `strcat()`, just `filename` will do if it was declared `char filename[..]`. – Weather Vane Dec 29 '14 at 20:43

3 Answers3

2

You can generate a filename like JohnEngland.txt with this

char filename[45];
sprintf (filename, "%s%s.txt", pIndex->name, pIndex->country);
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • This is definenitly vunerable to bufferoverflows – hetepeperfan Dec 29 '14 at 20:32
  • @hetepeperfan that's true, but OP limited the string entry lengths, so I added an appropriate `fielname[]`. – Weather Vane Dec 29 '14 at 20:33
  • No luck... Program runs, but no file is created. If I try to printf("%s%s.txt", pIndex->name, pIndex->country); it adds the newline (\n) to the output.. – Espen Dec 29 '14 at 20:36
  • So did you actually get the original input with `fgets()` which keeps the `newline` entered, and not with the `scanf()` you posted? – Weather Vane Dec 29 '14 at 20:41
  • At the moment I query the user manually using scanf, but my goal is to create the filename automatically based on the values i'we already got from the user using fgets (when getting the input to my two arrays). The newlines is added after John and after England. – Espen Dec 29 '14 at 20:48
  • @Espen, you have already told us in your post you use `fgets(pIndex->country, 20, stdin);`. That is where the `newline` is coming from and you need to remove it from each string record. – Weather Vane Dec 29 '14 at 20:56
  • @Espen you can remove a trailing `newline` like this. `char *sptr; if (sptr=strchr(pIndex->Country,'\n')) *sptr=0;` – Weather Vane Dec 29 '14 at 21:09
  • @WeatherVane That worked like a charm: both the answer and the solution for removing the trailing newline. Thanks for your time, it saved my day :) – Espen Dec 29 '14 at 21:53
1

To prevent overflow do

snprintf(filename, sizeof(filename), "%s%s.txt", pIndex->name, pIndex->country);

or the MS Windows variant

sprintf_s(filename, sizeof(filename), "%s%s.txt", pIndex->name, pIndex->country);
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
1

You can use the function strcat and friends to concatenate multiple strings. I prefer strncat over strcat especially with user input, to prevent bufferoverflows. also don't use "%s" in the scanf, because that allows the user to insert any length of string which in turn also leads to buffer overflows.

#include <string.h>

const char* suffix = ".txt";
char filename[1024];
char* output = NULL;

scanf("%50s", filename); // Don't use %s because that could lead to a buffer overflow and is therfor insecure.
output = strncat(filename, suffix, 1024);

after the strncat filename will have the suffix appended. check man strcat or your local resource for other issues involving strncat.

hetepeperfan
  • 4,292
  • 1
  • 29
  • 47
  • should consider changing: output = strncat(filename, suffix, strlen(suffix)); _(1024 is excessive)_ (+1) – ryyker Dec 29 '14 at 21:58