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.
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.
-PreludeCode:system ( oss.str() );
My best code is written with the delete key.
Thank you very much!
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.
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.
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.
In this case, you can easily read the names:
This assumes that you have an open file called infile.Code:#include <string> using std::string; using std::getline; int main() { ... string s; while ( getline ( infile, s ) ) { // Do something with s } }
-Prelude
My best code is written with the delete key.
Just to point out:
string::c_str() also returns const char*
>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.
Boy I'm way off today, and you're right on my ass prelude.
I read the line:
But failed to read the following line..chistina15
Can I convert a string to a constant char?
Again, pardon my zinger. I'll be sure to check with you before the next time I post.
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!
Sorry for the mistake chris ;P
What compile errors are you having? How bout some code yo.
>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.
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.
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.