C++ Read Tab Delimited File Into Vector and Display It The 2019 Stack Overflow Developer Survey Results Are InC++ error when opening fileRead whole ASCII file into C++ std::stringRead file line by line using ifstream in C++Why is reading lines from stdin much slower in C++ than Python?In C++: How to read from file into a vector of objectsReading File into Vector Delimited by Comma and New LineSegmantation fault in c++, no outputC++ Taking data from a text file into int VectorTokenize returns a vector that contains the delimiters - C++C++ Read binary file into vector of custom class

If I score a critical hit on an 18 or higher, what are my chances of getting a critical hit if I roll 3d20?

Where to refill my bottle in India?

During Temple times, who can butcher a kosher animal?

How to save as into a customized destination on macOS?

Are there incongruent pythagorean triangles with the same perimeter and same area?

Can a rogue use sneak attack with weapons that have the thrown property even if they are not thrown?

Time travel alters history but people keep saying nothing's changed

Falsification in Math vs Science

How to deal with fear of taking dependencies

Can one be advised by a professor who is very far away?

Do these rules for Critical Successes and Critical Failures seem Fair?

What to do when moving next to a bird sanctuary with a loosely-domesticated cat?

Why didn't the Event Horizon Telescope team mention Sagittarius A*?

Why isn't airport relocation done gradually?

Worn-tile Scrabble

What do hard-Brexiteers want with respect to the Irish border?

Why was M87 targetted for the Event Horizon Telescope instead of Sagittarius A*?

Protecting Dualbooting Windows from dangerous code (like rm -rf)

How to notate time signature switching consistently every measure

What does "fetching by region is not available for SAM files" means?

Identify boardgame from Big movie

Geography at the pixel level

How are circuits which use complex ICs normally simulated?

How to answer pointed "are you quitting" questioning when I don't want them to suspect



C++ Read Tab Delimited File Into Vector and Display It



The 2019 Stack Overflow Developer Survey Results Are InC++ error when opening fileRead whole ASCII file into C++ std::stringRead file line by line using ifstream in C++Why is reading lines from stdin much slower in C++ than Python?In C++: How to read from file into a vector of objectsReading File into Vector Delimited by Comma and New LineSegmantation fault in c++, no outputC++ Taking data from a text file into int VectorTokenize returns a vector that contains the delimiters - C++C++ Read binary file into vector of custom class



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















Im trying to read a tab delimited file into a string vector and display it. But I am not getting the desired output.



This is the file I am trying to read in and display:



1 amazon billybob@kfc.com password!23
2 facebook digitalGlut3n@ello.mail fri3dMayoNaize
3 bank.com brokeDude@sofa.com LostTheRemot3!.Crap


This is my display function:



void DisplayRecords() 

vector<string> vRecords;
ifstream inFile("database.txt");

string entry;

while (inFile >> entry)
vRecords.push_back(entry);


for (int i = 0; i < vRecords.size(); i++)
if (i % 4 == 0)
cout << "n";

cout << setw(5) << vRecords[i];


cout << "nn";



And this is the output I get:



1amazonbillybob@kfc.compassword!23
2facebookdigitalGlut3n@ello.mailfri3dMayoNaize
3bank.combrokeDude@sofa.comLostTheRemot3!.CrapPress any key to continue . . .


How would I get my function to display similar to the original file with spaces between strings?










share|improve this question
























  • One small thing. Your if statement in the for loop triggers whenever i%4 == 0, but you start at i = 0 for which i%4 == 0 is true.

    – iHowell
    Mar 22 at 3:47






  • 2





    It feels almost crazy to have to point out that you are not currently outputting any space between records, apart from a newline. And so, if you want spaces between records, then you need to output them.

    – paddy
    Mar 22 at 3:47











  • I understand. I shouldn't have said "spaces." I am setting a column width with setw() to help keep things uniform and aligned. I shouldn't need to cout >> " "; I adjusted the value from 5 to 30 and it looks better now but still not good.

    – okkv1747vm
    Mar 22 at 4:04


















