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

D

DTO

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.
 
D

DHOLLINGSWORTH2

DTO said:
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.
 
V

Victor Bazarov

Dylan Nicholson said:
Victor Bazarov said:
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.
 
R

Richard Herring

DHOLLINGSWORTH2 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top