Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Newbie string question: how to read off the first char from a string?

Reply
Thread Tools

Newbie string question: how to read off the first char from a string?

 
 
DTO
Guest
Posts: n/a
 
      02-27-2005
Oh my god, I have seen that "int main (int argc, char** argv) many
times before, didn't know what is was.
Anyway, I put that in my program, works flawless.
Thanks Rennie, I had no idea it could be that easy.

 
Reply With Quote
 
 
 
 
DHOLLINGSWORTH2
Guest
Posts: n/a
 
      02-27-2005

"DTO" <> wrote in message
news: ups.com...
> Oh my god, I have seen that "int main (int argc, char** argv) many
> times before, didn't know what is was.
> Anyway, I put that in my program, works flawless.
> Thanks Rennie, I had no idea it could be that easy.
>


You can use that to verify that the file has not been moved or renamed since
installed. This helps a lot when trouble shooting apps on Customers
computers. Seems thay aren't satisfied leaving it alone.


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-28-2005
"Dylan Nicholson" <> wrote...
> "Victor Bazarov" <> wrote in message
> news:<nJ-dnZM-u8l3drzfRVn->...
>>
>> Second, unfortunately this is one of the serious omissions in the C++
>> streams, no setwidth or setprecision can help you read a field of any
>> *particular* width from a stream, so you have to use scanf for that or
>> resort to extracting fields into separate strings and reading them using
>> strtod or some such.
>>
>> If you doubt the statement above, I dare you to write code to extract
>> two 4-digit integers from the stream that contains '12345678' so that
>> the first one would be 1234 and the second 5678. Simple, isn't it? I
>> will even help you:
>>
>> #include <iostream>
>> #include <sstream>
>> #include <iomanip>
>> int main()
>> {
>> std::istringstream is("12345678");
>> int one, two;
>> ????

>
> char field[5] = "";


char field[5] = {}; // otherwise only the first one is guaranteed to be 0.

> is.read(field, 4);
> one = atoi(field); // may need extra header for this, or std::atoi
> is >> two;



IOW, there is no way to do it without the intervening string and additional
conversion sequence. Fixed-length fields are not easy to deal with in C++.
QED.

> [...]



 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      02-28-2005
In message <S7tUd.18663$yr.6821@okepread05>, DHOLLINGSWORTH2
<> writes
>
>"DTO" <> wrote in message
>news: oups.com...
>> Oh my god, I have seen that "int main (int argc, char** argv) many
>> times before, didn't know what is was.
>> Anyway, I put that in my program, works flawless.
>> Thanks Rennie, I had no idea it could be that easy.
>>

>
>You can use that to verify that the file has not been moved or renamed since
>installed. This helps a lot when trouble shooting apps on Customers
>computers. Seems thay aren't satisfied leaving it alone.


1. The C++ Standard gives very little guarantee about the content of
argv[0], assuming that's what you're talking about. It merely says it's
"the name used to invoke the program".

2. If your program cares that much about where the user puts it, or what
they call it, then you're using some highly non-robust techniques, and I
wouldn't care to be one of your customers.

--
Richard Herring
 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      02-28-2005
Victor Bazarov wrote:
> "Dylan Nicholson" <> wrote...
>
> > char field[5] = "";

>
> char field[5] = {}; // otherwise only the
> first one is guaranteed to be 0.


"" is equivalent to { 0 }, so the OP code does set them all to 0.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57