Parsing ASCII Text file into 2d Vector of Char's











up vote
0
down vote

favorite












I need help, im trying to read a file that looks something like this:



.........
.........
.........
.........
....X....
.........
.........
.........
.........


i need to parse this into a 2d vector of chars so i can make modifications to it later.



What ive come up with so far is



#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
//look up line by line parsing
using namespace std;
int main(int argc, char* argv) {
vector<vector<char>> data;
ifstream myReadFile;
myReadFile.open("input1.txt");

for (int i = 0; i < data.size(); i++) {
int c = 0;
char currentchar;

while (!myReadFile.eof()) {
data[i][c] = currentchar;
c++;
currentchar = myReadFile.get();
}
}

//for ()


myReadFile.close();


return 0;
}









share|improve this question






















  • Re: i < data.size() -- data.size() is 0, so the loop body won't be run. Do you know the dimensions of the grid?
    – Pete Becker
    Nov 7 at 19:25












  • This doesn't address the question, but get in the habit of initializing objects with meaningful values instead of creating them with default constructors and immediately changing them. That is, change ifstream myReadFile; myReadFile.open("input1.txt'); to ifstream myReadFile("input1.txt");.
    – Pete Becker
    Nov 7 at 19:27












  • @PeteBecker Thanks, im new to c++ ill keep this in mind
    – xannax159
    Nov 7 at 20:22















up vote
0
down vote

favorite












I need help, im trying to read a file that looks something like this:



.........
.........
.........
.........
....X....
.........
.........
.........
.........


i need to parse this into a 2d vector of chars so i can make modifications to it later.



What ive come up with so far is



#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
//look up line by line parsing
using namespace std;
int main(int argc, char* argv) {
vector<vector<char>> data;
ifstream myReadFile;
myReadFile.open("input1.txt");

for (int i = 0; i < data.size(); i++) {
int c = 0;
char currentchar;

while (!myReadFile.eof()) {
data[i][c] = currentchar;
c++;
currentchar = myReadFile.get();
}
}

//for ()


myReadFile.close();


return 0;
}









share|improve this question






















  • Re: i < data.size() -- data.size() is 0, so the loop body won't be run. Do you know the dimensions of the grid?
    – Pete Becker
    Nov 7 at 19:25












  • This doesn't address the question, but get in the habit of initializing objects with meaningful values instead of creating them with default constructors and immediately changing them. That is, change ifstream myReadFile; myReadFile.open("input1.txt'); to ifstream myReadFile("input1.txt");.
    – Pete Becker
    Nov 7 at 19:27












  • @PeteBecker Thanks, im new to c++ ill keep this in mind
    – xannax159
    Nov 7 at 20:22













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I need help, im trying to read a file that looks something like this:



.........
.........
.........
.........
....X....
.........
.........
.........
.........


i need to parse this into a 2d vector of chars so i can make modifications to it later.



What ive come up with so far is



#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
//look up line by line parsing
using namespace std;
int main(int argc, char* argv) {
vector<vector<char>> data;
ifstream myReadFile;
myReadFile.open("input1.txt");

for (int i = 0; i < data.size(); i++) {
int c = 0;
char currentchar;

while (!myReadFile.eof()) {
data[i][c] = currentchar;
c++;
currentchar = myReadFile.get();
}
}

//for ()


myReadFile.close();


return 0;
}









share|improve this question













I need help, im trying to read a file that looks something like this:



.........
.........
.........
.........
....X....
.........
.........
.........
.........


i need to parse this into a 2d vector of chars so i can make modifications to it later.



What ive come up with so far is



#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
//look up line by line parsing
using namespace std;
int main(int argc, char* argv) {
vector<vector<char>> data;
ifstream myReadFile;
myReadFile.open("input1.txt");

for (int i = 0; i < data.size(); i++) {
int c = 0;
char currentchar;

while (!myReadFile.eof()) {
data[i][c] = currentchar;
c++;
currentchar = myReadFile.get();
}
}

//for ()


myReadFile.close();


return 0;
}






c++ parsing vector






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 7 at 19:18









xannax159

448




448












  • Re: i < data.size() -- data.size() is 0, so the loop body won't be run. Do you know the dimensions of the grid?
    – Pete Becker
    Nov 7 at 19:25












  • This doesn't address the question, but get in the habit of initializing objects with meaningful values instead of creating them with default constructors and immediately changing them. That is, change ifstream myReadFile; myReadFile.open("input1.txt'); to ifstream myReadFile("input1.txt");.
    – Pete Becker
    Nov 7 at 19:27












  • @PeteBecker Thanks, im new to c++ ill keep this in mind
    – xannax159
    Nov 7 at 20:22


















  • Re: i < data.size() -- data.size() is 0, so the loop body won't be run. Do you know the dimensions of the grid?
    – Pete Becker
    Nov 7 at 19:25












  • This doesn't address the question, but get in the habit of initializing objects with meaningful values instead of creating them with default constructors and immediately changing them. That is, change ifstream myReadFile; myReadFile.open("input1.txt'); to ifstream myReadFile("input1.txt");.
    – Pete Becker
    Nov 7 at 19:27












  • @PeteBecker Thanks, im new to c++ ill keep this in mind
    – xannax159
    Nov 7 at 20:22
















