Element access (get and put) using std::vector in C++

Viewed 334

I am trying to manipulate elements of file (.txt) using vector - such as push_back, accessing [] etc. I want to perform a simple encoding on the elements I retrieved from the file, I succeeded partially though the end result is not what I expected.

Here is the file content (sample):

datum
-6353
argo

What I wanna perform here is read each and every element of the vector (kinda 2-D) and encode them with their respective ASCII codes.

Here is my code (so far):

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <algorithm>
    #include <iterator>

    using namespace std;

    int main () 
    {
        std::string word;
        //std::vector<std::vector <std::string> > file;
        std::vector<std::string> file;

        std::ifstream in( "test.txt" );

        while (in >> word)
        {
            file.push_back(word);
        }

        for (size_t i=0; i<file.size(); i++)
        {
           for (int j=0;j<file[i].size();j++)
           {
               //cout<<i<<","<< j<<endl;
               cout<<((int)file[i][j])<<",";
           }
        }

        in.close();

  return 0;
}

Present Output (for the above code):

100,97,116,117,109,45,54,51,53,51,97,114,103,111,
RUN FINISHED; exit value 0; real time: 0ms; user: 0ms; system: 0ms

Expected Output:

100,97,116,117,109
45,54,51,53,51
97,114,103,111

Please guide me to achieve this.

2 Answers
Related