-1















Im trying to read a tab delimited file into a string vector and display it. But I am not getting the desired output.



This is the file I am trying to read in and display:



1 amazon billybob@kfc.com password!23
2 facebook digitalGlut3n@ello.mail fri3dMayoNaize
3 bank.com brokeDude@sofa.com LostTheRemot3!.Crap


This is my display function:



void DisplayRecords() 

vector<string> vRecords;
ifstream inFile("database.txt");

string entry;

while (inFile >> entry)
vRecords.push_back(entry);


for (int i = 0; i < vRecords.size(); i++)
if (i % 4 == 0)
cout << "n";

cout << setw(5) << vRecords[i];


cout << "nn";



And this is the output I get:



1amazonbillybob@kfc.compassword!23
2facebookdigitalGlut3n@ello.mailfri3dMayoNaize
3bank.combrokeDude@sofa.comLostTheRemot3!.CrapPress any key to continue . . .


How would I get my function to display similar to the original file with spaces between strings?










share|improve this question
























  • One small thing. Your if statement in the for loop triggers whenever i%4 == 0, but you start at i = 0 for which i%4 == 0 is true.

    – iHowell
    Mar 22 at 3:47






  • 2





    It feels almost crazy to have to point out that you are not currently outputting any space between records, apart from a newline. And so, if you want spaces between records, then you need to output them.

    – paddy
    Mar 22 at 3:47











  • I understand. I shouldn't have said "spaces." I am setting a column width with setw() to help keep things uniform and aligned. I shouldn't need to cout >> " "; I adjusted the value from 5 to 30 and it looks better now but still not good.

    – okkv1747vm
    Mar 22 at 4:04














-1












-1








-1








Im trying to read a tab delimited file into a string vector and display it. But I am not getting the desired output.



This is the file I am trying to read in and display:



1 amazon billybob@kfc.com password!23
2 facebook digitalGlut3n@ello.mail fri3dMayoNaize
3 bank.com brokeDude@sofa.com LostTheRemot3!.Crap


This is my display function:



void DisplayRecords() 

vector<string> vRecords;
ifstream inFile("database.txt");

string entry;

while (inFile >> entry)
vRecords.push_back(entry);


for (int i = 0; i < vRecords.size(); i++)
if (i % 4 == 0)
cout << "n";

cout << setw(5) << vRecords[i];


cout << "nn";



And this is the output I get:



1amazonbillybob@kfc.compassword!23
2facebookdigitalGlut3n@ello.mailfri3dMayoNaize
3bank.combrokeDude@sofa.comLostTheRemot3!.CrapPress any key to continue . . .


How would I get my function to display similar to the original file with spaces between strings?










share|improve this question
















Im trying to read a tab delimited file into a string vector and display it. But I am not getting the desired output.



This is the file I am trying to read in and display:



1 amazon billybob@kfc.com password!23
2 facebook digitalGlut3n@ello.mail fri3dMayoNaize
3 bank.com brokeDude@sofa.com LostTheRemot3!.Crap


This is my display function:



void DisplayRecords() 

vector<string> vRecords;
ifstream inFile("database.txt");

string entry;

while (inFile >> entry)
vRecords.push_back(entry);


for (int i = 0; i < vRecords.size(); i++)
if (i % 4 == 0)
cout << "n";

cout << setw(5) << vRecords[i];


cout << "nn";



And this is the output I get:



1amazonbillybob@kfc.compassword!23
2facebookdigitalGlut3n@ello.mailfri3dMayoNaize
3bank.combrokeDude@sofa.comLostTheRemot3!.CrapPress any key to continue . . .


How would I get my function to display similar to the original file with spaces between strings?







c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 5:19









Remy Lebeau

343k19268462




343k19268462










asked Mar 22 at 3:40









okkv1747vmokkv1747vm

204




