Reputation: 11
I wanna ask you, if is it possible to have user inputed name of FILE*... something like this:
#include <stdio.h>
int main() {
char* name1, name2, path1, path2;
printf("Insert first file name and path\nExample: file, c:\\path\\to\\file.txt");
scanf("%s, %s", name1, path1);
printf("Insert second file name and path\nExample: file, c:\\path\\to\\file.txt");
scanf("%s, %s", name2, path2);
FILE* name1;
FILE* name2;
name1 = fopen(path1, "a+");
name2 = fopen(path2, "a+");
...
}
So on the console will be:
Insert file name and path
Example: file, c:\path\to\file.txt
so if user inserts:
File1, c:\file1.txt
File2, c:\file2.txt
I would like the "code" to look something like this:
FILE* File1;
File1 = fopen("c:\file1.txt", "a+");
FILE* File2;
File2 = fopen("c:\file2.txt", "a+");
Thanks for help ;)
Upvotes: 0
Views: 1957
Reputation: 8313
You have problem with all strings:
char* name1, name2, path1, path2;
name1
is uninitialized pointer, name2
, path1
and path2
are single characters variables.
I guess it should be something like this:
#define MAX_NAME 40
#define MAX_PATH 255
char name1[MAX_NAME], name2[MAX_NAME], path1[MAX_PATH], path2[MAX_PATH];
Another issue:
FILE* name1;
FILE* name2;
name1
and name2
are declared in the same scope, so I guess you get an "Already defined" error.
So, if you want to attached name and path to a FILE*
, the best way to do so is to use a struct
as follows:
typedef struct File_t
{
char name[MAX_NAME];
char path[MAX_PATH];
FILE* file;
};
File_t file1, file2;
scanf("%s, %s", file1.name, file1.path);
file1.file = fopen(file1.path, "a+");
scanf("%s, %s", file2.name, file2.path);
file2.file = fopen(file2.path, "a+");
Upvotes: 1
Reputation: 780879
Escape sequences aren't processed in data that's read from a stream, so the user should type c:\file.txt
, not c:\\file.txt
.
Upvotes: 4