C++ Can't open file for input

Discussion in 'General Programming Support' started by YourFace, Apr 22, 2011.

  1. YourFace

    YourFace <span style="color:#9C9C9C;"><strong>Runner Up - T

    Ratings:
    +90 / 0 / -0
    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?
     
  2. seph ir oth

    seph ir oth Mod'n Dat News Jon Staff Member

    Ratings:
    +261 / 2 / -0
    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();
     
  3. YourFace

    YourFace <span style="color:#9C9C9C;"><strong>Runner Up - T

    Ratings:
    +90 / 0 / -0
    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?
     
  4. Lyerae

    Lyerae I keep popping up on this site from time to time.

    Ratings:
    +105 / 0 / -0
    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:

    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).
     
    • Like Like x 1
  5. YourFace

    YourFace <span style="color:#9C9C9C;"><strong>Runner Up - T

    Ratings:
    +90 / 0 / -0
    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!
     

Share This Page