Is it possible to do this?

This is a discussion on Is it possible to do this? within the C++ Programming forums, part of the General Programming Boards category; Can I convert a string to a constant char? Here's what I'm trying to do... I have a ostrstream that ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    11

    Is it possible to do this?

    Can I convert a string to a constant char?

    Here's what I'm trying to do... I have a ostrstream that I input data to and I would like to take that string and make it a constat char so I can run it in system().

    Any tip would be great, thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    system ( oss.str() );
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    Thank you very much!

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    I have a file that has names listed in it. I need to figure out how to read each name and have it printed out on the screen.

    I know how to read columns of numbers one by one, but when I try to do a name it doesn't grab the whole thing. I searched the board and found a few tips, but it still takes each letter and pulls it and puts it on the screen in its own line. How can I make it grab the whole word, print it, and then go to the next?

    I think I'm missing something simple.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    If a single name contains no whitespace then the >> operator of the input stream will do what you want. If a name contains whitespace (first and last name for example), then you must use getline to read an entire line and then extract multiple names manually. It's difficult to determine what would work adequately if I don't know what the file looks like, can you post a relevant piece of the file?

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    I don't have the file on this PC, but the foloowing is what it will look like...

    Chris
    Bob
    Jane
    Jimmy
    Dave
    Amy

    So it is pretty much just a list... or it can be any list for that matter.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    In this case, you can easily read the names:
    Code:
    #include <string>
    using std::string;
    using std::getline;
    
    int main()
    {
      ...
      string s;
    
      while ( getline ( infile, s ) ) {
        // Do something with s
      }
    }
    This assumes that you have an open file called infile.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Just to point out:
    string::c_str() also returns const char*

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >string::c_str() also returns const char*
    What does that have to do with an ostrstream? The std::string class doesn't even come into the picture since ostrstream works with char pointers. ostringstream withs with std::strings.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Boy I'm way off today, and you're right on my ass prelude.
    I read the line:
    chistina15
    Can I convert a string to a constant char?
    But failed to read the following line..

    Again, pardon my zinger. I'll be sure to check with you before the next time I post.

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    Why you busting my balls LuckY? Balls, as in a man.. as in Christopher15, not Christina15. J/k, thank you both for your help.

    I worked with Prelude's code and I'm still having some mean problems with that getline command. When I comile I get all kinds of errors, I must be doing something worng. Any tips that will make it work. Arrgh!

  12. #12
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Sorry for the mistake chris ;P

    What compile errors are you having? How bout some code yo.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I'm still having some mean problems with that getline command.
    Make sure that you include <string>, that is where the std::string class as well as the getline function are declared. They are both in the std namespace as well. Post your code and the errors you are getting and we can help more.

    -Prelude
    My best code is written with the delete key.

  14. #14
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Very beside any point, but funny fact, Prelude.. I accidentally clicked on your profile and discovered your birthday is 6 days before mine.. Perhaps a completely inane fact, but interesting (to me) nonetheless.

  15. #15
    Registered User
    Join Date
    Mar 2003
    Posts
    11
    Here's the code. I'm still learning so I'm sure this isn't pretty. I been adding and subtracting parts, the part with Preludes code is mangled, i've been playing with it so ti's def not correct now.

    I'm trying to take a file, open it read off a word, then add it into the string and execute it in dos. then go back and take the next word in the file and put it into the string and execute it in dos. I know there are progs that already do this, but I want to learn how they work.

    I can make it work with a file with one word in it. It will take the file and insert it and execute, but I don't know how to read from a list.



    Code:
    #include <iostream.h>
    #include <strstrea.h>
    #include <stdlib.h>
    #include <string>
    #include <fstream.h>
    
    using std::string;
    using std::getline;
    
    
    int main()
    
    {
    	char administrator [15];  
    	cout<<"Enter username:";	
    	cin>>administrator;  
    	
    	
    	ostrstream ostr;  
    	int ip[4] = {0,0,0,0};  
    	char password [12];
    
    	cout<<"Enter IP address of target:";
    	cin>>ip[0];
    	cin>>ip[1];
    	cin>>ip[2];
    	cin>>ip[3];
    	
    	ifstream iFile;
    	iFile.open ("password.txt"); 
    	iFile>>password;  
    	if (! iFile)
    	{
           cout << "Error opening input file" << endl;
           return -1;
        }
    
    	String s;
    
    	while (getline (iFile, s));
    	
    	
    	
    	iFile.close();  
    	
    	
    
    	ostr << "net use W: \\\\" 
         << ip[0] << '.' << ip[1] << '.' << ip[2] << '.' << ip[3]
         << "\\c$ " << password << " /user:"<<administrator << ends;
       
    	cout << ostr.str() << endl;  
    	system (ostr.str());  
    
    	system ("net use W: /d");
    	
    	return 0;
    }
    Last edited by christopher15; 03-25-2003 at 10:56 AM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21