Sign in to follow this  

File.ReadAllText?

This topic is 3114 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
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:

[i]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
@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
Quote:
Original post by Powell
@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


Can you post the code please? I can't see any syntax errors in the code (certainly nothing resembling this), so perhaps you made some small mistake when copying it.

Share this post


Link to post
Share on other sites
It sounds like you copy and pasted this line:

[i]vector<string> linesVector;

This is a mis-formatted text. It looks like he tried to italicize the line but forgot the closing tag. The [i] is not part of the code.

Share this post


Link to post
Share on other sites
Alternatively, if you don't need it separated by lines (an XML file for instance)

std::ifstream file("yarr.xml");
std::string text;
std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), std::back_inserter(text));

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
It sounds like you copy and pasted this line:

[i]vector<string> linesVector;

This is a mis-formatted text. It looks like he tried to italicize the line but forgot the closing tag. The [i] is not part of the code.


Yes that was it! Thanks

Share this post


Link to post
Share on other sites
Again, a big newb here, here is my code


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

using namespace std;

void readFile ()
{
string line;
vector<string> linesVector;
ifstream myfile ("text.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
linesVector.push_back(line);
}
myfile.close();
}
else
cout << "Unable to open file" << endl;
}

int main ()
{
if (readFile::linesVector.empty());
{
cout << "This vector is empty!" << endl;
}
else
cout << "There is something is the vector!" << endl;

}



How can I get information out of readFile into main?

This is also more of a general question sense I have never done anything past main.

Share this post


Link to post
Share on other sites
Quote:
Original post by Powell
Again, a big newb here, here is my code

*** Source Snippet Removed ***

How can I get information out of readFile into main?

This is also more of a general question sense I have never done anything past main.

You have to either return it from your function, or you have to pass it in as a reference parameter. In either case your current code is wrong...


std::vector<std::string> readAllLines(std::string const& filename) {
std::vector<std::string> lines;

std::ifstream ifs(filename.c_str());

while (ifs) {
std::string line;
std::getline(ifs, line);
if (ifs) lines.push_back(line);
}

return lines;
}

int main() {
std::vector<std::string> lines = readAllLines("oogabooga.txt");

std::copy(lines.begin(), lines.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
Quote:
Original post by Powell
Again, a big newb here, here is my code

*** Source Snippet Removed ***

How can I get information out of readFile into main?

This is also more of a general question sense I have never done anything past main.

You have to either return it from your function, or you have to pass it in as a reference parameter. In either case your current code is wrong...


std::vector<std::string> readAllLines(std::string const& filename) {
std::vector<std::string> lines;

std::ifstream ifs(filename.c_str());

while (ifs) {
std::string line;
std::getline(ifs, line);
if (ifs) lines.push_back(line);
}

return lines;
}

int main() {
std::vector<std::string> lines = readAllLines("oogabooga.txt");

std::copy(lines.begin(), lines.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}



but that code outputted the whole file, while I only want to print out individual lines I specify.

Share this post


Link to post
Share on other sites
... that's why you use a for loop instead of std::copy, and perhaps an "if" statement. You do have to do SOME work for yourself you know...

No offense intended but: A big part of programming is problem solving. That means taking a problem, dissecting it into its various components, and then figuring out how to build a solution based on what you have. If you don't know what to do, the best thing to do is to learn (aka, research, spend time reading, etc). From what I've seen here, you've basically copied and pasted what's been posted, instead of LEARNING what the code that's been posted does. You will never learn anything if you don't start trying to solve the problem yourself. Generally you will never be given the entire solution to a problem, instead people will give you bits and pieces, and your job is to figure out how to take the knowledge you've learned (from those bits and pieces) and put them together or alter them to provide a solution to your problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
... that's why you use a for loop instead of std::copy, and perhaps an "if" statement. You do have to do SOME work for yourself you know...

No offense intended but: A big part of programming is problem solving. That means taking a problem, dissecting it into its various components, and then figuring out how to build a solution based on what you have. If you don't know what to do, the best thing to do is to learn (aka, research, spend time reading, etc). From what I've seen here, you've basically copied and pasted what's been posted, instead of LEARNING what the code that's been posted does. You will never learn anything if you don't start trying to solve the problem yourself. Generally you will never be given the entire solution to a problem, instead people will give you bits and pieces, and your job is to figure out how to take the knowledge you've learned (from those bits and pieces) and put them together or alter them to provide a solution to your problem.


Yes I do understand that


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

using namespace std;

vector<string> readAllLines(string const& filename)
{
vector<string> lines;

ifstream ifs(filename.c_str());

while (ifs)
{
string line;
getline(ifs, line);
if (ifs) lines.push_back(line);
}

return lines;
}

int main()
{
vector<string> lines = readAllLines("text.txt");
int i = lines.size();
cout << i << endl;
}



I managed to get it to tell me the size of the vector, nothing more. I will lone it from here, close this thread if need be

Share this post


Link to post
Share on other sites
Quote:
Original post by Powell
I managed to get it to tell me the size of the vector, nothing more. I will lone it from here, close this thread if need be

*sigh*
Now you sound like a petulant child.

You know the size of the vector, now perhaps you should lookup "How do I iterate (or loop) over a vector" in whatever reference material you have.

Share this post


Link to post
Share on other sites
wow, forget child I am an idiot. I forgot about printing arrays, and printing vectors is similar

cout << lines[0] << endl;

*facepalm*

Share this post


Link to post
Share on other sites
Quote:
Original post by Powell
wow, forget child I am an idiot. I forgot about printing arrays, and printing vectors is similar

cout << lines[0] << endl;

*facepalm*


Bingo. You win 1 internet.

Share this post


Link to post
Share on other sites

This topic is 3114 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this