Reading from a text file

Missing Something...

Page 1 of 1

3 Replies - 643 Views - Last Post: 05 February 2008 - 07:44 PM Rate Topic: -----

#1 ddollar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 16-January 08

Reading from a text file

Post icon  Posted 05 February 2008 - 07:06 PM

I'm trying to read from a text file but nothing is displayed. Anyone see whats wrong? The file is on the root.

Thanks for any advise.

#include <iostream>
#include <conio.h>
#include <iomanip>
#include <fstream>  // dont' forget to include fstream
using namespace std;

int main()
{
		char fName;
		// create object and open file
		ifstream readFile;
		readFile.open("c:\contents.txt");

		// test open operation for success
		if (readFile.fail())
		{
				cout << "The fail member function returned true when ";
				cout << "an attempt was\nmade to open c:\\grades.txt. ";
				cout << "The operation failed. The\nfile may not exist. ";
				cout << "Or, like me, Vista put a fight while\njust ";
				cout << "just trying to store the file on the root ";
				cout << "and now you\nare wondering if Vista is ";
				cout << "somehow interfering with the open\noperation. ";
				cout << endl;
		}
		else
		{
		cout << "yep, worked.";
		readFile >> fName;
		cout << fName;

		// rest of program here

		// don't forget to close the file
		readFile.close();
		}

		cout << endl;
		cout << "Press any key to quit...";
		getch();
		return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Reading from a text file

#2 Jingle  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 317
  • Joined: 20-October 07

Re: Reading from a text file

Posted 05 February 2008 - 07:29 PM

you sure that c:\grades.txt exsists? and that you are looking in the right place?

This post has been edited by Jingle: 05 February 2008 - 07:30 PM

Was This Post Helpful? 0
  • +
  • -

#3 ddollar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 16-January 08

Re: Reading from a text file

Posted 05 February 2008 - 07:34 PM

Its there. I even copied it to my docs folder and edited the path. Still nothing.

The part that says 'yep, worked' is displayed, not the read from the file. If I edit the file name the error is displayed, but with the file name correct, no display of read. Am I missing something?


View PostJingle, on 5 Feb, 2008 - 07:29 PM, said:

you sure that c:\grades.txt exsists? and that you are looking in the right place?

Was This Post Helpful? 0
  • +
  • -

#4 ddollar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 16-January 08

Re: Reading from a text file

Posted 05 February 2008 - 07:44 PM

Got it. Need a pair of backslashes: "c:\\grades.txt". Thanks, thought I already checked that.

Thank you.

View PostJingle, on 5 Feb, 2008 - 07:29 PM, said:

you sure that c:\grades.txt exsists? and that you are looking in the right place?

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1