Explanations

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>
using namespace std;

ofstream f;

class Number {...}

int main()
{
    f.open ("text.txt", ios::app);
    Number N;
    f.close();
    return 0;
}
Last edited on
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
5: Put all the functions, variables, declared in the namespace std into the global namespace
now they can be referred as foo() besides of std::foo()

13: iirc http://www.cplusplus.com/reference/fstream/fstream/open/
c++11 it opens the file in output mode, appending to the existing content
c++98 the open operation fails
Last edited on
Thanks ne555, I didn't know that was fixed in C++11
@ LB @ ne555: what are you talking about?

The OP used std::ofstream not std::fstream, and so the flag std::ios_base::out is always implied.
http://www.cplusplus.com/reference/fstream/ofstream/open/
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.
@ LB: not to argue but it works fine for me, can you please give a test program that fails?

1
2
3
4
5
6
7
8
9
10
#include <fstream>
#include <ios>

int main()
{
    std::ofstream output;

    output.open("output_fail.test.txt", std::ios_base::app);
    output << "Hello, World!\n";
}


I'm using nuwen's MinGW GCC 4.8.1 32-bit and compiling with:
g++ -Wall -Wextra -pedantic -std=c++98
@LB: read the link
`out is always set for ofstream objects (even if explicitly not set in argument mode).'

sorry about the confusion.
Ah, I was relying on ne555 alone; I didn't have any access to a compiler on my phone.
If I move "ofstream f" into main, it gives me compilation errors.

so "ofstream f" just declares a file stream?
@ AnaMariaB: the code is not correct, because of line 9:

1
2
3
class Number {...}
// "..." invalid code
// missing semicolon ; at the end 


If you want to write an empty class Number:

1
2
3
class Number
{
}; // semicolon 

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?
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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".
Last edited on
Topic archived. No new replies allowed.