Good day everyone!

Hi everyone I'm new here, I'm using English but not so good. Hopefully I can share my few experience with all of you.

For the first post, I wanna ask about using file in c++ as an input output process.

Below is several code that I wrote recently:

1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>

using namespace std;

int main(){
    ofstream myfile("oct-24th-test.txt");
    if (myfile.is_open()){
        myfile << "This is a line.\n";
        myfile << "This is another line.\n";
        cout<<"adding data success";
    }
    else
        cout << "Unable to open file";
    myfile.close();
    
    cin.get();
    return 0;
}


2.
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 <fstream>

using namespace std;

int main(){
    ofstream myfile;
    myfile.open("oct-24th-test.txt");
    if (!myfile){
        cout << "Unable to open file";
        exit(1);
    }
    else{
        myfile << "This is a line.\n";
        myfile << "This is another line.\n";
        cout<<"adding data success";
    }
    myfile.close();
    
    cin.get();
    return 0;
}


those two generate:
adding data success


it means I managed to open the file (and (I think) of course wrote the file on "oct-24th-test.txt") but the reality is, I opened the "oct-24th-test.txt" file and didn't see the string that I meant to be written by the program.

I really confused with this problem, I seek for the articles in internet about using file in c++ as an input output process all day and follow the code they're using but still I've got no results in my "oct-24th-test.txt" file.

FYI: I'm using visual c++ 2010 express

Please anybody help me, I really appreciate your attention.
Last edited on
make sure that you are checking the right file ( ie: that you are looking in the program working directory )
You can also try this test:
19
20
21
22
23
24
25
26
27
28
29
// ...  all your code which writes to the file ...

ifstream check("oct-24th-test.txt");
if (!check) {
    cout << "Something weird happened";
    return 1;
}
string hopefully_it_will_read_something;
getline ( check, hopefully_it_will_read_something );
cout << hopefully_it_will_read_something; // it should print "This is a line."
//... 
@Bazzy:
woah..it works! just now I compiled your syntax and it does generate a
This is a line
output

Then my question is, why aren't the strings showed up in my "oct-24th-test.txt" file?

From your working syntax and your information about

"make sure that you are checking the right file ( ie: that you are looking in the program working directory )"

I checked my "oct-24th" project folder, previously I created my own "oct-24th-test.txt" file in
\Visual Studio 2010\Projects\oct-24th

but the truth is, from my previous 'ofstream syntax' (to create output file) the program wrote the "oct-24th-test.txt" in
\Visual Studio 2010\Projects\oct-24th\oct-24th

(So that's why I can't see "This is a line." and "This is another line." string in my "oct-24th-test.txt" file -- I mean, yes, please make sure that you are pointing or accessing the right file (including it's path))

So the problem has solved, and from now on I can continue doing my project

Thanks a lot Bazzy
Last edited on
Topic archived. No new replies allowed.