Write a program named “ FileOperationPractice.cpp .” 4. This program should have
ID: 3756579 • Letter: W
Question
Write a program named “FileOperationPractice.cpp.”
4. This program should have a main function.
5. In your program
Read a text file that contains integers, print out each integer to "cout" and stop when you hit the end of file.
Question
1. Do you encounter any problems? Does it matter what OS you're developing your code on? See if transferring these files from, say, a Windows machine to a Linux machine alters them by checking their ASCII contents using the Linux od -a command (Examples (Links to an external site.) to use the od command).
------------------- textfile1 -------------
32
71
95
13
65
47
94
81
86
23
2
1
57
65
16
24
11
54
75
3
97
19
100
66
14
79
86
41
31
53
28
14
40
33
31
89
94
45
14
21
78
12
20
87
60
68
75
64
43
83
38
81
70
35
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. If you are satisfied with the solution, please rate the answer. Thanks
Though different Operating Systems have different text encoding methods, the below approach can print the contents of a file containing integer values without any issues.
//Code
#include<iostream>
#include<fstream>
using namespace std;
int main(){
string filename;
cout<<"Enter file name: ";
//getting input file name
cin>>filename;
//opening file for reading
ifstream infile(filename.c_str());
//checking if file opened properly
if(!infile){
cout<<"File not found or cannot be opened!"<<endl;
return 0;
}
int number;
//looping until the end of file, reading integers one by one
while(infile>>number){
//one number read successfully, printing it
cout<<number<<endl;
}
return 0;
}
/*OUTPUT*/
Enter file name: textfile1.txt
32
71
95
13
65
47
94
81
86
23
2
1
57
65
16
24
11
54
75
3
97
19
100
66
14
79
86
41
31
53
28
14
40
33
31
89
94
45
14
21
78
12
20
87
60
68
75
64
43
83
38
81
70
35
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.