Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 15th, 2005, 4:42 PM   #1
Cipher
Hobbyist Programmer
 
Cipher's Avatar
 
Join Date: Feb 2005
Location: /home/cipher
Posts: 123
Rep Power: 4 Cipher is on a distinguished road
Send a message via AIM to Cipher Send a message via MSN to Cipher
Using Fstream to read files

Ok I was reading "Sams: Teach yourself C++ in 21 Days" blah blah blah and I came across the section about using streams to read files. Soon after, I coded a CONSOLE(MS-DOS) Program to read a text file that I had typed. Here was the code in the book for reading files:


  #include<iostream>
  #include<fstream>
  using namespace std;
  int main()
  {
  	char filename[80];
  	cout << "enter filename where txt file is kept.";
  	cin >> filename:
  	ifstream fin(filename);
  	cout << "File Contents:  "
  	char ch;
  	while (fin.get(ch)) /*Problem begins here..../*
  		cout << ch;
  	cout << "\n END OF FILE REACHED\n";
  	fin.close();
  	return 0;
  }

I enter the file name. Which would be "C:\Documents and settings\Cipher\My Documents\My txt\Weapons List.txt" and
then it justs displays a blank line and "END OF FILE REACHED" then closes when I press enter? So I suppose it's not reading the file contents, did I just miss something such as a syntax error or do I maybe have to specify the text file itself?

Any help would be apperciated.
__________________
And there was much rejoicing... Yay....
Cipher is offline   Reply With Quote
Old Jun 15th, 2005, 5:07 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
One possibility is that the file didn't open for any number of reasons. Actually, that's why they provide tests for that eventuality. Presuming that everything is going to work (including user input always being valid) is a serious mistake often made by beginners. It's something to get over as rapidly as you can if professional coding is your goal.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 15th, 2005, 5:09 PM   #3
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
Are you sure you entered a valid path?
uman is offline   Reply With Quote
Old Jun 15th, 2005, 5:19 PM   #4
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 599
Rep Power: 4 Ancient Dragon is on a distinguished road
Quote:
Originally Posted by Cipher
Which would be "C:\Documents and settings\Cipher\My Documents\My txt\Weapons List.txt"
That string contains one or more spaces. Therefore you can't use cin's >> operator. use getline()
cin.getline(filename,sizeof(filename));
Ancient Dragon is offline   Reply With Quote
Old Jun 15th, 2005, 5:58 PM   #5
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 4 uman is on a distinguished road
lol... we are dumb. I can't believe I didn't catch that
uman is offline   Reply With Quote
Old Jun 15th, 2005, 6:01 PM   #6
Scorpions4ever
Programmer
 
Join Date: Jun 2005
Posts: 86
Rep Power: 4 Scorpions4ever is on a distinguished road
or use the string class and getline like this:
string filename;
getline(cin, filename);
ifstream fin(filename.c_str());
Scorpions4ever is offline   Reply With Quote
Old Jun 15th, 2005, 9:17 PM   #7
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
You could have caught that bug by using your IDE's debugging and analysis tools would have saved you some time in awaiting an answer.
__________________
Clifford Matthew Roche &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Jun 15th, 2005, 9:37 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
As a matter of fact, virtually all your code will be rife with information. In addition to the values in many of your variables, which you may inspect with a debugger or with something like an output statement, there are function returns and status queries which may be (and usually SHOULD be) examined. As I've said many times before, 99 point umpty-ump percent of the functions you use do not promise to deliver the goods. They promise to deliver the goods OR TELL YOU THAT THEY DID NOT. Be sure and ask them.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:55 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC