2 Replies - 209 Views - Last Post: 20 February 2011 - 07:46 PM Rate Topic: -----

#1 Ajgilzean  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 27-July 10

Problem opening file stream with a text document

Posted 20 February 2011 - 06:58 PM

I'm new to file streams so here is the problem. I need to open an existing text file that I can read and write data to and from. I have the following code (it's not the entire program), but it always seems to display the "File not found" error message. I am using Visual Studio 2010 and Windows 7 if that helps.

int main()
{
	string filename;

	// Input section
	cout << "Please enter the name of a file: ";
	getline(cin,filename);

	// Declare and open the file
	fstream fin;
	fin.open( filename.c_str(), ios::in | ios::out );

	if( !fin.is_open() )
	{
		cout << "File not found." << endl;
		cout << endl << "Press any key to continue . . . ";
		cin.get();
        return 1;    
    }

    ...
}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 21 February 2011 - 06:39 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem opening file stream with a text document

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Problem opening file stream with a text document

Posted 20 February 2011 - 07:40 PM

Sorry to give you such an annoying answer, but it simply means that the file was not found. Either it doesn't exist, or it doesn't exist in the directory in which the program is running. Otherwise, it would not be giving you your error message.

Try entering a full pathname.
Was This Post Helpful? 0
  • +
  • -

#3 Ajgilzean  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 27-July 10

Re: Problem opening file stream with a text document

Posted 20 February 2011 - 07:46 PM

View Postr.stiltskin, on 20 February 2011 - 07:40 PM, said:

Sorry to give you such an annoying answer, but it simply means that the file was not found. Either it doesn't exist, or it doesn't exist in the directory in which the program is running. Otherwise, it would not be giving you your error message.

Try entering a full pathname.

Actually I figure it out.. rookie mistake. I named the file "thefile.txt" and Windows appended .txt to the end of that. So the real name of the file was "thefile.txt.txt".

This post has been edited by Ajgilzean: 20 February 2011 - 08:07 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1