GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Dec-2006, 13:02
shatred shatred is offline
New Member
 
Join Date: Dec 2006
Posts: 6
shatred is on a distinguished road

coding wont read the file


hi n tq dave ,...ive resolved the problem wif the header...

one question
my program should read string from file and the file named example.txt in folder test in bcc55 directory in c.
but why it doesnt read the file n im sure the coding was right....
hope u guys can help and the example.txt file only containe word hi...
the result show unable to open file

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("c:\bcc55\test\example.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

in what way must i keep the output file?
Last edited by LuciWiz : 03-Dec-2006 at 06:31. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 02-Dec-2006, 13:42
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: coding wont read the file


Works for me. Are you sure the file exists where you are specifying?

Also, please use proper English. Many people here don't speak short-talk or whatever that is.

And Guidelines #1 shows the proper use of code tags.
__________________

Age is unimportant -- except in cheese
  #3  
Old 02-Dec-2006, 14:12
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,712
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: coding wont read the file


Quote:
Originally Posted by shatred

my program should read string from file and the file named example.txt in folder test in bcc55 directory in c.
but why it doesnt read the file n im sure the coding was right....

Well, it is not right (and the proof is that it doesn't work the way you expected). The file name inside the string literal can be done two ways:

CPP / C++ / C Code:
  ifstream myfile ("c:\\bcc55\\test\\example.txt");

Or
CPP / C++ / C Code:
  ifstream myfile ("c:/bcc55/test/example.txt");


Inside a string, something like "\t" is a tab character. "\b" is a backspace, etc. If you want a backslash in a string literal, then it is "\\".

However, as my second example shows, you can actually use forward slashes (Yes! Even in Windows XP! It's documented by Microsoft!).

If you want to see what Windows thought your file name was, you could try:

CPP / C++ / C Code:
    char *inname = "c:\bcc55\test\example.txt";
    cout << "inname: <" << inname << ">" << endl;
    ifstream myfile(inname);
    if (myfile.is_open()) {
        while (!myfile.eof()) {
            getline(myfile, line);
            cout << line << endl;
        }
        myfile.close();
    }

    else {
        cout << "Unable to open file <" << inname << "> for reading" <<
            endl;
    }
My output:

Code:
inname: <ccc55 estexample.txt> Unable to open file <ccc55 estexample.txt> for reading


Regards,

Dave
  #4  
Old 02-Dec-2006, 21:02
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: coding wont read the file


Ahh, missed that since I edited the file name.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogHalfway done! by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
php.ini file not being read ian111 Apache Web Server Forum 1 29-Jun-2005 11:35
How to detect end of file with read() function call? nkhambal C Programming Language 6 12-Oct-2004 01:08
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:32.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.