reading input files
Apr 17, 2020 at 10:04pm UTC
Hello,
I have been using visual studio code with the code runner ext. on the mac.I cant seem to get the terminal to input a file. I have been trying to import a .txt file however each time I get the error message. I have looked around to see if there was another ext. I needed to download but havent had much luck
I dont think its a code problem but figured I would ask on here if any one has add this issue.
I am inputing: c:/users/#####/desktop/####.txt
I think I got the code right but here it is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
cout << "Welcome to File input" << endl;
cout << endl;
string fileName;
cout << "Please enter file name: " ;
getline(cin,fileName);
ifstream fin;
fin.open(fileName);
if (fin.is_open()){
int buffer;
while (fin >> buffer){
cout << buffer << endl; }
}
else {
cout << "ERROR opening file" << fileName << "!!!" << endl;
}
return 0;
}
Apr 17, 2020 at 10:55pm UTC
I am inputing: c:/users/#####/desktop/####.txt
Please, try to be a bit more accurate; for example: file content, actual code, actual terminal output.
For example:
DJL.txt:
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
main.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include <fstream>
#include <iostream>
#include <string>
int main()
{
std::cout << "Welcome to File input\n\n"
"Please enter file name: " ;
std::string file_name;
std::getline( std::cin, file_name );
std::ifstream fin( file_name );
if (!fin) {
std::cout << "ERROR opening file" << file_name << "!!!\n" ;
return 0;
}
for (int buffer; fin >> buffer; /**/ ) {
std::cout << buffer << '\n' ;
}
}
Output:
Welcome to File input
Please enter file name: C:\Users\#####\Desktop\DJL.txt
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Topic archived. No new replies allowed.