0

Okay so in C++ I am trying to develop a program that can take input from a file like this one:

1.3 2.4 3.8 4.0 5

6.79 7 8 9 10

23 45 5.6 2.45 2.3

and get it to output one value per line like this:

1.3
2.4
3.8
4.0
5
6.79
7
8
9
10
23
45
5.6
2.45
2.3

my program already can open read and close the file, so that's not a problem.

Community
  • 1
  • 1
  • 1
    Welcome to SO. Please read the [help] and [ask] sections on how to ask a good question. You will get a better result that way. You don't show us any code. How are we supposed to provide any meaningful answer? – OldProgrammer Feb 12 '14 at 01:45
  • 1
    If you're tring - what have you done so far? – Paweł Stawarz Feb 12 '14 at 01:45
  • 1
    Questions describing your requirements and asking someone to write the code for you or explain to you how to write the code are off-topic for Stack Overflow. Please identify a specific problem or question about programming. Include attempted solutions, an explanation of how the results differed from the expected results, and any error messages you received. Please read this: http://stackoverflow.com/help/how-to-ask – Adi Inbar Feb 12 '14 at 05:39

1 Answers1

2

When you use the >> stream operator to read into a value, it uses whitespace delimiters by default. So, your newlines and spaces will be automatically eaten up. All you need to do is read values one by one until there are none left.

Because you want the values output in the same format, and they appear to be double or float types, you should really read into a std::string. Otherwise translation issues and rounding errors can creep in.

std::ifstream infile( "values.txt" );
std::string val;

while( infile >> val ) cout << val << std::endl;
paddy
  • 60,864
  • 6
  • 61
  • 103