Finding information in parts of txt document and storing them in variablesifstream opens files named by cin but not when pulled from an array. Any difference between cin and a regular string definition?**Compiler error** - getline() function not accepting first parameter “std:ifstream” what is my issue?mixing cin and getline input issuesgetline does not take any inputgetline(cin, str) not workingHow to fix string user input without using cin.ignore() and clear()— C++?Why won't my cout statements print after opening a textfile?I'm having trouble dynamically allocating my structIterating values into a vector while reading in a file using getlineWhy is getline (from a file) to cout (that line) not printing the line?

Query about absorption line spectra

Is it possible to use .desktop files to open local pdf files on specific pages with a browser?

MAXDOP Settings for SQL Server 2014

Does having a TSA Pre-Check member in your flight reservation increase the chances that everyone gets Pre-Check?

Did US corporations pay demonstrators in the German demonstrations against article 13?

ArcGIS not connecting to PostgreSQL db with all upper-case name

Why has "pence" been used in this sentence, not "pences"?

Do Legal Documents Require Signing In Standard Pen Colors?

Is XSS in canonical link possible?

Freedom of speech and where it applies

Flux received by a negative charge

Varistor? Purpose and principle

Why did the HMS Bounty go back to a time when whales are already rare?

Open a doc from terminal, but not by its name

Visiting the UK as unmarried couple

Greco-Roman egalitarianism

Are lightweight LN wallets vulnerable to transaction withholding?

Translation of Scottish 16th century church stained glass

How do I extrude a face to a single vertex

What linear sensor for a keyboard?

Create all possible words using a set or letters

Why did the EU agree to delay the Brexit deadline?

Can somebody explain Brexit in a few child-proof sentences?

Is it possible to have a strip of cold climate in the middle of a planet?



Finding information in parts of txt document and storing them in variables


ifstream opens files named by cin but not when pulled from an array. Any difference between cin and a regular string definition?**Compiler error** - getline() function not accepting first parameter “std:ifstream” what is my issue?mixing cin and getline input issuesgetline does not take any inputgetline(cin, str) not workingHow to fix string user input without using cin.ignore() and clear()— C++?Why won't my cout statements print after opening a textfile?I'm having trouble dynamically allocating my structIterating values into a vector while reading in a file using getlineWhy is getline (from a file) to cout (that line) not printing the line?













1















I have a large .txt document holding information. It is structured in this kind of format:




ID: 54670
Gender: M
Name: John Doe
Address: Blah Blah Blah
Email: JohnDoe@.com


I'm trying to create a program that allows one to enter the ID. The program will find the ID in the text document and then store the ID,Gender,Name,Address, etc.. in variables.
I can search for the ID and print following lines. But I'm lost on how to store particular parts of each line and only storing the info from say 5 lines after finding the ID. This is all i have so far. Any pointers would be much appreciated. I have just started teaching myself to code in the last couple weeks. So you are all such a great resource.



struct userInfo

int id;
char gender;
std::string name,address,email;
;
std::string search,line;
std::ifstream inFile;
inFile.open("iData.txt");

if (!inFile)

std::cout << "Was unable to open file!";
return 1;


std::cout << "Enter I.D" << std::endl;
std::getline(std::cin, search);

while (inFile.good())

std::getline(inFile, line);
if (line.find(search) != std::string::npos)

std::cout << line << std::endl;


inFile.close();









