![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
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.... |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
Are you sure you entered a valid path?
|
|
|
|
|
|
#4 | |
|
PFO God In Training
![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 599
Rep Power: 4
![]() |
Quote:
cin.getline(filename,sizeof(filename)); |
|
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 4
![]() |
lol... we are dumb. I can't believe I didn't catch that
![]() |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Jun 2005
Posts: 86
Rep Power: 4
![]() |
or use the string class and getline like this:
string filename; getline(cin, filename); ifstream fin(filename.c_str()); |
|
|
|
|
|
#7 |
|
Expert Programmer
|
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 <geek@cliffordroche.com> Web Hosting: http://www.crd-hosting.com Consulting: http://www.crdev-consulting.com |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|