-3

So I'm trying to create a file and save it to a desired directory.

eg: user-input:

Directory? c:\user\sample\

Name? hello.txt

This is what I've tried so far:

char str[200],str2[200];
FILE * out_file;
fgets(str,sizeof str,out_file);
fgets(str2,sizeof str2,out_file);
out_file = fopen(str+str2,"w");

Can someone please help me with this issue?

Antonio
  • 19,451
  • 13
  • 99
  • 197
Joel
  • 5,732
  • 4
  • 37
  • 65
  • 1
    You cannot concatenate strings with the `+` operator in C. Use `strcat` or any other appropriate function for this purpose. – fuz Jan 20 '16 at 12:05
  • As, @FUZxxl said, you will have to use `strcat` to concatenate the strings. – Box Box Box Box Jan 20 '16 at 12:06
  • 5
    Read this [How to concatenate 2 strings in C?](http://stackoverflow.com/questions/8465006/how-to-concatenate-2-strings-in-c) – mazhar islam Jan 20 '16 at 12:08
  • You should have at least reported which kind of error you had, i.e. at compile time (which error?) or runtime (which wrong behaviour?) – Antonio Jan 20 '16 at 13:15

3 Answers3

1

A few issues:

  • You'll have to use strcat() to concatenate the strings.

  • Here you are reading from out_file before opening it.

    fgets(str,sizeof str,out_file);
    fgets(str2,sizeof str2,out_file);
    

A quick example (no error checking done):

char str[200], str2[200];
char fname[400];
FILE *out_file;

printf("\nEnter path: ");
scanf("%199s", str);  
printf("\nEnter filename: ");
scanf("%199s", str2);

strcpy(fname, str);
strcat(fname, str2);

out_file = fopen(fname, "w");

Or, a shorter way:

char str[400],str2[200];
FILE * out_file;

printf("\nEnter path: ");
scanf("%199s", str);  
printf("\nEnter filename: ");
scanf("%199s", str2);

strcat(str, str2);

out_file = fopen(str, "r");
Danny_ds
  • 11,201
  • 1
  • 24
  • 46
1

See How to concatenate 2 strings in C?.

Also, you should use strcat.

Check out this tutorial.

An example in this is:

/* Example using strcat by TechOnTheNet.com */

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

int main(int argc, const char * argv[])
{
   /* Define a temporary variable */
   char example[100];

   /* Copy the first string into the variable */
   strcpy(example, "TechOnTheNet.com ");

   /* Concatenate the following two strings to the end of the first one */
   strcat(example, "is over 10 ");
   strcat(example, "years old.");

   /* Display the concatenated strings */
   printf("%s\n", example);

   return 0;
}

In your case it would be:

char file_name[200 + 200];
file_name [0] = '\0'                    // To make sure that it's a valid string.

strcpy (file_name, str);                // Concatenate `str` and `file_name`
strcat(file_name, str2);                // Concatenate `str2` and `file_name`

out_file = fopen(file_name, "w");       // Open the file.

Also, thanks to 'laerne' for pointing out some mistakes.

Community
  • 1
  • 1
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
0

You cannot concatenate strings like this in C! *

You'd better use strcat:

char fname[200+200];

strcat(fname, str);
strcat(fname, str2);

out_file = fopen(fname, "w");

* Although you are allowed to add pointers. In your case it doesn't do what you expect.

ForceBru
  • 43,482
  • 10
  • 63
  • 98