Thread: passing stings and intigers from a file to program

  1. #1
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87

    passing stings and intigers from a file to program

    hi guys i want to pass some strings and int values from a file to my program.. if i was using c i could do it like scanf(p,"%s %s %d %d",&str1,&str2,&num1,&num2); i dont want to get the whole line just the vallues one by one if i wanted the whole line i could do it with getline(); now that i want the values one by one how do i do it ...
    thanx

  2. #2
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main () {
      string str1,str2;
      int p1,p2;
      ifstream myfile ("book.txt");
      if (myfile.is_open())
      {
        while ( myfile.good() )
        {
    
          cout << "str1 : "  << str1 << "   str2 :  " << str2 << "   p1 :  "  << p1 << " p2: "<<p2<<endl;
    
    
      /*    getline (myfile,line);
          cout << "line : " << line << endl;
    */
        }
        myfile.close();
      }
    
      else cout << "Unable to open file";
    
      return 0;
    }
    something like this

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by wesdom
    hi guys i want to pass some strings and int values from a file to my program.. if i was using c i could do it like scanf(p,"%s %s %d %d",&str1,&str2,&num1,&num2); i dont want to get the whole line just the vallues one by one if i wanted the whole line i could do it with getline(); now that i want the values one by one how do i do it ...
    One option is to use formatted input with the overloaded operator>> such as:
    Code:
    myfile >> str1 >> str2 >> num1 >> num2
    You have probably seen such code used with std::cin.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    yes i have seen but i have a problem with both ways .... if i use myfile>>str1..... when i run my program it opens and close so i am thinking i should have done something wrong... and when i use cin>> it doesnt run it opens a black window and do nothing .... i have seen some examples on net i have written the code but it doesnt work.... btw my code is the one that i wrote plus myfile >>str1..... i just forgot to add it copy error

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by wesdom
    if i use myfile>>str1..... when i run my program it opens and close so i am thinking i should have done something wrong
    Run your program from a separate command prompt window.

    Quote Originally Posted by wesdom
    when i use cin>> it doesnt run it opens a black window and do nothing
    That is because your program is waiting for you to enter the input.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    xmmm.... i am using code Blocks i dont know if it is possible to run it to a separate window... I will try to find some more information an i will post the answer

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by wesdom
    i am using code Blocks i dont know if it is possible to run it to a separate window
    It certainly is, outside of Code Blocks. If you really want to run it within Code Blocks, then change the project preferences to get the program execution to pause at the end, or run it within a debugger with a breakpoint at the end, or insert extra code to read input at the end and hence get the program to wait for input.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Oct 2013
    Location
    greece
    Posts
    87
    thanks very much you ve been very helpful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing argument to a program
    By john7 in forum C Programming
    Replies: 2
    Last Post: 06-01-2011, 01:06 AM
  2. need help passing keystroke to program
    By snap-tech in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2008, 04:41 AM
  3. Passing PIDs to another program
    By DarrenY in forum C Programming
    Replies: 3
    Last Post: 06-02-2006, 02:04 PM
  4. passing bye parts of program
    By ReLiEnThAwK in forum C++ Programming
    Replies: 5
    Last Post: 04-06-2006, 12:02 PM
  5. Placing values inside stings
    By darfader in forum C Programming
    Replies: 2
    Last Post: 10-15-2003, 05:46 AM