Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,466 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,055 people online right now. Registration is fast and FREE... Join Now!




Reading from a text file

 
Reply to this topicStart new topic

Reading from a text file, Missing Something...

ddollar
5 Feb, 2008 - 06:06 PM
Post #1

New D.I.C Head
*

Joined: 16 Jan, 2008
Posts: 38



Thanked: 1 times
My Contributions
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.

CODE

#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;
}

User is offlineProfile CardPM
+Quote Post

Jingle
RE: Reading From A Text File
5 Feb, 2008 - 06:29 PM
Post #2

D.I.C Regular
***

Joined: 20 Oct, 2007
Posts: 250


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

This post has been edited by Jingle: 5 Feb, 2008 - 06:30 PM
User is offlineProfile CardPM
+Quote Post

ddollar
RE: Reading From A Text File
5 Feb, 2008 - 06:34 PM
Post #3

New D.I.C Head
*

Joined: 16 Jan, 2008
Posts: 38



Thanked: 1 times
My Contributions

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?


QUOTE(Jingle @ 5 Feb, 2008 - 07:29 PM) *

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


User is offlineProfile CardPM
+Quote Post

ddollar
RE: Reading From A Text File
5 Feb, 2008 - 06:44 PM
Post #4

New D.I.C Head
*

Joined: 16 Jan, 2008
Posts: 38



Thanked: 1 times
My Contributions

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

Thank you.

QUOTE(Jingle @ 5 Feb, 2008 - 07:29 PM) *

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


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 02:12PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month