![]() |
|
|
|
#1 |
|
Hi all,
I have a char buff[100] which contains "1234 ABCD 5678". I can't remember what function I used before to parse the string into individual variables. It was something like this: forgotenFunction(var1, var2, var3, buff, "%d %s %d") It gave me this: var1 = "1234" var2 = "ABCD" var3 = "5678" Any ideas? I will appreciate it. J. John Smith |
|
|
|
|
#2 |
|
Posts: n/a
|
Ok, I found it. It is sscan().
"John Smith" <> wrote in message news:bjdfeu$26p4$... > Hi all, > > I have a char buff[100] which contains "1234 ABCD 5678". > > I can't remember what function I used before to parse the string into > individual variables. It was something like this: > > forgotenFunction(var1, var2, var3, buff, "%d %s %d") > > It gave me this: > > var1 = "1234" > var2 = "ABCD" > var3 = "5678" > > Any ideas? I will appreciate it. > > J. > > John Smith |
|
|
|
#3 |
|
Posts: n/a
|
int sprintf(char *buffer, const char *format [,argument] ... );
"John Smith" <> wrote in message news:bjdfeu$26p4$... > Hi all, > > I have a char buff[100] which contains "1234 ABCD 5678". > > I can't remember what function I used before to parse the string into > individual variables. It was something like this: > > forgotenFunction(var1, var2, var3, buff, "%d %s %d") > > It gave me this: > > var1 = "1234" > var2 = "ABCD" > var3 = "5678" > > Any ideas? I will appreciate it. > > J. > > phykell |
|
|
|
#4 |
|
Posts: n/a
|
phykell wrote:
> int sprintf(char *buffer, const char *format [,argument] ... ); Please don't top-post. Re-read section 5 of the FAQ for posting guidelines. You might also be interested in section 3 of RFC 1855 (Netiquette Guide): http://www.parashift.com/c++-faq-lite/ http://www.dtcc.edu/cs/rfc1855.html Your answer is also wrong, and wouldn't be a very good suggestion even if it wasn't. sprintf is a very dangerous function that is very difficult to use correctly. -Kevin -- My email address is valid, but changes periodically. To contact me please use the address from a recent posting. Kevin Goodsell |
|
|
|
#5 |
|
Posts: n/a
|
"John Smith" <> wrote in message news:bjdfeu$26p4$... > Hi all, > > I have a char buff[100] which contains "1234 ABCD 5678". > > I can't remember what function I used before to parse the string into > individual variables. It was something like this: > > forgotenFunction(var1, var2, var3, buff, "%d %s %d") > > It gave me this: > > var1 = "1234" > var2 = "ABCD" > var3 = "5678" > > Any ideas? I will appreciate it. > > J. > > If you'd like to use C++ rather than C, you'd use something like; #include <iostream> #include <string> #include <sstream> int main(int argc, char* argv[]) { std::string str("1234 ABCD 5678"); std::stringstream sstr(str); int var1,var3; std::string var2; sstr >> var1 >> var2 >> var3; std::cout<<var1<<"\n"<<var2<<"\n"<<var3<<"\n"; return 0; } It is a lot safer. However one advantage in sscanf is that it handles fixed width numerics much more tidily. The stringstream operators have the notion of delimiters hardwired in (which very strong coupling to the data I find pretty weird in an OO library). The setwidth() manipulators only work if the next extraction is to a string. If you are doing lots of this stuff it is worth looking at the boost libraries for <lexical_cast> The old gcc stream class had stream::sscanf(). I think BSD still has this implementation though I stand to be corrected. Pat Pat McCormack |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| constants as of array of integers, for loops | octavsly | Hardware | 0 | 04-25-2009 11:53 AM |
| Array Programme | rits | Software | 2 | 03-04-2009 05:18 PM |
| need help Invalid length for a Base-64 char array | rrwestva | Hardware | 0 | 07-04-2006 09:36 PM |