204












  • One small thing. Your if statement in the for loop triggers whenever i%4 == 0, but you start at i = 0 for which i%4 == 0 is true.

    – iHowell
    Mar 22 at 3:47






  • 2





    It feels almost crazy to have to point out that you are not currently outputting any space between records, apart from a newline. And so, if you want spaces between records, then you need to output them.

    – paddy
    Mar 22 at 3:47











  • I understand. I shouldn't have said "spaces." I am setting a column width with setw() to help keep things uniform and aligned. I shouldn't need to cout >> " "; I adjusted the value from 5 to 30 and it looks better now but still not good.

    – okkv1747vm
    Mar 22 at 4:04


















  • One small thing. Your if statement in the for loop triggers whenever i%4 == 0, but you start at i = 0 for which i%4 == 0 is true.

    – iHowell
    Mar 22 at 3:47






  • 2





    It feels almost crazy to have to point out that you are not currently outputting any space between records, apart from a newline. And so, if you want spaces between records, then you need to output them.

    – paddy
    Mar 22 at 3:47











  • I understand. I shouldn't have said "spaces." I am setting a column width with setw() to help keep things uniform and aligned. I shouldn't need to cout >> " "; I adjusted the value from 5 to 30 and it looks better now but still not good.

    – okkv1747vm
    Mar 22 at 4:04

















One small thing. Your if statement in the for loop triggers whenever i%4 == 0, but you start at i = 0 for which i%4 == 0 is true.

– iHowell
Mar 22 at 3:47





One small thing. Your if statement in the for loop triggers whenever i%4 == 0, but you start at i = 0 for which i%4 == 0 is true.

– iHowell
Mar 22 at 3:47




2




2





It feels almost crazy to have to point out that you are not currently outputting any space between records, apart from a newline. And so, if you want spaces between records, then you need to output them.

– paddy
Mar 22 at 3:47





It feels almost crazy to have to point out that you are not currently outputting any space between records, apart from a newline. And so, if you want spaces between records, then you need to output them.

– paddy
Mar 22 at 3:47













I understand. I shouldn't have said "spaces." I am setting a column width with setw() to help keep things uniform and aligned. I shouldn't need to cout >> " "; I adjusted the value from 5 to 30 and it looks better now but still not good.

– okkv1747vm
Mar 22 at 4:04






I understand. I shouldn't have said "spaces." I am setting a column width with setw() to help keep things uniform and aligned. I shouldn't need to cout >> " "; I adjusted the value from 5 to 30 and it looks better now but still not good.

– okkv1747vm
Mar 22 at 4:04













2 Answers
2






active

oldest

votes


















1














A vector of individual strings really does not make a lot of sense for structured data. A vector of structs would make more sense, eg:



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

struct Record

std::string id;
std::string site;
std::string user;
std::string pass;
;

void DisplayRecords()

std::vector<Record> vRecords;
std::ifstream inFile("database.txt");
std::string entry;

while (std::getline(inFile, entry))

std::istringstream iss(entry);

Record rec;
std::getline(iss, rec.id, 't');
std::getline(iss, rec.site, 't');
std::getline(iss, rec.user, 't');
std::getline(iss, rec.pass, 't');

vRecords.push_back(rec);


for (int i = 0; i < vRecords.size(); ++i)

Record &rec = vRecords[i];
std::cout << std::setw(5) << rec.id
<< std::setw(10) << rec.site
<< std::setw(30) << rec.user
<< std::setw(30) << rec.pass
<< "n";


std::cout << "n";






share|improve this answer























  • This is very clear and concise code. Thank you for the help. After reading more about structs, I agree with you.

    – okkv1747vm
    Mar 22 at 17:14


















0














if your database file is fixed format.



you can get answer through below code.



void DisplayRecords() 

vector<string> vRecords;
ifstream inFile("database.txt");

string entry;

while (inFile >> entry)
vRecords.push_back(entry);
vRecords.emplace_back("t");


for (int i = 0; i < vRecords.size(); i++)
if (i % 8 == 0)
cout << "n";

cout << setw(5) << vRecords[i];


cout << "nn";



