Replacing stdin within a piece of code
I have a piece of code that uses stdin. When I run the program from command-line I pass it the location of a wav file i.e. /Users/username/Desktop/music.wav.
Th开发者_如何学Pythone code is written only in C. The stdin variable runs throughout 2 functions.
How would I replace the stdin within the code with the input of the file directory and location?
In other words, how do I hard code '/Users/username/Desktop/music.wav' into two different C functions.
I think you are looking for freopen.
If I understand correctly, you read the filename from argv[1]
and call freopen():
freopen(argv[1], "r", stdin);
Not that I wish to encourage hardwiring of course ....
It sounds like you wish to define the file location & reference it from multiple locations ?
You could #define the path in a header & then include that in multiple files.
E.g.
header.h
#define FILE_LOCATION "/Users/username/Desktop/music.wav"
myProgram.c
#include "header.h"
void functionA() {
fopen(FILE_LOCATION, ...);
/*
* etc ..
*/
}
void functionB() {
printf("The file is %s\n", FILE_LOCATION);
/*
* etc ..
*/
}
myOtherProgram.c
#include "header.h"
void someOtherFunction() {
doSomethingWith(FILE_LOCATION);
}
void doSomethingWith(char *fileLoc) {
}
精彩评论