share|improve this question




























    1















    I have a large .txt document holding information. It is structured in this kind of format:




    ID: 54670
    Gender: M
    Name: John Doe
    Address: Blah Blah Blah
    Email: JohnDoe@.com


    I'm trying to create a program that allows one to enter the ID. The program will find the ID in the text document and then store the ID,Gender,Name,Address, etc.. in variables.
    I can search for the ID and print following lines. But I'm lost on how to store particular parts of each line and only storing the info from say 5 lines after finding the ID. This is all i have so far. Any pointers would be much appreciated. I have just started teaching myself to code in the last couple weeks. So you are all such a great resource.



    struct userInfo

    int id;
    char gender;
    std::string name,address,email;
    ;
    std::string search,line;
    std::ifstream inFile;
    inFile.open("iData.txt");

    if (!inFile)

    std::cout << "Was unable to open file!";
    return 1;


    std::cout << "Enter I.D" << std::endl;
    std::getline(std::cin, search);

    while (inFile.good())

    std::getline(inFile, line);
    if (line.find(search) != std::string::npos)

    std::cout << line << std::endl;


    inFile.close();









    share|improve this question


























      1












      1








      1








      I have a large .txt document holding information. It is structured in this kind of format:




      ID: 54670
      Gender: M
      Name: John Doe
      Address: Blah Blah Blah
      Email: JohnDoe@.com


      I'm trying to create a program that allows one to enter the ID. The program will find the ID in the text document and then store the ID,Gender,Name,Address, etc.. in variables.
      I can search for the ID and print following lines. But I'm lost on how to store particular parts of each line and only storing the info from say 5 lines after finding the ID. This is all i have so far. Any pointers would be much appreciated. I have just started teaching myself to code in the last couple weeks. So you are all such a great resource.



      struct userInfo

      int id;
      char gender;
      std::string name,address,email;
      ;
      std::string search,line;
      std::ifstream inFile;
      inFile.open("iData.txt");

      if (!inFile)

      std::cout << "Was unable to open file!";
      return 1;


      std::cout << "Enter I.D" << std::endl;
      std::getline(std::cin, search);

      while (inFile.good())

      std::getline(inFile, line);
      if (line.find(search) != std::string::npos)

      std::cout << line << std::endl;


      inFile.close();









      share|improve this question
















      I have a large .txt document holding information. It is structured in this kind of format:




      ID: 54670
      Gender: M
      Name: John Doe
      Address: Blah Blah Blah
      Email: JohnDoe@.com


      I'm trying to create a program that allows one to enter the ID. The program will find the ID in the text document and then store the ID,Gender,Name,Address, etc.. in variables.
      I can search for the ID and print following lines. But I'm lost on how to store particular parts of each line and only storing the info from say 5 lines after finding the ID. This is all i have so far. Any pointers would be much appreciated. I have just started teaching myself to code in the last couple weeks. So you are all such a great resource.



      struct userInfo

      int id;
      char gender;
      std::string name,address,email;
      ;
      std::string search,line;
      std::ifstream inFile;
      inFile.open("iData.txt");

      if (!inFile)

      std::cout << "Was unable to open file!";
      return 1;


      std::cout << "Enter I.D" << std::endl;
      std::getline(std::cin, search);

      while (inFile.good())

      std::getline(inFile, line);
      if (line.find(search) != std::string::npos)

      std::cout << line << std::endl;


      inFile.close();






      c++ variables search ifstream






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 7 at 9:13









      Some programmer dude

      303k25265426




      303k25265426










      asked Mar 7 at 9:09









      TheLawmanTheLawman

      122




      122






















          2 Answers
          2






          active

          oldest

          votes


















          1














          you can create



          map<int,list<string>>


          Later parse your file once and split each line and store those value in your map



          //Example code to fetch data from line
          std::getline(inFile, line);
          int pos = line.find(":");
          std::string val = line.substr(pos+1, line.length());


          after that you can just use



          List<string> details = map.find[id];





          share|improve this answer























          • Knew i could count on this place. I have a few books but none of them mention maps. I appreciate your help. I have some refinements to do but you have helped me take a big step forward. Much appreciated.

            – TheLawman
            Mar 8 at 4:19


















          0














          you can read the file once and save the info in map so when given id you can get the info map[id].gender






          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%2f55039914%2ffinding-information-in-parts-of-txt-document-and-storing-them-in-variables%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














            you can create



            map<int,list<string>>


            Later parse your file once and split each line and store those value in your map



            //Example code to fetch data from line
            std::getline(inFile, line);
            int pos = line.find(":");
            std::string val = line.substr(pos+1, line.length());


            after that you can just use



            List<string> details = map.find[id];





            share|improve this answer























            • Knew i could count on this place. I have a few books but none of them mention maps. I appreciate your help. I have some refinements to do but you have helped me take a big step forward. Much appreciated.

              – TheLawman
              Mar 8 at 4:19















            1














            you can create



            map<int,list<string>>


            Later parse your file once and split each line and store those value in your map



            //Example code to fetch data from line
            std::getline(inFile, line);
            int pos = line.find(":");
            std::string val = line.substr(pos+1, line.length());


            after that you can just use



            List<string> details = map.find[id];





            share|improve this answer























            • Knew i could count on this place. I have a few books but none of them mention maps. I appreciate your help. I have some refinements to do but you have helped me take a big step forward. Much appreciated.

              – TheLawman
              Mar 8 at 4:19













            1












            1








            1







            you can create



            map<int,list<string>>


            Later parse your file once and split each line and store those value in your map



            //Example code to fetch data from line
            std::getline(inFile, line);
            int pos = line.find(":");
            std::string val = line.substr(pos+1, line.length());


            after that you can just use



            List<string> details = map.find[id];





            share|improve this answer













            you can create



            map<int,list<string>>


            Later parse your file once and split each line and store those value in your map



            //Example code to fetch data from line
            std::getline(inFile, line);
            int pos = line.find(":");
            std::string val = line.substr(pos+1, line.length());


            after that you can just use



            List<string> details = map.find[id];






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 7 at 10:57









            Apoorva RajuApoorva Raju

            1318




            1318












            • Knew i could count on this place. I have a few books but none of them mention maps. I appreciate your help. I have some refinements to do but you have helped me take a big step forward. Much appreciated.

              – TheLawman
              Mar 8 at 4:19

















            • Knew i could count on this place. I have a few books but none of them mention maps. I appreciate your help. I have some refinements to do but you have helped me take a big step forward. Much appreciated.

              – TheLawman
              Mar 8 at 4:19
















            Knew i could count on this place. I have a few books but none of them mention maps. I appreciate your help. I have some refinements to do but you have helped me take a big step forward. Much appreciated.

            – TheLawman
            Mar 8 at 4:19





            Knew i could count on this place. I have a few books but none of them mention maps. I appreciate your help. I have some refinements to do but you have helped me take a big step forward. Much appreciated.

            – TheLawman
            Mar 8 at 4:19













            0














            you can read the file once and save the info in map so when given id you can get the info map[id].gender






            share|improve this answer



























              0














              you can read the file once and save the info in map so when given id you can get the info map[id].gender






              share|improve this answer

























                0












                0








                0







                you can read the file once and save the info in map so when given id you can get the info map[id].gender






                share|improve this answer













                you can read the file once and save the info in map so when given id you can get the info map[id].gender







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 7 at 10:34









                user2320641user2320641

                545




                545



























                    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%2f55039914%2ffinding-information-in-parts-of-txt-document-and-storing-them-in-variables%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

                    Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                    Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                    Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved