Can anyone tell me what does exactly every line of the next program?
Thank you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// T3_P1.cpp : Defines the entry point for the console application.
#include<iostream>
#include<fstream>
usingnamespace std;
ofstream f;
class Number {...}
int main()
{
f.open ("text.txt", ios::app);
Number N;
f.close();
return 0;
}
1: comment
2: blank
3: include <iostream> header: http://en.cppreference.com/w/cpp/header/iostream
4: include <fstream> header: http://en.cppreference.com/w/cpp/header/fstream
5: bad practice, don't do this
6: blank
7: global variables are bad practice, move this into main - it declares and defines an output file stream but does not open any file
8: blank
9: invalid code
10: blank
11: declare main function
12: define main function
13: open the file now instead of when the stream was first constructed, and append to it but without being allowed to read from or write to it
14: we have no way to know what this will do
15: close the file after doing nothing to it (since we could not read from or write to it)
16: optional return statement in main
17: end definition of main
Prior to C++11 you have to explicitly specify std::ios::out with any other flags if you don't let the compiler substitute the default argument for you.
Yes, I know this. In that class are some instructions, but I understood what's there so I replace these instructions with "...'
I'm interested in lines 7, 13 and 15. What do these instructions?
Line 7 creates a file stream, although it does this outside of main when it belongs inside of it.
The file stream is used on line 13 to open the file in append mode, which means if we wrote anything to the file, it would be added to whatever is already there. Yet your code doesn't do anything to the file.
I'm unsure about line 15. We have no way of knowing from this code alone. But line 16 closes the file and line 17 terminates the program. For a basic idea of how to read from a file or write to it, search the forum about fstream, ofstream, and ifstream. Their usage is all similar to what you see above, and becomes intuitive after a while. Sometimes, you may hit snags where your file structure is causing your program to behave incorrectly, and you don't want to modify your file, but instead modify your program. The forums can be very handy for that. Here's a basic output operation next to a basic input operation as an example.
#include <iostream>
#include <string>
#include <fstream>
int main()
{
std::ifstream input;//create file stream
std::string data;//string used to store line
input.open("test.txt");//open file
if (!input)//error message if file not found
{
std::cout<<"ERROR READING FILE!";
return 1;//terminate with error
}
else
{
getline(input,data);//grab line from file and put it in data
std::cout<<data;//output data read from file
input.close();//close file stream
return 0;//terminate with no errors
}
}
#include <iostream>
#include <string>
#include <fstream>
int main()
{
std::ofstream output;//create file stream
std::string data="Data Sample II";//string used to store data
output.open("test.txt");//open file
if (!output)//error message if file not found
{
std::cout<<"ERROR WRITING TO FILE!";
return 1;//terminate with error
}
else
{
output<<data;//grab line from data and put it in file
std::cout<<"Data written to file!";
output.close();//close file stream
return 0;//terminate with no errors
}
}
Assuming you have a text file named "test.txt" in your working directory, the program on the left will read in the top line from the file and output it. If you then run the program on the right, it will replace that data with "Data Sample II".