reading a column of data 
Author Message
 reading a column of data

Hello,

I have a file with columns of floats. How do I read a specific column of
data? I could do it in PERL no problem (split whitespace, line by line),
is there a comparative way to do it in C though?

Thanks
/Cody Caughlan


--



Mon, 18 Mar 2002 03:00:00 GMT  
 reading a column of data
: Hello,
:
: I have a file with columns of floats. How do I read a specific column of
: data? I could do it in PERL no problem (split whitespace, line by line),
: is there a comparative way to do it in C though?

You just discard the columns you don't need (e.g. fscanf(fp, "%*f")
discards a float).  If you don't know how many columns there are in
total, you read lines using fgets() (don't use gets()), get to the
right column with strtok(), and convert it using strtod() or sscanf().
All these fine functions are from the standard C library and should be
documented with your compiler, or in man pages in unix.

HTH,
Eric

--

 Eindhoven Univ. of Technology
 Lab. for Catalysis and Inorg. Chem. (SKA)
--



Mon, 18 Mar 2002 03:00:00 GMT  
 reading a column of data
: I have a file with columns of floats. How do I read a specific column of
: data? I could do it in PERL no problem (split whitespace, line by line),
: is there a comparative way to do it in C though?

Read about assignment suppression in the *scanf() family of functions.
For example,

int i;

scanf( %*d %*d %d %*d", &i );

parses a line containing 4 ints, it discards columns 0, 1, 3, and assigns
the int located in column 2 to the variable 'i'.

Paul

--

Director, X-ray Structural Facility |   phone: (919) 515-7362
Department of Chemistry - Box 8204  |   FAX:   (919) 515-5079
North Carolina State University     |
Raleigh, NC, 27695-8204
http://laue.chem.ncsu.edu/web/xray.welcome.html
--



Mon, 18 Mar 2002 03:00:00 GMT  
 reading a column of data


Quote:
>Hello,

>I have a file with columns of floats. How do I read a specific column of
>data? I could do it in PERL no problem (split whitespace, line by line),
>is there a comparative way to do it in C though?

Making the assumption that each line of the file has an equal number of
floats;

The following function will return a specified token from a line from
the file. You can then use sscanf to convert that number to a float or
whatever.

char* getcol(char string[], int maxcol, int col, char* buf)
{
    int i;

    /* Grab the first token. */
    buf = strtok(string, " ");
    /* Only run through the rest if needed. Note that col is zero
       based*/
    if(col > 0)
    {
        for( i = 0; i < col && i < maxcol; ++i)
        {
            buf = strtok(NULL, " ");
        }
    }
    return buf;

Quote:
}

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Bob Wightman
--



Thu, 21 Mar 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Reading selected columns of data

2. EOF ? Problem with reading columns of data

3. Problems with reading columns of data

4. Multi-column data relation (one to many)

5. column data of a datarow loss after added into datatable

6. Problem in binding one data columns to two controls

7. How to make a datagrid column read-only?

8. Fitting the columns to width of Data Grid

9. reading columns...

10. Cannot change DataType of a column once it has data

11. Validating Data During Column Changes, DataGrid

12. Using DataGridTableStyle for grd with 10 columns but only 1 clmn and cannot see any data

 

 
Powered by phpBB® Forum Software