Open file fopen() and print values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #31
    Originally posted by Wolkec
    and how do i dislpay the user inside []
    I suggest you read my reply #27 above and follow the advice there.
    This thread may soon be closed if you won't take the advice.

    Comment

    • Wolkec
      New Member
      • Jul 2007
      • 23

      #32
      lol, this is as far as i got :(
      plz point me to the right direction:

      Code:
      int main ()
      {
      	string line;
      	ifstream fp("c:\\Vaja.ini");
      	if (fp.is_open())
      	{
      		while (! fp.eof())
      		{
      			getline (fp, line);
                              if (line[0] == '[') {
      				// this is player
      
      			cout << line << endl;
      			}
      		}
      		fp.close();
      	}
      	else cout << "Vaja.ini doesn't exist or it cant be opened.\n";
      
      		system("PAUSE");
        return EXIT_SUCCESS;
      }
      and should i use string for each type (health, energy) or should i use loop:

      Comment

      • niskin
        New Member
        • Apr 2007
        • 109

        #33
        [QUOTE=Wolkec]lol, this is as far as i got :(
        plz point me to the right direction:

        Code:
        int main ()
        {
        	string line;
                        //code
        	if (line[0] == '[') {
                        //code
        }
        [QUOTE]

        You are treating line as if it is an array. Instead of:
        Code:
        string line;
        Put:

        Code:
        char line[20]; //20 is just an example, you can have it any size you want

        Comment

        • Wolkec
          New Member
          • Jul 2007
          • 23

          #34
          any other way? This one is hard :(

          Comment

          • Wolkec
            New Member
            • Jul 2007
            • 23

            #35
            any other good way to search for user and then type his status?

            Comment

            • Silent1Mezzo
              New Member
              • Feb 2007
              • 208

              #36
              Originally posted by Wolkec
              any other way? This one is hard :(
              Programmings not always easy :P Sometimes the best way to do something is the hardest. I would advise you first read a c++ tutorial because us just giving you the answers really isn't helping you.

              Comment

              • tnga
                New Member
                • Jul 2007
                • 27

                #37
                Originally posted by Wolkec
                any other way? This one is hard :(
                Well, it's not that hard!

                What you need to know is:

                1. How to treat text files
                Some people has already pointed you the right way. Read info on how to open a file using fopen and how to read strings using fscanf with %s into a char array.

                2. How to read values from a char array
                For example, if you want to get "USER" you should make somthing like this:

                Code:
                if (myarray[0]=="[") {
                // get the USER string here until you find "]"
                }
                Best regards,

                TNGA

                Comment

                • Wolkec
                  New Member
                  • Jul 2007
                  • 23

                  #38
                  can any1 try and type the whole code :S i tried everything and it didnt work :(:(

                  Comment

                  • tnga
                    New Member
                    • Jul 2007
                    • 27

                    #39
                    Dude, I think we can't do it... It goes against the forum rules.
                    We all want to help you, but the right way for you to learn is...learning
                    and not taking the full code!
                    Try to follow my tips, search for tutorials on how to treat files and char arrays.

                    Best regards,
                    TNGA

                    Comment

                    • Wolkec
                      New Member
                      • Jul 2007
                      • 23

                      #40
                      I said i tried every think i know, and it doesnt work :S

                      Comment

                      • tnga
                        New Member
                        • Jul 2007
                        • 27

                        #41
                        Originally posted by Wolkec
                        I said i tried every think i know, and it doesnt work :S
                        Yes, you'd better try to learn something new. How about reading some tutorials?
                        I gave you the road to track after you said it was too difficult. After that you said you've tried everything but it hasn't passed enough time for a normal being to search stuff and read.
                        So, I keep my advice on trying to search and learn that stuff.

                        Best regards,
                        TNGA

                        Comment

                        • niskin
                          New Member
                          • Apr 2007
                          • 109

                          #42
                          I've never seen this done before on thescripts, but I'm going to give you some code and you have to fill in the gaps. Each underscore represents 1 letter you need to fill in. So a series of four underscores means you need to type a four letter word:

                          Code:
                          #include <_stream> //note this underscore represents a missing letter as well!
                          #include <iostream>
                          
                          using namespace std;
                          
                          int main ()
                          {     
                              _ _ _ _ line[50];
                              _ _ _ _ _ _ _ _ fp("c:\\vaja.ini");
                              if (fp.is_open())
                              {     
                                               _ _>> line;
                                               _ _ _ _<< line << endl;
                                               system("PAUSE"); //OR cin.get(); (cin.get() is better programming unless you specifically want the user to press any key not just enter)
                              }
                              
                              if (!fp.is_open()) {
                                   _ _ _ _ << "Vaja.ini doesn't exist or it cant be opened.\n";
                                   system("PAUSE");
                                   }
                          }

                          Comment

                          • tnga
                            New Member
                            • Jul 2007
                            • 27

                            #43
                            Oh, dude, this is so cool!!!
                            That's why I love this site!
                            I think I know the answers! Lol

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #44
                              Originally posted by niskin
                              I've never seen this done before on thescripts, but I'm going to give you some code and you have to fill in the gaps. Each underscore represents 1 letter you need to fill in. So a series of four underscores means you need to type a four letter word:

                              Code:
                              #include <_stream> //note this underscore represents a missing letter as well!
                              #include <iostream>
                              
                              using namespace std;
                              
                              int main ()
                              {     
                                  _ _ _ _ line[50];
                                  _ _ _ _ _ _ _ _ fp("c:\\vaja.ini");
                                  if (fp.is_open())
                                  {     
                                                   _ _>> line;
                                                   _ _ _ _<< line << endl;
                                                   system("PAUSE"); //OR cin.get(); (cin.get() is better programming unless you specifically want the user to press any key not just enter)
                                  }
                                  
                                  if (!fp.is_open()) {
                                       _ _ _ _ << "Vaja.ini doesn't exist or it cant be opened.\n";
                                       system("PAUSE");
                                       }
                              }
                              Ha. Too many smart alecs around here

                              Comment

                              • Silent1Mezzo
                                New Member
                                • Feb 2007
                                • 208

                                #45
                                Originally posted by niskin
                                I've never seen this done before on thescripts, but I'm going to give you some code and you have to fill in the gaps. Each underscore represents 1 letter you need to fill in. So a series of four underscores means you need to type a four letter word:

                                Code:
                                #include <_stream> //note this underscore represents a missing letter as well!
                                #include <iostream>
                                
                                using namespace std;
                                
                                int main ()
                                {     
                                    _ _ _ _ line[50];
                                    _ _ _ _ _ _ _ _ fp("c:\\vaja.ini");
                                    if (fp.is_open())
                                    {     
                                                     _ _>> line;
                                                     _ _ _ _<< line << endl;
                                                     system("PAUSE"); //OR cin.get(); (cin.get() is better programming unless you specifically want the user to press any key not just enter)
                                    }
                                    
                                    if (!fp.is_open()) {
                                         _ _ _ _ << "Vaja.ini doesn't exist or it cant be opened.\n";
                                         system("PAUSE");
                                         }
                                }
                                I'd like to buy a vowel.....'a'

                                Comment

                                Working...