0

Suppose I have two methods: int main(void) and void func(char *FileName).

The main method will scan in, the name of a file from a user then save the value to char fileInput[20], then call func(fileInput).

In the func method, I want to open a file FILE *infile = fopen(/*FileName as string value*/, "rb");, but using the parameter value as the file name to be opened.

void func(char *FileName){
    //Open the file.        
    //FileName = address of first element of char array
    //*FileName = value of first element of char array
    //But How can I use the all the elements of the char array as a string ?
    FILE *infile = fopen(/*FileName as string value*/, "rb"); 
}

int main(void) {
    //Assuming the user will always enter a valid value.
    char fileInput[20];
    //Prompt the user to input a value.
    printf("File name then enter: ");
    //Save the input value to fileInput.
    scanf("%s", fileInput);
    func(fileInput);
}

As an example, if the fileInput[20] = "hello";, then call the func(fileInput);

How can i open a file in the func method using the "hello" value?

FILE *infile = fopen(value of fileInput = "hello", "rb");

IrAM
  • 1,720
  • 5
  • 18
jungrylls
  • 29
  • 6
  • Does this answer your question? [C fopen vs open](https://stackoverflow.com/questions/1658476/c-fopen-vs-open) – Zois Tasoulas Feb 02 '21 at 00:01
  • 3
    `fopen(FileName, "rb")` – kaylum Feb 02 '21 at 00:05
  • Consider opening the file in `main()` and then passing the `FILE*` pointer as a parameter. Unless you can open **and validate** the file is open for reading in `main()`, there is no need to make the function call. NEVER use `scanf()` with `"%s"` without a *field-width* modifier, it is no safer than `gets()`, See: [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/q/1694036/3422102) – David C. Rankin Feb 02 '21 at 01:53
  • @zois thanks for the reply, but no I did not mean to go in depth in fopen vs open route. My question comes from doing my school assignment and primary focus for us right now is to use arrays and pass them as an argument. While the use of fopen function was simply given to us by the instructor. – jungrylls Feb 02 '21 at 05:15
  • @David C. Rankin Thank you for your insight. But may I ask what a "field-width modifier" is ? I'd appreciate any resource you can throw me into. – jungrylls Feb 02 '21 at 05:17
  • The POSIX specification for [`scanf()`](https://pubs.opengroup.org/onlinepubs/9699919799/functions/scanf.html) and family includes field-width modifiers. For the most part, that's the same as standard C, but there are some carefully annotated extensions to standard C. – Jonathan Leffler Feb 02 '21 at 05:55

1 Answers1

1

I am not sure if this is what you are asking but, this is the function prototytpe of fopen(),

FILE *fopen(const char *pathname, const char *mode);

pathname is a pointer to char. You are not limited to hard-coding the pathname, you can provide a variable to it (assuming its of type char *).

So this:

FILE *infile = fopen(/*FileName as string value*/, "rb"); 

Becomes this:

FILE *infile = fopen(FileName, "rb"); /* Filename is the argument you provided by calling the function */
kaylum
  • 13,833
  • 2
  • 22
  • 31
alex01011
  • 1,670
  • 2
  • 5
  • 17
  • 1
    I'd go so far as to argue that you should never call `fopen()` with a character literal as the file name because you should want to report the file name if the `fopen()` fails, and you can't do that without repeating yourself if the first argument to `fopen()` is a literal. `if ((fp = fopen(filename, "rb") == NULL) err_syserr("failed to open file '%s' for reading\n", filename);` using an appropriate error reporting function that exits. – Jonathan Leffler Feb 02 '21 at 00:17
  • 1
    See the code available in my [SOQ](https://github.com/jleffler/soq) (Stack Overflow Questions) repository on GitHub as files `stderr.c` and `stderr.h` in the [src/libsoq](https://github.com/jleffler/soq/tree/master/src/libsoq) sub-directory for `err_syserr()` and friends. – Jonathan Leffler Feb 02 '21 at 00:17