
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.
--
-----------------------------------------
-----------------------------------------