File.ReadAllText?

Started by
20 comments, last by Washu 14 years, 10 months ago
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.
Advertisement
It sounds like you copy and pasted this line:

vector<string> linesVector;

This is a mis-formatted text. It looks like he tried to italicize the line but forgot the closing tag. The is not part of the code.
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));

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Quote:Original post by SiCrane
It sounds like you copy and pasted this line:

vector<string> linesVector;

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


Yes that was it! Thanks
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.
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"));}

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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.
... 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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

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
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.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement