Newb question
Hey guys, I''m sure anyone can spot what is wrong with the following code, but I can''t figure what I''m doing wrong. I am trying just basic read from file commands that will break words from a file by wordspaces. But it doesnt read the file at all as if it wasn''t there. I am using Dev C++ compiler, and I have the file that I''m reading from in the same directory as this program. Don''t know if I am doing this write... please help
//
//whitespace.cpp
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
vector words;
ifstream in("getwords.cpp");
string word;
while(in >> word)
words.push_back(word);
for(int i = 0; i < words.size(); i++)
cout << words << endl;
}
Why are you using the result of the >> operator as the condition for your while loop? It returns a copy of the file stream not a boolean value.
No, it will return a reference to in.
Maybe
while(in.good())
. . .
Maybe
while(in.good())
. . .
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
smart_idiot: no, that syntax is valid. The implicit cast to void* causes it to return false if failbit or badbit is set. Read your STL documentation.
Shrap: I suggest you add a bit more error-checking to your code. after declaring in, print a message and exit if the stream is not in a "good" state.
But... but that''s what HITLER would say!!
Shrap: I suggest you add a bit more error-checking to your code. after declaring in, print a message and exit if the stream is not in a "good" state.
But... but that''s what HITLER would say!!
Your code for the most part is fine.. I suspect that the current working directory is wrong... Call:
#include <windows.h>
char szBuffer[256];
GetCurrentDirectory(256,szBuffer);
cout<
to figure out where you are and use
SetCurrentDirectory("directory of the file") to specify where you want to load from if you don''t use a path.
Compilers use different directories as the current directory compared to when you just double click on the exe.
www.brightbug.com
#include <windows.h>
char szBuffer[256];
GetCurrentDirectory(256,szBuffer);
cout<
to figure out where you are and use
SetCurrentDirectory("directory of the file") to specify where you want to load from if you don''t use a path.
Compilers use different directories as the current directory compared to when you just double click on the exe.
www.brightbug.com
www.brightbug.com
quote: Original post by Sneftel
Read your STL documentation.
Okay, you're right.
quote: smart_idiot's stl documentation (basic_ios)
bool fail() const;
Returns true if failbit or badbit is set in rdstate().
bool operator!() const;
Returns fail() ? 1 : 0;
operator void*() const;
Returns fail() ? 0 : 1;
I'm not sure why it is converted to a void* instead of a bool, but the effect is the same I guess. . .
[edited by - smart_idiot on March 4, 2003 2:09:08 PM]
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
quote: Original post by Sneftel
smart_idiot: no, that syntax is valid. The implicit cast to void* causes it to return false if failbit or badbit is set. Read your STL documentation.
It''s part of iostreams, not stl.
The gcc documentation just says it returns *this, and that on an error it sets a flag and rethrows any exceptions that may have occurred. The dinkumware documentation says pretty much the same thing. I couldn''t find any mention of casts to void* in either one. Where are you getting this from?
quote: Original post by Dobbs
The gcc documentation just says it returns *this, and that on an error it sets a flag and rethrows any exceptions that may have occurred. The dinkumware documentation says pretty much the same thing. I couldn't find any mention of casts to void* in either one. Where are you getting this from?
Read smart_idiot's last post; it's the same docs I looked at, AFAICT. For more info, I suggest http://www.cplusplus.com .
I really have no idea why they went with void* instead of bool; I assume there was some weirdness in the standards process early on that made it a good idea, and then nobody bothered changing it.
But... but that's what HITLER would say!!
[edited by - sneftel on March 4, 2003 5:56:24 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement