Using ifstream to read floats

前端 未结 2 994
忘掉有多难
忘掉有多难 2020-12-14 12:10

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:



        
2条回答
  •  爱一瞬间的悲伤
    2020-12-14 13:04

    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(); 
    }
    

提交回复
热议问题