Jump to content
  • Advertisement
Sign in to follow this  
Powell

File.ReadAllText?

This topic is 3451 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am running Visual Studio 2005, programming language C++ I am using the basic framework found here: http://wiki.gamedev.net/index.php/C:Simple_Engine_Framework_Using_SDL I have a text file that I wish to display on the screen, but I get errors.
#include <stdlib.h>
#include <stdio.h>
#include "iostream"
#include "Engine.h"

using namespace std;

...

int main(int argc, char* argv[])
{
        CMyEngine Engine;
 
	Engine.SetTitle( "Loading..." );
	Engine.Init();
 
	Engine.SetTitle( "SDL Testing!" );
	Engine.Start();

	string text = FILE.ReadAllText("text.txt");

	Engine.SetTitle( "Quitting..." );
	return 0;
}

...



EDIT: Forgot to post the errors :P BUILD LOG:
1>------ Build started: Project: SDL Engine, Configuration: Debug Win32 ------
1>Compiling...
1>Main.cpp
1>c:\users\anthony\documents\visual studio 2005\projects\sdl engine\sdl engine\main.cpp(61) : error C2039: 'ReadAllText' : is not a member of '_iobuf'
1>        c:\program files\microsoft visual studio 8\vc\include\stdio.h(59) : see declaration of '_iobuf'
1>Build log was saved at "file://c:\Users\Anthony\Documents\Visual Studio 2005\Projects\SDL Engine\SDL Engine\Debug\BuildLog.htm"
1>SDL Engine - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Share this post


Link to post
Share on other sites
Advertisement
FILE is a struct that's part of the C standard library. Since it's from C, it lacks member functions of any sort; it certainly does not have a a "ReadAllText" member function.

That's what the error message is telling you, although it's a bit obfuscated because FILE is really a typedef for some other underlying library type (_iobuf, in this case). What makes you think there's a ReadAllText function?

There is no built-in way to read all the text in a file in C++. You will have to do it yourself using, for example, std::getline in a loop.

Other, unrelated points:
You should #include <cstdlib> and #include <cstdio> in C++, rather than stdlib.h and stdio.h. Additionally, you should use the angle-bracket syntax to include iostream as well.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
FILE is a struct that's part of the C standard library. Since it's from C, it lacks member functions of any sort; it certainly does not have a a "ReadAllText" member function.

That's what the error message is telling you, although it's a bit obfuscated because FILE is really a typedef for some other underlying library type (_iobuf, in this case). What makes you think there's a ReadAllText function?

There is no built-in way to read all the text in a file in C++. You will have to do it yourself using, for example, std::getline in a loop.

Other, unrelated points:
You should #include <cstdlib> and #include <cstdio> in C++, rather than stdlib.h and stdio.h. Additionally, you should use the angle-bracket syntax to include iostream as well.


Ok, thanks for the reply and the help

Share this post


Link to post
Share on other sites
Quote:
Original post by ravengangrel
I guess you have seen that File.ReadAllText instruction here. Note, however, that it's C# related, not C++.


Yes, I thought because they are closly related, that they would have similar syntax(I knew they weren't going to be exact).

I have looked up on the getline and found this page

http://www.java2s.com/Code/Cpp/File/readingatextfile.htm

Which sets it up and works, but it shows all of the content of the file at once. Is it possible to have it read the file, and store each line into an array? So instead of having 10 or so text files with 1 line each, I could have 1 text file with all the lines and just have them shown via printing an array.

Share this post


Link to post
Share on other sites

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

int main(int, char **) {
std::vector<std::string> lines;
std::ifstream ifs("main.cpp");
while (ifs) {
std::string line;
std::getline(ifs, line);
if (ifs) lines.push_back(line);
}
// do something with the lines. In this case, print them out to the console
std::copy(lines.begin(), lines.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}

Share this post


Link to post
Share on other sites
Easy!


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("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;
}



That's the original code from the link you posted.
You would need to #include <vector>, and after the string declaration

string line;

you would declare a vector of strings:

vector<string> linesVector;

Then inside the while loop, instead of getting a line and printing it out, you would store it into the array:

while (! myfile.eof() )
{
getline (myfile,line); //get the line
linesVector.push_back(line); //store it in the vector
}


EDIT: PWND!!

Share this post


Link to post
Share on other sites
@ravengangrel

Your code contains a small bug. It will read an additional line over the number there are in the file. This is because std::ifstream::eof() only reports the end of stream after an attempt to read.

The solution is to use the read operation as the loop condition, like so:

while(getline(myfile,line))
{
// use "line"
}

Alternatively, you could use SiCrane's code, which has similar logic.

Also note that it is unnecessary to close() the fstream, the destructor will close() the stream for you.

Share this post


Link to post
Share on other sites
Thanks, I didn't know that. Anyway, I was just reusing the code in the link the OP posted.
I guess using good() instead of !eof() has the same problem, right?

Share this post


Link to post
Share on other sites
@raven

Using your code I get an error:

1>c:\users\anthony\documents\visual studio 2005\projects\tests\tests\main.cpp(10) : error C2337: 'i' : attribute not found

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!