Reputation: 245
I have to program to read some values from a Input text file.
int main(){
FILE *pf;
int i;
int j;
pf = fopen("input.txt" , "r");
fscanf(pf ,"%d , %d" , &i ,&j );
printf("%d ,%d\n" , i ,j);
fclose(pf);
}
and Input.txt has some values. Can anyone suggest me a way to get input.txt after running the program.
For example :
Open a terminal
compile the code
Run the code
---Here it should ask for the file name---
Upvotes: 3
Views: 2012
Reputation:
Example:
#include <fstream>
#include <iostream>
int main()
{
std::string filename;
cin >> filename;
ifstream inFile;
inFile.open(filename.append(".txt");
int a;
while (inFile)
{
inFile >> a;
cout << a;
}
return 0;
}
Upvotes: 2
Reputation: 88155
Command line arguments:
int main(int argc, char *argv[]) {
std::cout << argv[1] << '\n';
}
Or stdin:
int main() {
std::string filename;
std::cout << "enter file name: ";
std::cin >> filename;
std::cout << filename << '\n';
}
Upvotes: 1