Hello guys!
I'm new at C++ (started yesterday) but I'm a fast learner and I think I got pretty far with this noob project! The only thing I need to do is to make C++ open my .txt file and the beta version of this application is done! By the way, I'm advanced in Java, so I think I'll get most of the directions you give me in C++.
Anyway enough chit-chat!
Here's my Data.cpp file:

#include <iostream>
#include <fstream>
using namespace std;

struct person
{
    string firstName;
	int age;
	int phoneNumber;
};

int main()
{	
    ofstream data; 
    person people;
	ifstream personExists("Person.txt", ios::out);
	
	if(!personExists)
	{
	    cout << "Persons name: <First name only>" << endl;
		cin >> people.firstName;
		cout << endl;
		cout << "Persons age: " << endl;
		cin >> people.age;
		cout << endl;
		cout << "Persons phone number: " << endl;
		cin >> people.phoneNumber;
		cout << endl;	
		cout << "Creating file: Persons.txt" << endl;
		data.open("Person.txt");
		data << "Name: " << people.firstName << endl;
		data << "Age: " << people.age << endl;
		data << "Phone number: " << people.phoneNumber;
		data.close();
	}
	else
	{
	    char answer;
	    cout << "You already got a data file, would you like to open it? <y = yes/n = no>" << endl;
		cin >> answer;
		cout << endl;
		
		if(answer == 'y' || answer == 'Y')
		{
		    data.open("Person.txt");
		}
		else if(answer == 'n' || answer == 'N')
		{
		    exit(0);
		}
		else
		{
		    cout << "Please answer my question!" << endl;
		}
	}

    system("pause");
	return 0;
}

I don't get any compilation errors, the only problem is that when you got the Person.txt file, and you chose the 'y' or 'Y' option nothing happens! It still won't open the. txt file..
So help will be appreticated A LOT! Thanks in advantage your friend: Benjamin Dahse ;P

What are you doing to tell the difference between "opens the file and then has nothing more to do to closes it and exits the program" and "fails to open the file"?

BTW, you may be interested in this shortly.

What are you doing to tell the difference between "opens the file and then has nothing more to do to closes it and exits the program" and "fails to open the file"?

Well.. It's just a beta version, and most of all I did this to test my knowledge in C++ :) And I ran out of ideas :P so I just chose to exit the program if the 'n' option takes place

Please guys?? Some help??

Your problem is that data is an ofstream which can only be written to and not read from. If you tried to read from it to test if it was open it wouldn't have worked but if you use the member function is_open() to test whether it is open it should work. You can declare data as an fstream in order to be able to both read and write to/from it.

Your problem is that data is an ofstream which can only be written to and not read from. If you tried to read from it to test if it was open it wouldn't have worked but if you use the member function is_open() to test whether is is open it should work. You can declare data as an fstream in order to be able to both read and write to/from it.

Oh thanks :) so you mean that ofstream means: outfilestream? and that ifstream means: infilestream? and that ofstream can only be written to, while ifstream can only be read from? But fstream is just filestream and can be both written to and readed from?

Yeah, that's it.

Yeah, that's it.

Okay, so I followed your adivce of changing 'data' to a fstream. But still it doesn't opens the file? + now it can't write Person.txt? :S

Sorry, I forgot to mention that if you use an fstream you have to declare the mode with flags if you're writing instead of reading. So, when you open it, you do data.open("Persons.txt", ios::out) for writing or data.open("Persons.txt") for reading since reading is the default mode.

Instead of using fstream for reading and writing, why don't you use ofstream and ifstream depending on the process and see how that goes?

Also you should post the new code

Instead of using fstream for reading and writing, why don't you use ofstream and ifstream depending on the process and see how that goes?

Also you should post the new code

Well if you want the class I got from following your advices, here it is:

#include <iostream>
#include <fstream>
using namespace std;

struct person
{
    string firstName;
	int age;
	int phoneNumber;
};

int main()
{	
    ofstream data; 
    person people;
	ifstream personExists("Person.txt");
	
	if(!personExists)
	{
	    cout << "Persons name: <First name only>" << endl;
		cin >> people.firstName;
		cout << endl;
		cout << "Persons age: " << endl;
		cin >> people.age;
		cout << endl;
		cout << "Persons phone number: " << endl;
		cin >> people.phoneNumber;
		cout << endl;	
		cout << "Creating file: Persons.txt" << endl;
		data.open("Person.txt");
		data << "Name: " << people.firstName << endl;
		data << "Age: " << people.age << endl;
		data << "Phone number: " << people.phoneNumber;
		data.close();
	}
	else
	{
	    char answer;
	    cout << "You already got a data file, would you like to open it? <y = yes/n = no>" << endl;
		cin >> answer;
		cout << endl;
		
		if(answer == 'y' || answer == 'Y')
		{
		    ifstream open("Person.txt");
		}
		else if(answer == 'n' || answer == 'N')
		{
		    exit(0);
		}
		else
		{
		    cout << "Please answer my question!" << endl;
		}
	}

    system("pause");
	return 0;
}

But as you see, I replaced: 'data.open("Person.txt");' with 'ifstream open("Person.txt");' but it still doesn't work?

It works for me. How are you testing whether or not it is open?
What I did was add:

