0

I have a c++ program, I would like the first argument of the main (argv[1]) to correspond to a table of float. Is it possible to do that??

I was thinking about putting in a string my floats separated with spaces (e.g. "1.12 3.23 4.32 1.1 ...") Is there a way to automatically convert such a string into a table of floats? If I understand well the atof function converts a string into a double. So it seems it could be possible to split my string using the spaces and then convert each portion using atof. This option does not seem to be very efficient to me? In addition it returns double and not float :(

So, is there a better way to pass table of float as argument of a c++ program ?

Thank you

lizzie
  • 1,506
  • 1
  • 18
  • 31
  • If you are not expecting any other parameters, then you don't need to pass the numbers as a single string through the command interpreter so you end up with it in `argv[1]`. Instead just loop through from `1` to `argc` and get one number at a time. Then you can also get the size from `argc - 1` and don't have to split the string. – Some programmer dude Aug 10 '12 at 10:45
  • If you need to pass it as a single string, then you should read the documentation for the `strtok` function. Also, I would recommend you use `strtof` instead of `atof`. – Some programmer dude Aug 10 '12 at 10:51

4 Answers4

4

A stringstream can do both the splitting at spaces and the parsing into a float.

std::stringstream ss(the_string);
std::vector<float> v(std::istream_iterator<float>(ss),
                     (std::istream_iterator<float>()));
                   // the extra parentheses here are ugly but necessary :(

How to obtain the string with the data depends on how large it is and where it is supposed to come from. Just keep in mind that in many systems the arguments passed to program are already split by spaces, putting each part in a different element of argv.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
2

Save it in a text file, and then read it from the file when your program starts. I isn't worth it to pass it as a command-line argument.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
0

The main() parameter list is as it is. You can pass the strings of your numbers as arguments to your program. The main function will have to parse its argument.

When you want to pass a space separated list of numbers in argv[1] you can use the strtok function to get the individual number strings and have to pass it to a conversion function.

When your conversion function returns a double you should check that the result can be represented by a float and cast the value to a float variable. But I would consider to use double as the internal representation.

harper
  • 13,345
  • 8
  • 56
  • 105
0

In addition to Singer's answer: The commandline should be used mainly by human, not computer. If you really need a table of values, use configuration file. You can always use human readable format for it.

Bartek Banachewicz
  • 38,596
  • 7
  • 91
  • 135