in this code, entry variable is 1, amazon, .... in while loop.



so, i added tab between each strings.



and print new line, when i%8 == 0 is true.



i wish it was helpful to you.






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',
    autoActivateHeartbeat: false,
    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%2f55292551%2fc-read-tab-delimited-file-into-vector-and-display-it%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









    1














    A vector of individual strings really does not make a lot of sense for structured data. A vector of structs would make more sense, eg:



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

    struct Record

    std::string id;
    std::string site;
    std::string user;
    std::string pass;
    ;

    void DisplayRecords()

    std::vector<Record> vRecords;
    std::ifstream inFile("database.txt");
    std::string entry;

    while (std::getline(inFile, entry))

    std::istringstream iss(entry);

    Record rec;
    std::getline(iss, rec.id, 't');
    std::getline(iss, rec.site, 't');
    std::getline(iss, rec.user, 't');
    std::getline(iss, rec.pass, 't');

    vRecords.push_back(rec);


    for (int i = 0; i < vRecords.size(); ++i)

    Record &rec = vRecords[i];
    std::cout << std::setw(5) << rec.id
    << std::setw(10) << rec.site
    << std::setw(30) << rec.user
    << std::setw(30) << rec.pass
    << "n";


    std::cout << "n";






    share|improve this answer























    • This is very clear and concise code. Thank you for the help. After reading more about structs, I agree with you.

      – okkv1747vm
      Mar 22 at 17:14















    1














    A vector of individual strings really does not make a lot of sense for structured data. A vector of structs would make more sense, eg:



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

    struct Record

    std::string id;
    std::string site;
    std::string user;
    std::string pass;
    ;

    void DisplayRecords()

    std::vector<Record> vRecords;
    std::ifstream inFile("database.txt");
    std::string entry;

    while (std::getline(inFile, entry))

    std::istringstream iss(entry);

    Record rec;
    std::getline(iss, rec.id, 't');
    std::getline(iss, rec.site, 't');
    std::getline(iss, rec.user, 't');
    std::getline(iss, rec.pass, 't');

    vRecords.push_back(rec);


    for (int i = 0; i < vRecords.size(); ++i)

    Record &rec = vRecords[i];
    std::cout << std::setw(5) << rec.id
    << std::setw(10) << rec.site
    << std::setw(30) << rec.user
    << std::setw(30) << rec.pass
    << "n";


    std::cout << "n";






    share|improve this answer























    • This is very clear and concise code. Thank you for the help. After reading more about structs, I agree with you.

      – okkv1747vm
      Mar 22 at 17:14













    1












    1








    1







    A vector of individual strings really does not make a lot of sense for structured data. A vector of structs would make more sense, eg:



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

    struct Record

    std::string id;
    std::string site;
    std::string user;
    std::string pass;
    ;

    void DisplayRecords()

    std::vector<Record> vRecords;
    std::ifstream inFile("database.txt");
    std::string entry;

    while (std::getline(inFile, entry))

    std::istringstream iss(entry);

    Record rec;
    std::getline(iss, rec.id, 't');
    std::getline(iss, rec.site, 't');
    std::getline(iss, rec.user, 't');
    std::getline(iss, rec.pass, 't');

    vRecords.push_back(rec);


    for (int i = 0; i < vRecords.size(); ++i)

    Record &rec = vRecords[i];
    std::cout << std::setw(5) << rec.id
    << std::setw(10) << rec.site
    << std::setw(30) << rec.user
    << std::setw(30) << rec.pass
    << "n";


    std::cout << "n";






    share|improve this answer













    A vector of individual strings really does not make a lot of sense for structured data. A vector of structs would make more sense, eg:



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

    struct Record

    std::string id;
    std::string site;
    std::string user;
    std::string pass;
    ;

    void DisplayRecords()

    std::vector<Record> vRecords;
    std::ifstream inFile("database.txt");
    std::string entry;

    while (std::getline(inFile, entry))

    std::istringstream iss(entry);

    Record rec;
    std::getline(iss, rec.id, 't');
    std::getline(iss, rec.site, 't');
    std::getline(iss, rec.user, 't');
    std::getline(iss, rec.pass, 't');

    vRecords.push_back(rec);


    for (int i = 0; i < vRecords.size(); ++i)

    Record &rec = vRecords[i];
    std::cout << std::setw(5) << rec.id
    << std::setw(10) << rec.site
    << std::setw(30) << rec.user
    << std::setw(30) << rec.pass
    << "n";


    std::cout << "n";







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 22 at 5:39









    Remy LebeauRemy Lebeau

    343k19268462




    343k19268462












    • This is very clear and concise code. Thank you for the help. After reading more about structs, I agree with you.

      – okkv1747vm
      Mar 22 at 17:14

















    • This is very clear and concise code. Thank you for the help. After reading more about structs, I agree with you.

      – okkv1747vm
      Mar 22 at 17:14
















    This is very clear and concise code. Thank you for the help. After reading more about structs, I agree with you.

    – okkv1747vm
    Mar 22 at 17:14





    This is very clear and concise code. Thank you for the help. After reading more about structs, I agree with you.

    – okkv1747vm
    Mar 22 at 17:14













    0














    if your database file is fixed format.



    you can get answer through below code.



    void DisplayRecords() 

    vector<string> vRecords;
    ifstream inFile("database.txt");

    string entry;

    while (inFile >> entry)
    vRecords.push_back(entry);
    vRecords.emplace_back("t");


    for (int i = 0; i < vRecords.size(); i++)
    if (i % 8 == 0)
    cout << "n";

    cout << setw(5) << vRecords[i];


    cout << "nn";



    in this code, entry variable is 1, amazon, .... in while loop.



    so, i added tab between each strings.



    and print new line, when i%8 == 0 is true.



    i wish it was helpful to you.






    share|improve this answer



























      0














      if your database file is fixed format.



      you can get answer through below code.



      void DisplayRecords() 

      vector<string> vRecords;
      ifstream inFile("database.txt");

      string entry;

      while (inFile >> entry)
      vRecords.push_back(entry);
      vRecords.emplace_back("t");


      for (int i = 0; i < vRecords.size(); i++)
      if (i % 8 == 0)
      cout << "n";

      cout << setw(5) << vRecords[i];


      cout << "nn";



      in this code, entry variable is 1, amazon, .... in while loop.



      so, i added tab between each strings.



      and print new line, when i%8 == 0 is true.



      i wish it was helpful to you.






      share|improve this answer

























        0












        0








        0







        if your database file is fixed format.



        you can get answer through below code.



        void DisplayRecords() 

        vector<string> vRecords;
        ifstream inFile("database.txt");

        string entry;

        while (inFile >> entry)
        vRecords.push_back(entry);
        vRecords.emplace_back("t");


        for (int i = 0; i < vRecords.size(); i++)
        if (i % 8 == 0)
        cout << "n";

        cout << setw(5) << vRecords[i];


        cout << "nn";



        in this code, entry variable is 1, amazon, .... in while loop.



        so, i added tab between each strings.



        and print new line, when i%8 == 0 is true.



        i wish it was helpful to you.






        share|improve this answer













        if your database file is fixed format.



        you can get answer through below code.



        void DisplayRecords() 

        vector<string> vRecords;
        ifstream inFile("database.txt");

        string entry;

        while (inFile >> entry)
        vRecords.push_back(entry);
        vRecords.emplace_back("t");


        for (int i = 0; i < vRecords.size(); i++)
        if (i % 8 == 0)
        cout << "n";

        cout << setw(5) << vRecords[i];


        cout << "nn";



        in this code, entry variable is 1, amazon, .... in while loop.



        so, i added tab between each strings.



        and print new line, when i%8 == 0 is true.



        i wish it was helpful to you.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 22 at 4:31









        Seil ChoiSeil Choi

        324




        324



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55292551%2fc-read-tab-delimited-file-into-vector-and-display-it%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







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해