Shweta
Shweta

Reputation: 5446

variable as a file name

Is it possible to use a variable as a filename in function fopen()? Can someone help with some example?

Upvotes: 2

Views: 8257

Answers (1)

Jeff Mercado
Jeff Mercado

Reputation: 134841

You just need a C string to pass into fopen(). Just declare the string char * (I'd recommend using const char *) variable, set it and pass into the function.

const char *filename = "my/path/to/the/file";
FILE *file = fopen(filename, "r");

Upvotes: 5

Related Questions