9

I'm trying to read a series of floats from a .out file using ifstream, but if I output them afterwards, they are not correct.

This is my input code:

float x, y, z;

ifstream table;
table.open("Resources/bones.out");
if (table.fail())
{
    cout << "Can't open table" << endl;
    return ;
}

table >> x;
table >> y;
table >> z;

cout << x << " " << y << " " << z << endl;

table.close();

My input file:

0.488454 0.510216 0.466979
0.487242 0.421347 0.472977
0.486773 0.371251 0.473103
...

Now for testing, i'm just reading the first line into x y and z and my output is

1 0 2

Any ideas as to why I'm not getting the right output?

jsan
  • 1,047
  • 8
  • 20
  • 33
  • 5
    It works fine, I just tested it. Prints "0.488454 0.510216 0.466979". So perhaps there's something wrong with the code you have not shown. Post a complete, compilable example. Also tell us your compiler and how you've invoked it to compile the code. – Christian Hackl Feb 28 '14 at 16:41
  • 4
    I'm not 100% sure `fail()` will actually tell you if you couldn't open the file. The error flags on streams are a bit fiddly. I think that the "best" way to check the state of a `[io]fstream` before using it is just `if (!myStream) // something went wrong`. – BoBTFish Feb 28 '14 at 16:43
  • 4
    `if (table >> x >> y >> z) {...} else { std::cout << "failed to read x,y,z"; }` – WhozCraig Feb 28 '14 at 16:47
  • @WhozCraig Or even better, read a whole line first with std::getline, then use a std::istringstream on the line to get the individual tokens, so that you can produce more precise error messages :) – Christian Hackl Feb 28 '14 at 16:53
  • Is it because I'm reading an .out file incorrectly, or am I right in assuming it reads identically to a .txt file? – jsan Feb 28 '14 at 17:00
  • @jsan you're assuming its reading something *at all*. – WhozCraig Feb 28 '14 at 17:02
  • What is the result of using the debugger? – Thomas Matthews Feb 28 '14 at 17:09
  • Are you sure you post the right data file? – herohuyongtao Feb 28 '14 at 18:37

2 Answers2

8
#include <fstream>
#include <strtk.hpp>   // http://www.partow.net/programming/strtk

std::string filename("Resources/bones.out");

// assuming the file is text
std::fstream fs;
fs.open(filename.c_str(), std::ios::in);

if(fs.fail())  return false;   

const char *whitespace    = " \t\r\n\f";

std::string line;
std::vector<float> floats;
std::vector<std::string> strings;
float x = 0.0, y = 0.0, z = 0.0;
std::string xs, ys, zs;

// process each line in turn
while( std::getline(fs, line ) )
{
    // Removing beginning and ending whitespace
    // can prevent parsing problems from different line endings.
    // formerly accomplished with boost::algorithm::trim(line)

    strtk::remove_leading_trailing(whitespace, line);


    // strtk::parse combines multiple delimiters in these cases

    if( strtk::parse(line, whitespace, floats ) ) 
    {
         std::cout << "succeed" << std::endl;
         // floats contains all the values on the in as floats
    }

    if( strtk::parse(line, whitespace, strings) ) 
    {
         std::cout << "succeed" << std::endl;
         // strings contains all the values on the in line as strings
    }

    if( strtk::parse(line, whitespace, x, y, z) ) 
    {
         std::cout << "succeed" << std::endl;
         // x,y,z contain the float values.  parse fails if more than 3 floats are on the line
    }

    if( strtk::parse(line, whitespace, xs, ys, zs) ) 
    {
         std::cout << "succeed" << std::endl;
         // xs,ys,zs contain the strings.  parse fails if more than 3 strings are on the line
    }
}

This is how I would solve it. You can pick your way to parse the data.

DannyK
  • 1,342
  • 16
  • 23
0

I have worked with that before and something I would do is like the code below where you read your text file line by line and use getline and a string to put the twext into variables. You don't have to ue an array as it is limited to the elements but use a vector and that way you can add dynamically.

    string xs;
    string ys;
    string zs;
    ifstream infile;
    someArray[50];
    infile.open("some file.txt");

    if (!infile)
    {
        cout << "no good file failed! \n" << endl;
    }

    while (infile.good())
    {
        for (int i = 0; i < 49; ++i)
        {
            getline(infile, xs);
            //Saves the line in xs.
                infile >> p[i].xs;

            getline(infile, ys, ',');
            infile >> p[i].ys;
            getline(infile, zs, ',');
            infile >> p[i].zs;

        }
        //infile >> p.fromFloor; */



    }

    infile.close(); 
}
Josh
  • 188
  • 1
  • 12