0

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 ;)

  • Did you try running it to see if it works? – Liftoff Jul 25 '18 at 21:52
  • Possible duplicate of [How do I define and pass in a file name to fopen() from command line?](https://stackoverflow.com/questions/8625740/how-do-i-define-and-pass-in-a-file-name-to-fopen-from-command-line) –  Jul 25 '18 at 21:54
  • 1
    @David Of course... I'm asking 'cause ti doesn't work – Martin Šimara Jul 25 '18 at 21:56
  • 1
    The user shouldn't escape the backslash. You only need to do that in source code. – Barmar Jul 25 '18 at 21:56
  • 1
    While I won't start a religious war over the relative merits of `char* path;` and `char *path;` it's fairly clear that the desire is for all four variables to be pointers. In that case, this code will exhibit undefined behavior when executing the two `scanf()` statements. Also, using the same variable name for two different variables, e.g. `name1` is probably not going to work as desired. – dgnuff Jul 25 '18 at 22:23
  • 1
    You can't do this, because variable names in C only exist in your code, not in the compiled program. I assume that you want the user to input the file name later and be able to get the corresponding `FILE*` based on that name. For that, you need to create some sort of lookup table. You would use an `std::map` in C++, but I'm not sure how this would be done in C. – eesiraed Jul 25 '18 at 23:25
  • *char\* name1, name2, path1, path2;* A perfect example of why defining more than one variable on a line is generally a very bad idea. That declares **one** `char *` - `name1`, and **three** plain `char` variables. `name2`, `path1`, and `path2` are all simple `char` variables. Stuffing as much code as you can onto one line is generally a very bad idea - unless you like to hide bugs. – Andrew Henle Jul 26 '18 at 09:36

2 Answers2

4

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

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+");
SHR
  • 7,940
  • 9
  • 38
  • 57
  • 1
    Perhaps reading this [question/answer](https://stackoverflow.com/questions/9449241/where-is-path-max-defined-in-linux) would be better that using your own `MAX_PATH`. Also change `scanf` to ensure that no buffer overruns – Ed Heal Jul 26 '18 at 09:25