Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems getting data from a file 1

Status
Not open for further replies.

markitus

Programmer
Apr 12, 2005
10
PL
hi, i want to read float data from a .txt file, and i'm having problems doing it. The file seems something like this :
Code:
233.6	163.5	291.0	228.5	252.3	259.2	232.1	318.5	251.4	
.1	60.6	736.1	354.1	106.6	1154.7	261.1	165.5	1499.4
9	344.2	289.5	153.0	305.7	288.5	242.5	275.2	289.9
.4	349.5	57.2	718.7	389.1	109.0	1141.0	497.6	152.3
5	227.6	96.8	353.2	229.4	164.0	315.8	224.2	252.6....
....
....

the number of columns in it is variable, the number of lines is always the same (45 - actually it is 15*3 , since these data are coordinates of a 3dimensional image). I got a matrix as follows to store the data :
Code:
	float coord[NUM_P][2][NUM_S]; 
//NUM_P=44,NUM_S=number of samples, it depends on the file given

so the problem comes when i'm trying to take the data of one line. I don't know how to get them, since the number of columns is not constant (NUM_S). I was trying to use 'getline' but i didn't manage to make it work properly ( it was impossible to convert char to float). My question is : how could i detect where the end of line is????

i got something like this ( i know, it's nothing for now, but i can't figure out how to get what i want, and what i need :(
Code:
rfile.open("ima1.txt");

while(!rfile.eof())
{
 rfile>>f;

}

please, help me!!!! i'll be completely grateful for life :p
 
C++ I/O is extremely clumsy and painful to use. Far easier to stay in the dark ages and do it in C. It is quick, simple and there are no mode changes in between items.

You could use getline followed by sscanf,
Code:
   howmany = sscanf ("%f %f %f %f %f", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5]);
howmany will tell you how many numbers were read in.

Alternatively, you could use strtod to convert the characters to a double. Strtod takes two parameters: the second tells you where it stopped the conversion so you can use it to start the next one.

 
Use getline to read in a string that holds a single line.

Then use a stringstream initialized with the string you read in to read each float one at a time. Note that you shouldn't use eof() like that, the example below shows a better way:
Code:
#include <iostream>
#include <sstream>

int main()
{
    char data[] = "3.5 4.6 1.2";
    std::istringstream istr(data);
    float fl;
    while (istr >> fl)
    {
        std::cout << "Float: " << fl << std::endl;
    }
    std::cout << "Done." << std::endl;
}
 
xwb said:
C++ I/O is extremely clumsy and painful to use. Far easier to stay in the dark ages and do it in C. It is quick, simple and there are no mode changes in between items.
I disagree (strongly). See my simple example above.
 
thank u both for your quick answers...
ok, as it seems simpler i'm trying yours, uolj, but i still have a doubt...

when i use getline.. how can i specify the number of characters to take???
Code:
   char c[5000];
   rfile.open("ima1.txt");
   rfile.getline(c,5000);

i was trying to use this other way of getline, but it says that 'getline' is an unspecified declaration.
Code:
CString st;
getline(rfile,st);
i'm using visual c++. what should i use then?? do i need to include any library?? every step i take i'm finding problems :(
 
hehehe.. now wouldn't that be easier in perl! i wish they would add a good regular expression/pattern matching system to c++.. in hte above code snippets you might also consider using the atof (ascii to float) function.. look on google to check the params
 
Just put the getline call inside a while loop:
Code:
while (rfile.getline(c,5000))
and inside that loop use the stringstream code I showed.

I wrote up a test function that worked as expected:
Code:
#include <iostream>
#include <fstream>
#include <sstream>

int main()
{
    char c[5000];

    std::ifstream rfile;
    rfile.open("ima1.txt");
    while (rfile.getline(c,5000))
    {
        std::cout << "\nNEW ROW\n--------" << std::endl;

        std::istringstream istr(c);
        float fl;
        while (istr >> fl)
        {
            std::cout << "Float: " << fl << std::endl;
        }
    }
}
 
the problem now is that i don't know the number of characters that i must take... as the number of columns is completely variable... there must be one way to make it work!!!
i don't know if 'c' must be 5000, or 10000 or 150000.. i can't know it...
 
There are a couple solutions. You can pick a number that is big enough to hold the longest possible line (will you really have lines longer than 150000 characters??).

The other solution, which is better in my opinion, is to use the string class. It will grow as necessary to the size of the line, and will not waste as much room.

To use a string instead of the char array, just #include <string>, change "char c[5000]" to "std::string c;" and change the getline call from "rfile.getline(c,5000)" to "std::getline(rfile, c)". There is a different version of getline for strings, which is why that must change. All the other code in my sample above would remain the same. Notice how the new versions don't specify the size of the string, it will figure itout automatically.
 
man... i think now it's working!!!! i didn't know i could use the class string that way (std)... thank u so much for ur time. now i'll try to put the floats into a matrix... i hope that won't be a problem at all.. but i don't know why i'm so clumsy since i started the project.

thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top