C++ Can't open file for input

YourFace

<span style="color:#9C9C9C;"><strong>Runner Up - T
Reaction score
91
Code:
#include <iostream>
#include <fstream>
using namespace std;


int main(void)
{
	fstream File;
	char FirstName[256];
	char LastName[256];
	int Age;
	int IsMarried;
	// should open file for input but wtf

	File.open("Data.txt", ios::in);

	if (File.is_open())
	{
		File >> FirstName >> LastName >> Age>>IsMarried;
		cout <<"Your name is " << FirstName << " " << LastName;
		cout <<" and you are " << Age << " years old" << endl;
		if (IsMarried == 1)
		{
			cout << "You're married, make sure not to make lots of kids" << endl;
		}
		else if (IsMarried == 0)
		{
			cout << "Don't worry, there's like 20 chicks that want to go out with you in class" << endl;
		}
	}
	else 
	{
		cout << "Couldn't open it :(" << endl;
	}


		File.close();
		return 0;
	}

Whenever I run this code, I always get couldn't open it :(. I have a file called Data.txt but the program doesn't take the input from it and display it on the screen like it's suppose to!

Does anyone know how to fix this?
 

seph ir oth

Mod'n Dat News Jon
Reaction score
262
You declare an ifstream and not use it?

Try something like this:

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


int main(void)
{
	ifstream File ("Data.txt");
	char FirstName[256];
	char LastName[256];
	int Age;
	int IsMarried;
	// should open file for input but wtf

	if (File.is_open())
	{
		File >> FirstName >> LastName >> Age>>IsMarried;
		cout <<"Your name is " << FirstName << " " << LastName;
		cout <<" and you are " << Age << " years old" << endl;
		if (IsMarried == 1)
		{
			cout << "You're married, make sure not to make lots of kids" << endl;
		}
		else if (IsMarried == 0)
		{
			cout << "Don't worry, there's like 20 chicks that want to go out with you in class" << endl;
		}
	}
	else 
	{
		cout << "Couldn't open it :(" << endl;
	}


		File.close();
		return 0;
	}

And if that doesn't work you should read the file first and, outside the if(is_open()) statement, do the variable setting. Something like this inside the is_open() check would work:

Code:
while(myfile.good())
    {
      getline(myfile,line);
      lineList.push(line);
    }
    myfile.close();
 

YourFace

<span style="color:#9C9C9C;"><strong>Runner Up - T
Reaction score
91
You declare an ifstream and not use it?

Try something like this:

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


int main(void)
{
	ifstream File ("Data.txt");
	char FirstName[256];
	char LastName[256];
	int Age;
	int IsMarried;
	// should open file for input but wtf

	if (File.is_open())
	{
		File >> FirstName >> LastName >> Age>>IsMarried;
		cout <<"Your name is " << FirstName << " " << LastName;
		cout <<" and you are " << Age << " years old" << endl;
		if (IsMarried == 1)
		{
			cout << "You're married, make sure not to make lots of kids" << endl;
		}
		else if (IsMarried == 0)
		{
			cout << "Don't worry, there's like 20 chicks that want to go out with you in class" << endl;
		}
	}
	else 
	{
		cout << "Couldn't open it :(" << endl;
	}


		File.close();
		return 0;
	}

And if that doesn't work you should read the file first and, outside the if(is_open()) statement, do the variable setting. Something like this inside the is_open() check would work:

Code:
while(myfile.good())
    {
      getline(myfile,line);
      lineList.push(line);
    }
    myfile.close();
it didn't work D:! Btw, I want the program to open the file, so how does the is_open function work if the file isn't open?
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
I compiled and ran it (Code::Blocks, using the MinGW compiler), and it runs fine.

The output is "Your name is This is and you are 4273606 years old", with the contents of Data.txt being:

This is some random text that has no meaningful purpose in life.

Try using an absolute path (C:\Documents and Settings\Username\Desktop\Data.txt, for example, or whatever it is (assuming Windows)), or passing the filename via command-line argument (and make sure the file exists, and the name is spelled exactly).
 

YourFace

<span style="color:#9C9C9C;"><strong>Runner Up - T
Reaction score
91
I found out the problem! It was my data.txt file which was located on my desktop. The file had to be in my project with my source files!
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top