A file with variable name in C

In summary, the conversation focuses on how to open a file with a variable name at runtime using the fopen function. It is clarified that fopen requires a pointer to a variable length string, not a fixed string. It is also mentioned that the filename parameter does not have to be a string literal and can be obtained at runtime. Finally, it is emphasized to use the correct parameters when using standard library functions.
  • #1
Lojzek
249
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
  • #2
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.
 
  • #3
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.
 
  • #4
It works now, thanks. Previously I was trying with fopen("%s",stringname,"r") instead of
just fopen(stringname,"r").
 
  • #5
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.
 

1. What is a variable in C?

A variable in C is a named location in the computer's memory that can store a value. It is used to represent data and can be assigned different values throughout the program.

2. How do you declare a variable in C?

A variable in C is declared by specifying its data type and name. For example, to declare an integer variable named "num", you would write int num; This tells the compiler to allocate memory for an integer variable named "num".

3. Can the name of a variable in C contain spaces?

No, the name of a variable in C cannot contain spaces. It can only contain letters, numbers, and underscores, and must begin with a letter or underscore.

4. What is the scope of a variable in C?

The scope of a variable in C refers to the part of the program where the variable can be accessed. Variables declared outside of a function have global scope and can be accessed by any function in the program. Variables declared inside a function have local scope and can only be accessed within that function.

5. Can you change the data type of a variable in C?

Yes, you can change the data type of a variable in C by using type casting. This involves converting the value of a variable from one data type to another. However, this should be used with caution as it may result in loss of data or unexpected behavior.

Similar threads

  • Programming and Computer Science
Replies
1
Views
344
  • Programming and Computer Science
Replies
2
Views
350
  • Programming and Computer Science
Replies
1
Views
501
  • Programming and Computer Science
Replies
4
Views
748
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
Replies
9
Views
566
  • Programming and Computer Science
2
Replies
41
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
602
  • Programming and Computer Science
Replies
2
Views
633
Back
Top