How to Open a File with a Variable Name in C?

  • Thread starter Thread starter Lojzek
  • Start date Start date
  • Tags Tags
    File Variable
Click For Summary

Discussion Overview

The discussion revolves around how to open a file in C using a variable name provided at runtime, specifically focusing on the use of the fopen function and its parameters.

Discussion Character

  • Technical explanation, Conceptual clarification, Debate/contested

Main Points Raised

  • One participant inquires about opening a file with a name that is not predefined, suggesting a misunderstanding of the fopen function's requirements.
  • Another participant explains that fopen can accept a pointer to a variable-length string, indicating that user input can be used to specify the filename.
  • A different participant asserts that the filename parameter of fopen does not need to be a string literal, emphasizing that it can be determined at runtime.
  • One participant shares a resolution to their issue, noting that they initially attempted to use a format string with fopen, which was incorrect.
  • Another participant points out the confusion between the usage of fopen and printf, highlighting the importance of understanding function parameters in the C standard library.

Areas of Agreement / Disagreement

Participants appear to have differing levels of understanding regarding the use of fopen, with some clarifying misconceptions while others express confidence in their interpretations. The discussion does not reach a consensus on the initial confusion about function parameters.

Contextual Notes

Some assumptions about the nature of strings in C and the specific requirements of the fopen function remain unaddressed, particularly regarding user input handling and string termination.

Lojzek
Messages
246
Reaction score
1
How to open a file with a name that is not defined in advance, but
is entered after the program is executed?
I am using fopen function, but it seems that it's argument must be
a fixed string, not a variable.
 
Technology news on Phys.org
fopen uses a pointer to a variable length string terminated with a zero, sometimes called a "C" string (as opposed to a fixed length string that includes size information, usually called a "P" (Pascal) string). You just need to input a string from the user. You can also use an array, since the array name is treated similar to a pointer in C.
 
I'm pretty sure that the filename parameter doesn't have to be a string literal, so you can get the filename at run time. What the const char * filename parameter means is that fopen won't change the string that's passed to it.
 
It works now, thanks. Previously I was trying with fopen("%s",stringname,"r") instead of
just fopen(stringname,"r").
 
It looks like you confused printf with fopen, at least as far as the format string in printf is concerned. When you use standard library functions, make sure you understand what the parameters are used for, and that you use the right number of them.
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
65
Views
5K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 41 ·
2
Replies
41
Views
5K