Number of columns in an input file!! 
Author Message
 Number of columns in an input file!!

Hi,

I would like to create a function able to read from an input file and
indicates the number of columns that file contains. The program should
read only the first line.
If the input file is as follow
120    10    20    24
100    15    39    87
etc...
Then, I want to return the int value columns equals to four. The columns
should be separated by the caracter space ' '.

Have you got some ideas how I could do the job?
Thanks a lot for your advice.



Mon, 05 Nov 2001 03:00:00 GMT  
 Number of columns in an input file!!

Quote:

>Hi,

>I would like to create a function able to read from an input file and
>indicates the number of columns that file contains. The program should
>read only the first line.
>If the input file is as follow
>120    10    20    24
>100    15    39    87
>etc...
>Then, I want to return the int value columns equals to four. The columns
>should be separated by the caracter space ' '.

>Have you got some ideas how I could do the job?
>Thanks a lot for your advice.

One approach would be to read a line ijn using fgets(). and the count the
fields in a loop with successive calls to strtok().

--
-----------------------------------------


-----------------------------------------



Mon, 05 Nov 2001 03:00:00 GMT  
 Number of columns in an input file!!

Quote:


> writes:

> >Hi,

> >I would like to create a function able to read from an input file and

> >indicates the number of columns that file contains. The program
> should
> >read only the first line.
> >If the input file is as follow
> >120    10    20    24
> >100    15    39    87
> >etc...
> >Then, I want to return the int value columns equals to four. The
> columns
> >should be separated by the caracter space ' '.

> >Have you got some ideas how I could do the job?
> >Thanks a lot for your advice.

> One approach would be to read a line ijn using fgets(). and the count
> the
> fields in a loop with successive calls to strtok().

> --
> -----------------------------------------


> -----------------------------------------

   Thanks.

How could I do that loop. Sorry, I am totally stuck

Thanks,

Cheers



Mon, 05 Nov 2001 03:00:00 GMT  
 Number of columns in an input file!!

Quote:


> gontier"
> > writes:

> > >Hi,

> > >I would like to create a function able to read from an input file
> and

> > >indicates the number of columns that file contains. The program
> > should
> > >read only the first line.
> > >If the input file is as follow
> > >120    10    20    24
> > >100    15    39    87
> > >etc...
> > >Then, I want to return the int value columns equals to four. The
> > columns
> > >should be separated by the caracter space ' '.

> > >Have you got some ideas how I could do the job?
> > >Thanks a lot for your advice.

> > One approach would be to read a line ijn using fgets(). and the
> count
> > the
> > fields in a loop with successive calls to strtok().

> > --
> > -----------------------------------------


> > -----------------------------------------

I tried this and it's working.
#define MAXCHARS 300
columns = 0;
FILE *input;
char *currtoken;
i = 0;
input = fopen ("data.dat", "r");
   fgets(line, MAXCHARS, input);
   currtoken = strtok (line, " ");
   while ( (i<8) && (currtoken != NULL) )
   {
    columns++;
    currtoken = strtok (NULL, " ");
   }
   fclose(input);

It's the best way to do it????



Mon, 05 Nov 2001 03:00:00 GMT  
 Number of columns in an input file!!

...

Quote:
>I tried this and it's working.
>#define MAXCHARS 300
>columns = 0;
>FILE *input;
>char *currtoken;
>i = 0;
>input = fopen ("data.dat", "r");
>   fgets(line, MAXCHARS, input);

Don't forget to check the return values of nboth these functions for failure.

Quote:
>   currtoken = strtok (line, " ");

Consider making that

    currtoken = strtok (line, " \n");

so that the new-line read in by gets will be considered as a separator
rather than part of a token.

Quote:
>   while ( (i<8) && (currtoken != NULL) )
>   {
>    columns++;
>    currtoken = strtok (NULL, " ");

Similarly

     currtoken = strtok (NULL, " \n");

Quote:
>   }

i never seems to get changed here. Are you sure you need i at all?
columns looks like it gives you the value you want for the test.

Quote:
>   fclose(input);

>It's the best way to do it????

One thing you get to learn in programming is that there is rarely if ever a
"best way" to do something. The general approach here is sound however,
even if a few details need tidying up.

--
-----------------------------------------


-----------------------------------------



Mon, 05 Nov 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Finding the minimum number of a txt file of numbers using input redirection

2. How to get the clicked column number or column header text in CListctl(report view)

3. how to read variable number of columns in file

4. process input file with variable number of fields

5. Can I define the number of columns?

6. limiting number of returned characters in a column

7. How can I increase columns number in DataGrid?

8. HELP: CRecordset and Oracle NUMBER column problem

9. Expert required: Solution to setting a docked toolbars width (or number of columns)

10. "Invalid column number" ODBC

11. CRecordset invalid column number

12. how to get the index number of last column displayed

 

 
Powered by phpBB® Forum Software