Open file fopen() and print values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #16
    Originally posted by Wolkec
    uh.. Can you tell me how to read from a file that says:
    [USER]
    Energy=50
    Health=30

    [COMPUTER]
    Energy=100
    Health=10

    Now, how do i search values for USER and display its energy and health, and same for computer
    That can be done. :))

    Refer this link

    You will get an idea of how to do this..!

    Regards

    Comment

    • Wolkec
      New Member
      • Jul 2007
      • 23

      #17
      ok, ill try :):)
      Any other way to search stuff in []

      Comment

      • Meetee
        Recognized Expert Contributor
        • Dec 2006
        • 928

        #18
        Let me be more clear.

        Code:
         (from http://www.cplusplus.com/doc/tutorial/files.html)
        // reading a text file
        #include <iostream>
        #include <fstream>
        #include <string>
        using namespace std;
        
        int main () {
          string line;
          ifstream myfile ("example.txt");
          if (myfile.is_open())
          {
            while (! myfile.eof() )
            {
              getline (myfile,line);
              /*here you can compare : line == "user" than take health and energy
              and apply logic accordingly*/
              cout << line << endl;
            }
            myfile.close();
          }
        
          else cout << "Unable to open file"; 
        
          return 0;
        }

        Comment

        • Meetee
          Recognized Expert Contributor
          • Dec 2006
          • 928

          #19
          Originally posted by Wolkec
          should i use fgetstring?
          getline (myfile,line) would work for that! where line is string read from myfile.

          Good luck :)

          PS Search on google..u will get gr8 help!

          Comment

          • Wolkec
            New Member
            • Jul 2007
            • 23

            #20
            how do i take health and energy lol :S

            Comment

            • Wolkec
              New Member
              • Jul 2007
              • 23

              #21
              and also, if i search for USER, that name might change later on, so how do i make string search for specific person

              Comment

              • Meetee
                Recognized Expert Contributor
                • Dec 2006
                • 928

                #22
                Originally posted by Wolkec
                and also, if i search for USER, that name might change later on, so how do i make string search for specific person
                So you are making file having many users? Then also this loop will iterate until u get the person u want.

                while (! myfile.eof() )
                {
                getline (myfile,line);
                cout << line << endl;
                }

                Comment

                • Meetee
                  Recognized Expert Contributor
                  • Dec 2006
                  • 928

                  #23
                  Originally posted by Wolkec
                  and also, if i search for USER, that name might change later on, so how do i make string search for specific person
                  So you are making file having many users? Then also this loop will iterate until u get the person u want.

                  while (! myfile.eof() )
                  {
                  getline (myfile,line);
                  COMPARISON
                  cout << line << endl;
                  }

                  Sorry for double posting!!!
                  Last edited by Meetee; Jul 5 '07, 10:19 AM. Reason: Apology

                  Comment

                  • Wolkec
                    New Member
                    • Jul 2007
                    • 23

                    #24
                    so, basicly, that extracts the text, should i use if to compare the string?

                    Comment

                    • Wolkec
                      New Member
                      • Jul 2007
                      • 23

                      #25
                      so, loop inside loop, or somethink else?

                      Comment

                      • Meetee
                        Recognized Expert Contributor
                        • Dec 2006
                        • 928

                        #26
                        Originally posted by Wolkec
                        so, basicly, that extracts the text, should i use if to compare the string?
                        Yes, better you start implementing the suggestions given uptil now and you will find the way!!

                        Regards

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #27
                          Originally posted by Wolkec
                          and also, if i search for USER, that name might change later on, so how do i make string search for specific person
                          You know you should really consider taking time to read a c++ tutorial on file handling. Getting the program to work might be cool but knowing how to get the program to work is even better.

                          Comment

                          • Wolkec
                            New Member
                            • Jul 2007
                            • 23

                            #28
                            How do i display the values of the users :S
                            lol :)

                            Comment

                            • Wolkec
                              New Member
                              • Jul 2007
                              • 23

                              #29
                              1 string for each type (health, energy)?

                              Comment

                              • Wolkec
                                New Member
                                • Jul 2007
                                • 23

                                #30
                                and how do i dislpay the user inside []

                                Comment

                                Working...