Re: i < data.size() -- data.size() is 0, so the loop body won't be run. Do you know the dimensions of the grid?
– Pete Becker
Nov 7 at 19:25






Re: i < data.size() -- data.size() is 0, so the loop body won't be run. Do you know the dimensions of the grid?
– Pete Becker
Nov 7 at 19:25














This doesn't address the question, but get in the habit of initializing objects with meaningful values instead of creating them with default constructors and immediately changing them. That is, change ifstream myReadFile; myReadFile.open("input1.txt'); to ifstream myReadFile("input1.txt");.
– Pete Becker
Nov 7 at 19:27






This doesn't address the question, but get in the habit of initializing objects with meaningful values instead of creating them with default constructors and immediately changing them. That is, change ifstream myReadFile; myReadFile.open("input1.txt'); to ifstream myReadFile("input1.txt");.
– Pete Becker
Nov 7 at 19:27














@PeteBecker Thanks, im new to c++ ill keep this in mind
– xannax159
Nov 7 at 20:22




@PeteBecker Thanks, im new to c++ ill keep this in mind
– xannax159
Nov 7 at 20:22












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










You need to reserve space in the vector before assigning values to it through data[i][c] = currentchar;. But it's probably hard to reserve space before having read the contents.



An easier way would be to use the dynamic growth capabilities of vectors (i.e. push_back), and to use std::string as line content since you can read in complete lines then easily. You can still access/alter the contents then trough data[i][c] = currentchar;. See the following code illustrating this:



#include <sstream>
#include <iostream>
#include <vector>

int main() {

const char* fileContent = R"foo(.........
.........
.........
.........
....X....
.........
.........
.........
.........)foo";

std::vector<std::string> lines;
stringstream ss(fileContent);
string line;
while (getline(ss,line)) {
lines.push_back(line);
}

lines[2][5] = 'Y';

for (auto line : lines) {
for (auto c : line) {
cout << c << " ";
}
cout << endl;
}
}


Output:



. . . . . . . . . 
. . . . . . . . .
. . . . . Y . . .
. . . . . . . . .
. . . . X . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .
. . . . . . . . .





share|improve this answer

















  • 1




    thank you! i was able to modify your solution to work with text files input by the user.
    – xannax159
    Nov 8 at 19:06


















up vote
0
down vote













You may make life easier by using std::getline and std::string:



std::string row_text;
std::vector<std::string> grid;
while (std::getline(myReadFile, row_text))
{
grid.push_back(row_text);
}


The std::string can be accessed using array notation.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196324%2fparsing-ascii-text-file-into-2d-vector-of-chars%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    You need to reserve space in the vector before assigning values to it through data[i][c] = currentchar;. But it's probably hard to reserve space before having read the contents.



    An easier way would be to use the dynamic growth capabilities of vectors (i.e. push_back), and to use std::string as line content since you can read in complete lines then easily. You can still access/alter the contents then trough data[i][c] = currentchar;. See the following code illustrating this:



    #include <sstream>
    #include <iostream>
    #include <vector>

    int main() {

    const char* fileContent = R"foo(.........
    .........
    .........
    .........
    ....X....
    .........
    .........
    .........
    .........)foo";

    std::vector<std::string> lines;
    stringstream ss(fileContent);
    string line;
    while (getline(ss,line)) {
    lines.push_back(line);
    }

    lines[2][5] = 'Y';

    for (auto line : lines) {
    for (auto c : line) {
    cout << c << " ";
    }
    cout << endl;
    }
    }


    Output:



    . . . . . . . . . 
    . . . . . . . . .
    . . . . . Y . . .
    . . . . . . . . .
    . . . . X . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .





    share|improve this answer

















    • 1




      thank you! i was able to modify your solution to work with text files input by the user.
      – xannax159
      Nov 8 at 19:06















    up vote
    0
    down vote



    accepted










    You need to reserve space in the vector before assigning values to it through data[i][c] = currentchar;. But it's probably hard to reserve space before having read the contents.



    An easier way would be to use the dynamic growth capabilities of vectors (i.e. push_back), and to use std::string as line content since you can read in complete lines then easily. You can still access/alter the contents then trough data[i][c] = currentchar;. See the following code illustrating this:



    #include <sstream>
    #include <iostream>
    #include <vector>

    int main() {

    const char* fileContent = R"foo(.........
    .........
    .........
    .........
    ....X....
    .........
    .........
    .........
    .........)foo";

    std::vector<std::string> lines;
    stringstream ss(fileContent);
    string line;
    while (getline(ss,line)) {
    lines.push_back(line);
    }

    lines[2][5] = 'Y';

    for (auto line : lines) {
    for (auto c : line) {
    cout << c << " ";
    }
    cout << endl;
    }
    }


    Output:



    . . . . . . . . . 
    . . . . . . . . .
    . . . . . Y . . .
    . . . . . . . . .
    . . . . X . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .





    share|improve this answer

















    • 1




      thank you! i was able to modify your solution to work with text files input by the user.
      – xannax159
      Nov 8 at 19:06













    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    You need to reserve space in the vector before assigning values to it through data[i][c] = currentchar;. But it's probably hard to reserve space before having read the contents.



    An easier way would be to use the dynamic growth capabilities of vectors (i.e. push_back), and to use std::string as line content since you can read in complete lines then easily. You can still access/alter the contents then trough data[i][c] = currentchar;. See the following code illustrating this:



    #include <sstream>
    #include <iostream>
    #include <vector>

    int main() {

    const char* fileContent = R"foo(.........
    .........
    .........
    .........
    ....X....
    .........
    .........
    .........
    .........)foo";

    std::vector<std::string> lines;
    stringstream ss(fileContent);
    string line;
    while (getline(ss,line)) {
    lines.push_back(line);
    }

    lines[2][5] = 'Y';

    for (auto line : lines) {
    for (auto c : line) {
    cout << c << " ";
    }
    cout << endl;
    }
    }


    Output:



    . . . . . . . . . 
    . . . . . . . . .
    . . . . . Y . . .
    . . . . . . . . .
    . . . . X . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .





    share|improve this answer












    You need to reserve space in the vector before assigning values to it through data[i][c] = currentchar;. But it's probably hard to reserve space before having read the contents.



    An easier way would be to use the dynamic growth capabilities of vectors (i.e. push_back), and to use std::string as line content since you can read in complete lines then easily. You can still access/alter the contents then trough data[i][c] = currentchar;. See the following code illustrating this:



    #include <sstream>
    #include <iostream>
    #include <vector>

    int main() {

    const char* fileContent = R"foo(.........
    .........
    .........
    .........
    ....X....
    .........
    .........
    .........
    .........)foo";

    std::vector<std::string> lines;
    stringstream ss(fileContent);
    string line;
    while (getline(ss,line)) {
    lines.push_back(line);
    }

    lines[2][5] = 'Y';

    for (auto line : lines) {
    for (auto c : line) {
    cout << c << " ";
    }
    cout << endl;
    }
    }


    Output:



    . . . . . . . . . 
    . . . . . . . . .
    . . . . . Y . . .
    . . . . . . . . .
    . . . . X . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 7 at 19:45









    Stephan Lechner

    24.5k21738




    24.5k21738








    • 1




      thank you! i was able to modify your solution to work with text files input by the user.
      – xannax159
      Nov 8 at 19:06














    • 1




      thank you! i was able to modify your solution to work with text files input by the user.
      – xannax159
      Nov 8 at 19:06








    1




    1




    thank you! i was able to modify your solution to work with text files input by the user.
    – xannax159
    Nov 8 at 19:06




    thank you! i was able to modify your solution to work with text files input by the user.
    – xannax159
    Nov 8 at 19:06












    up vote
    0
    down vote













    You may make life easier by using std::getline and std::string:



    std::string row_text;
    std::vector<std::string> grid;
    while (std::getline(myReadFile, row_text))
    {
    grid.push_back(row_text);
    }


    The std::string can be accessed using array notation.






    share|improve this answer

























      up vote
      0
      down vote













      You may make life easier by using std::getline and std::string:



      std::string row_text;
      std::vector<std::string> grid;
      while (std::getline(myReadFile, row_text))
      {
      grid.push_back(row_text);
      }


      The std::string can be accessed using array notation.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You may make life easier by using std::getline and std::string:



        std::string row_text;
        std::vector<std::string> grid;
        while (std::getline(myReadFile, row_text))
        {
        grid.push_back(row_text);
        }


        The std::string can be accessed using array notation.






        share|improve this answer












        You may make life easier by using std::getline and std::string:



        std::string row_text;
        std::vector<std::string> grid;
        while (std::getline(myReadFile, row_text))
        {
        grid.push_back(row_text);
        }


        The std::string can be accessed using array notation.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 19:46









        Thomas Matthews

        43.9k1168120




        43.9k1168120






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196324%2fparsing-ascii-text-file-into-2d-vector-of-chars%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()