0

Hello,

I'm trying to implement a program in C to read and display data from a text file, assign certain values to pre-defined variables and continue reading in the data.

And example format of the data file is:
---------------------------------------------------------------------------------------------
*** General Comments
*** ....
***...
....
5 125 3
.6167 8.2837 7.4813 4.9291 .4125
......and so on.
---------------------------------------------------------------------------------------------

The asteriks and comments are to be ignored by the program. The first three numbers that the program encounters (5, 125, 3) should be assigned to variables m, n and v which can be called later on. The numbers following that is the data that should also be read in and stored possibly as an array. There might be upto 10,000 data values and beyond.

With the help of the C code snippets posted here I was able to construct a rudimentary program (see code snippet, btw comments can be beside asterisks or percentage signs which is why I have it in the code).

#include <stdio.h>

int main ( void )

{
static const char filename[] = "filename";
FILE *file = fopen ( filename, "r");
fpos_t pos;

char rec [ 125 ]; /* somewhere here is the character for comment */
fgest ( rec, 125, file); /* read a rec */
fgetpos (file,  &pos);
fputs (rec, stdout ); /* write the rec */
while  ( (rec[0] =='*' || rec [0] =='%') || (rec[1] == '*' || rec [1] == '%') || rec[2] == '*' ||rec[2] == '%') || (rec[3] == '*' || rec[3] =='%') || (rec[4] == '*' || rec[4] =='%'))
{
fgets ( rec, 125,  file ); / * read a rec */
fgetpos( file, &pos);
fputs ( rec, stdout ); /* write the rec */
}
fclose ( file );
printf("%6d\n",  pos);
}

Of course all this does is read in the data and display it. I thought of using "fegt_pos" to identify the position of the line but that doesn't seem to work.

I'm a complete C newb. Algorithms for doing this was done initially in VB (which is much easier as you could reference cells in the Excel sheet that contained the data) and Fortran. I'm trying to move it over to C.

Thanks in advance for any help.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by dkalita
0

Hello,

I'm trying to implement a program in C to read and display data from a text file, assign certain values to pre-defined variables and continue reading in the data.

And example format of the data file is:
---------------------------------------------------------------------------------------------
*** General Comments
*** ....
***...
....
5 125 3
.6167 8.2837 7.4813 4.9291 .4125
......and so on.
---------------------------------------------------------------------------------------------

The asteriks and comments are to be ignored by the program. The first three numbers that the program encounters (5, 125, 3) should be assigned to variables m, n and v which can be called later on. The numbers following that is the data that should also be read in and stored possibly as an array. There might be upto 10,000 data values and beyond.

With the help of the C code snippets posted here I was able to construct a rudimentary program (see code snippet, btw comments can be beside asterisks or percentage signs which is why I have it in the code).

#include <stdio.h>

int main ( void )

{
static const char filename[] = "filename";
FILE *file = fopen ( filename, "r");
fpos_t pos;

char rec [ 125 ]; /* somewhere here is the character for comment */
fgest ( rec, 125, file); /* read a rec */
fgetpos (file,  &pos);
fputs (rec, stdout ); /* write the rec */
while  ( (rec[0] =='*' || rec [0] =='%') || (rec[1] == '*' || rec [1] == '%') || rec[2] == '*' ||rec[2] == '%') || (rec[3] == '*' || rec[3] =='%') || (rec[4] == '*' || rec[4] =='%'))
{
fgets ( rec, 125,  file ); / * read a rec */
fgetpos( file, &pos);
fputs ( rec, stdout ); /* write the rec */
}
fclose ( file );
printf("%6d\n",  pos);
}

Of course all this does is read in the data and display it. I thought of using "fegt_pos" to identify the position of the line but that doesn't seem to work.

I'm a complete C newb. Algorithms for doing this was done initially in VB (which is much easier as you could reference cells in the Excel sheet that contained the data) and Fortran. I'm trying to move it over to C.

Thanks in advance for any help.

fgetpos() doesn't return the line no..
For details about fgetpos() read its manual.
To achieve what u are trying u can keep a counter (initialized to 0) which u increment after every fgets() call and likewise u will have the current line no. in it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.