if(answer == 'y' || answer == 'Y')
		{
		    ifstream open("Person.txt");
		    while(!open.eof()) {
		                       string line;
		                       getline(open, line);
		                       cout << line << endl;
		}

Hello guys!
I'm new at C++ (started yesterday) but I'm a fast learner and I think I got pretty far with this noob project! The only thing I need to do is to make C++ open my .txt file and the beta version of this application is done! By the way, I'm advanced in Java, so I think I'll get most of the directions you give me in C++.
Anyway enough chit-chat!
Here's my Data.cpp file:

#include <iostream>
#include <fstream>
using namespace std;

struct person
{
    string firstName;
	int age;
	int phoneNumber;
};

int main()
{	
    ofstream data; 
    person people;
	ifstream personExists("Person.txt", ios::out);
	// here you need to open the file with a    personExists.open command
	if(!personExists)
	{
	    cout << "Persons name: <First name only>" << endl;
		cin >> people.firstName;
		cout << endl;
		cout << "Persons age: " << endl;
		cin >> people.age;
		cout << endl;
		cout << "Persons phone number: " << endl;
		cin >> people.phoneNumber;
		cout << endl;	
		cout << "Creating file: Persons.txt" << endl;
		data.open("Person.txt");
		data << "Name: " << people.firstName << endl;
		data << "Age: " << people.age << endl;
		data << "Phone number: " << people.phoneNumber;
		data.close();
	}
	else
	{
	    char answer;
	    cout << "You already got a data file, would you like to open it? <y = yes/n = no>" << endl;
		cin >> answer;
		cout << endl;
		
		if(answer == 'y' || answer == 'Y')
		{
		    data.open("Person.txt");
		}
		else if(answer == 'n' || answer == 'N')
		{
		    exit(0);
		}
		else
		{
		    cout << "Please answer my question!" << endl;
		}
	}

    system("pause");
	return 0;
}

I don't get any compilation errors, the only problem is that when you got the Person.txt file, and you chose the 'y' or 'Y' option nothing happens! It still won't open the. txt file..
So help will be appreticated A LOT! Thanks in advantage your friend: Benjamin Dahse ;P

added a // line where you need to open the file after you get to your pause make sure you close the file.

added a // line where you need to open the file after you get to your pause make sure you close the file.

I'm not quite sure what you mean..? As I told you guys I'm a noob in C++. Well I tried like this:

#include <iostream>
#include <fstream>
using namespace std;

struct person
{
    string firstName;
	int age;
	int phoneNumber;
};

int main()
{	
    ofstream data; 
    person people;
	ifstream personExists("Person.txt");
	data.open("Person.txt");
	
	if(!personExists)
	{
	    cout << "Persons name: <First name only>" << endl;
		cin >> people.firstName;
		cout << endl;
		cout << "Persons age: " << endl;
		cin >> people.age;
		cout << endl;
		cout << "Persons phone number: " << endl;
		cin >> people.phoneNumber;
		cout << endl;	
		cout << "Creating file: Persons.txt" << endl;
		data.open("Person.txt");
		data << "Name: " << people.firstName << endl;
		data << "Age: " << people.age << endl;
		data << "Phone number: " << people.phoneNumber;
		data.close();
	}
	else
	{
	    char answer;
	    cout << "You already got a data file, would you like to open it? <y = yes/n = no>" << endl;
		cin >> answer;
		cout << endl;
		
        if(answer == 'y' || answer == 'Y')
		{
		    ifstream open("Person.txt");
		}
		else if(answer == 'n' || answer == 'N')
		{
		    exit(0);
		}
		else
		{
		    cout << "Please answer my question!" << endl;
		}
	}

    system("pause");
    data.close();
	return 0;
}

Is ^^ what you meant? (If it was it didn't worked)

added a // line where you need to open the file

The constructor of an ifstream, ofstream, or fstream calls the open command with the supplied file, so the open command doesn't need to be called.

In the condition

if(answer='y'||answer=='Y')
             data.open("Person.txt")

What is it that you are trying to do exactly?
I'm not sure but for the threat i can assume you want it to show the content of Person.txt, if i am right and you want it to show the content what are you doing to extract it, all you did with that instruction is to prepare the content to be worked with.

The constructor of an ifstream, ofstream, or fstream calls the open command with the supplied file, so the open command doesn't need to be called.

Okay, so what i got here should be right, or what?

#include <iostream>
#include <fstream>
using namespace std;

struct person
{
    string firstName;
	int age;
	int phoneNumber;
};

int main()
{	
    ofstream data; 
    person people;
	ifstream personExists("Person.txt");
	
	if(!personExists)
	{
	    cout << "Persons name: <First name only>" << endl;
		cin >> people.firstName;
		cout << endl;
		cout << "Persons age: " << endl;
		cin >> people.age;
		cout << endl;
		cout << "Persons phone number: " << endl;
		cin >> people.phoneNumber;
		cout << endl;	
		cout << "Creating file: Persons.txt" << endl;
		data.open("Person.txt");
		data << "Name: " << people.firstName << endl;
		data << "Age: " << people.age << endl;
		data << "Phone number: " << people.phoneNumber;
		data.close();
	}
	else
	{
	    char answer;
	    cout << "You already got a data file, would you like to open it? <y = yes/n = no>" << endl;
		cin >> answer;
		cout << endl;
		
        if(answer == 'y' || answer == 'Y')
		{
		    ifstream open("Person.txt");
		}
		else if(answer == 'n' || answer == 'N')
		{
		    exit(0);
		}
		else
		{
		    cout << "Please answer my question!" << endl;
		}
	}

    system("pause");
	return 0;
}

I don't see any errors. Why don't you try to compile it and see?

I too don't understand this part:

if(answer == 'y' || answer == 'Y')
		{
		    ifstream open("Person.txt");
		}

If you're trying to display the content of "Person.txt", it isn't done this way. Ifstream doesn't open the text file in a window , it opens it in the background. If you want to display the content of the .txt you should first declare a string, then open the .txt with ifstream, the using getline you store the content in the string and then output it.

For example:

string example;

ifstream file("Persons.txt");

getline(file, example);

cout<< example;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.