a function that reads a text file

Hi there

I'm trying to create a function that will get a text file and return a vector of strings, one for each line.

I think my main problem is that I don't understand exactly how to exactly syntax it. I'm adding the code for it and please let me know what is my problem.
Don't mind the fact that at the moment it should just print the content of the file.

vector <string> fromFileToVectorOfLines(string fileAsString) {
vector <string> vectorOfLines;
string line;
char *a=(char*)(fileAsString.c_str());
ifstream myfile(a);
if (myfile.is_open()){
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
return vectorOfLines;
}

thank you
What you have seems mostly fine. No point going the long way around with casting the string to a char array, though.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vector <string> fromFileToVectorOfLines(string fileAsString) {
  vector <string> vectorOfLines;
  string line;
  ifstream myfile(fileAsString.c_str());
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
       getline (myfile,line);
       cout << line << endl;
    }
    myfile.close();
  }
return vectorOfLines;
}

Hey

First thanks for the help

Second - I don't have any compilation problems, it's just not printing anything.
When I run my main function like this:

int main(){
cout << fromFileToVectorOfLines("example.txt") << endl;

return 0;
}

it dosen't print anything.
neither when the parameter called is "../src/example.txt" (src is the folder of example.txt)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vector<string> fromFileToVectorOfLines(string fileAsString) {
    vector <string> vectorOfLines;
    string line;
    char *a=(char*)(fileAsString.c_str());
    ifstream myfile(a);
    if (myfile.is_open()){ 
        while ( myfile.good() )
        {
            getline (myfile,line);
            cout << line << endl;
        }
        myfile.close();
    }
return vectorOfLines;
}


Other than returning vectorOfLines, what are you doing with it?

I would assume the loop should look something like:
1
2
        while ( getline(myfile, line) )
            vectorOfLines.push_back(line) ;

If you're getting nothing for output, given line 10, your file is never being succesfully opened.

Last edited on
I expect that example.txt is not in the same directory as the executable that is running, nor is it in the relative directory "../src/example.txt"

Most IDEs do not put the executable in the same directory as the source code.
Last edited on
OK ive done some checkings and indeed the file is not successfully opened.
Any idea how to fix that? let me know if I need to give more info.
And yes, my code would look like that exactly, just for the moment I want to see that the file is read properly.

Thanks
Any idea how to fix that?


Use the full directory structure to identify the file to open, or make sure that the file you want to open is in the same directory as the executable you are running (i.e. your program).
Unfortunately it dosen't work as well. They are both in the same folder but the file does not open.
B.T.W I'm running all this through the konsole cause my eclipse does not work. So maybe I have problem with that? The file compiles well, but maybe I need to do linking or something to both files?
For now what I've done is just compiling the program.cpp and run it.
It works fine for me. I run it, it opens the text file and displays it on screen.

The code is:

339.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <vector>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;


vector <string> fromFileToVectorOfLines(string fileAsString) {
  vector <string> vectorOfLines;
  string line;
  ifstream myfile(fileAsString.c_str());
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
       getline (myfile,line);
       cout << line << endl;
    }
    myfile.close();
  }
return vectorOfLines;
}

int main()
{
  fromFileToVectorOfLines("339.cpp");
}


The output is (I pointed it at its own source file):
#include <vector>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;


vector <string> fromFileToVectorOfLines(string fileAsString) {
  vector <string> vectorOfLines;
  string line;
  ifstream myfile(fileAsString.c_str());
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
       getline (myfile,line);
       cout << line << endl;
    }
    myfile.close();
  }
return vectorOfLines;
}

int main()
{
  fromFileToVectorOfLines("339.cpp");
}


The compilation command is:
g++ 339.cpp

There is no linking difficultly as this is a single source file program.
Hey thanks for the help!
My problem was I put the txt file in the same directory as the cpp file and not the compiled file.

Topic archived. No new replies allowed.