text File reading

Started by
6 comments, last by Tooko 20 years, 10 months ago
I''ve tried numerous methods to try and get this working, but it just doesn''t. I want to read text files, and convert the result into a LPSTR or something. I tried making a 4byte text file that says "Yay!" and load that up with a function I made. (that got changed and overwritten throughout time) and then write
if(lpString == "Yay!") //Do Something 
But alas, it doesn''t. Does anyone have anyknowledge or links for me? I don''t have a signature, sorry.
the future is just like the past, just later. - TANSTAAFL
Advertisement
quote:Original post by Tooko
if(lpString == "Yay!") //Do Something  



You can''t compare strings(character arrays) this way. You have to use strcmp()
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
Try using string:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
...

std::ifstream inFile("in_file.txt");
if (inFile.fail())
{
// handle error
}

std::string word;
while (!inFile.eof())
{
inFile >> word;

if (word == "Yay!")
std::cout << "Hmmm, I see that you are happy, Sir.\n";

...
}

...

inFile.close();

...

return 0;
}


[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
Ooh, I''m happier... , but I''ll give it a go. I can''t right now, but maybe in the next hour or so. Or later. Anyway, thanks for the help. MSDN wasn''t that helpful...

I don''t have a signature, sorry.
the future is just like the past, just later. - TANSTAAFL
quote:Original post by Tooko
Ooh, I''m happier... , but I''ll give it a go. I can''t right now, but maybe in the next hour or so. Or later. Anyway, thanks for the help. MSDN wasn''t that helpful...

I don''t have a signature, sorry.


Of course MSDN wouldn''t help for this! If you use the strcmp, you would need a libC reference, if you do it the string way, you would need the STL programmers manual.
strcmp() is in the MSDN. you would write it like this
if(!strcmp(lpString, "Yay!"
{
//lpString did equal Yay!
}
else
{
//lpString did not equal Yay!
}
strcmp() is in the MSDN. you would write it like this
if(!strcmp(lpString, "Yay!")
{
//lpString did equal Yay!
}
else
{
//lpString did not equal Yay!
}
I sense a double post...

I don''t have a signature, sorry.
the future is just like the past, just later. - TANSTAAFL

This topic is closed to new replies.

Advertisement