direct access with fpos_t doesn´t work

C

C. Sengstock

Hi,
i want to save the keywords of an ini file in a struct, together with a
fpos_t. I think i´m right with the concept, but the access through fsetpos()
doesn´t work. The position is always wrong (except, in this example, the
first line).

My struct looks like this:
***
typedef struct {
char pname[32];
fpos_t pos;
} iniParam;

This routine reads the data and put it to "iniParam param[100]":
***
while (!feof (pFile)) {
fgetpos(pFile, &pos);
c=fgetc(pFile);
ungetc(c, pFile);
if(c == '#')
fgets (linebuf, 255, pFile);
else {
param.pos = pos;
fgets (linebuf, 255, pFile);
sscanf(linebuf, "%s", param.pname);
i++;
}
}

This routine reads the keywords and do a corresponding fscanf:
***
for(i=0; i<n; i++) {
if(strcmp(param.pname, "keyword1") == 0) {
pos = param.pos;
fsetpos(pFile, &pos);
fscanf(pFile, "%*s %d", &param1);
}
if(strcmp(param.pname, "keyword2") == 0) {
...

Don´t know why the access through a fpos_t position doesn´t work.
Thanks for any help, Chris
 
N

Nick Austin

Hi,
i want to save the keywords of an ini file in a struct, together with a
fpos_t. I think i´m right with the concept, but the access through fsetpos()
doesn´t work. The position is always wrong (except, in this example, the
first line).

It works for me with just two problems. The reading loop doesn't
detect end of file correctly as explained in the faq:

http://www.eskimo.com/~scs/C-faq/q12.2.html

Also blank lines and junk lines in the ini file result in invalid
entries in param[].

My advice is:
1. Check the return code of each library functions you call.
2. Post a complete but minimalist program, including the ini
file contents that demonstrate the problem.
My struct looks like this:
***
typedef struct {
char pname[32];
fpos_t pos;
} iniParam;

This routine reads the data and put it to "iniParam param[100]":
***
while (!feof (pFile)) {
fgetpos(pFile, &pos);
c=fgetc(pFile);
ungetc(c, pFile);
if(c == '#')
fgets (linebuf, 255, pFile);
else {
param.pos = pos;
fgets (linebuf, 255, pFile);
sscanf(linebuf, "%s", param.pname);
i++;
}
}

This routine reads the keywords and do a corresponding fscanf:
***
for(i=0; i<n; i++) {
if(strcmp(param.pname, "keyword1") == 0) {
pos = param.pos;
fsetpos(pFile, &pos);
fscanf(pFile, "%*s %d", &param1);
}
if(strcmp(param.pname, "keyword2") == 0) {
...

Don´t know why the access through a fpos_t position doesn´t work.
Thanks for any help, Chris


Nick.
 
H

hongky

about
http://www.eskimo.com/~scs/C-faq/q12.2.html
u can
while(fgets(buf, MAXLINE, infp) != NULL && !feof(infp))
{

}

Nick Austin said:
Hi,
i want to save the keywords of an ini file in a struct, together with a
fpos_t. I think i´m right with the concept, but the access through fsetpos()
doesn´t work. The position is always wrong (except, in this example, the
first line).

It works for me with just two problems. The reading loop doesn't
detect end of file correctly as explained in the faq:

http://www.eskimo.com/~scs/C-faq/q12.2.html

Also blank lines and junk lines in the ini file result in invalid
entries in param[].

My advice is:
1. Check the return code of each library functions you call.
2. Post a complete but minimalist program, including the ini
file contents that demonstrate the problem.
My struct looks like this:
***
typedef struct {
char pname[32];
fpos_t pos;
} iniParam;

This routine reads the data and put it to "iniParam param[100]":
***
while (!feof (pFile)) {
fgetpos(pFile, &pos);
c=fgetc(pFile);
ungetc(c, pFile);
if(c == '#')
fgets (linebuf, 255, pFile);
else {
param.pos = pos;
fgets (linebuf, 255, pFile);
sscanf(linebuf, "%s", param.pname);
i++;
}
}

This routine reads the keywords and do a corresponding fscanf:
***
for(i=0; i<n; i++) {
if(strcmp(param.pname, "keyword1") == 0) {
pos = param.pos;
fsetpos(pFile, &pos);
fscanf(pFile, "%*s %d", &param1);
}
if(strcmp(param.pname, "keyword2") == 0) {
...

Don´t know why the access through a fpos_t position doesn´t work.
Thanks for any help, Chris


Nick.
 
C

C. Sengstock

Nick Austin" said:
My advice is:
1. Check the return code of each library functions you call.
2. Post a complete but minimalist program, including the ini
file contents that demonstrate the problem.

This is straighter to the point. The code should save the position of a
line, and do the output through that positions. But the code doesn´t work,
the output is:
-----------------------------
#dontreadthis <<< this is the "myfile.txt" data
baud = 4
#nodata

port = 50
parity = 6
<<< till here
*** output
#dontreadthis <<<< that´s the generated output through positions
this
= 4
ta
<<<< that´s the end
------------------------------------


the code looks like this:
-------------------------------------
fpos_t position[50];
FILE* fp;
char line[200];
int i, n;

fp = fopen("myfile.txt", "r");
if(!fp) perror("error opening file");
else {
i=0;
while(!feof(fp)) {
if(fgetpos(fp, &position) != 0) {
printf("error getting position\n");
exit(1);
}
if(fgets(line, 200, fp)==NULL)
break;
printf("%s", line);
i++;
}
}
n=i;
printf("\n*** output\n");
for(i=0; i<n; i++) {
if(fsetpos(fp, &position) != 0) {
printf("error setting position\n");
exit(1);
}
fgets(line, 200, fp);
printf("%s", line);
}
fclose(fp);
 
C

CBFalconer

C. Sengstock said:
This is straighter to the point. The code should save the position
of a line, and do the output through that positions. But the code
doesn´t work, the output is:
.... snip ...

All complete C programs include an "int main(...) {" line, and
code cannot be generated outside of a function, which, in turn,
usually requires some sort of return statement.

Try again, with something that can be cut, pasted and compiled
unchanged. Do include the sample data again - you did that right.
 
C

C. Sengstock

CBFalconer said:
All complete C programs include an "int main(...) {" line, and
code cannot be generated outside of a function, which, in turn,
usually requires some sort of return statement.
Yep ... copy paste ready cody ...

#include<stdio.h>
#include<stdlib.h>
int main() {
fpos_t position[50];
FILE* fp;
char line[200];
int i, n;

fp = fopen("myfile.txt", "r");
if(!fp) perror("error opening file");
else {
i=0;
while(!feof(fp)) {
if(fgetpos(fp, &position) != 0) {
printf("error getting position\n");
exit(1);
}
if(fgets(line, 200, fp)==NULL)
break;
printf("%s", line);
i++;
}
}
n=i;
printf("\n*** output\n");
for(i=0; i<n; i++) {
if(fsetpos(fp, &position) != 0) {
printf("error setting position\n");
exit(1);
}
fgets(line, 200, fp);
printf("%s", line);
}
fclose(fp);
return 0;
}

Thanks, Chris
 
N

Nick Austin

This is straighter to the point. The code should save the position of a
line, and do the output through that positions. But the code doesn´t work,
the output is:
-----------------------------
#dontreadthis <<< this is the "myfile.txt" data
baud = 4
#nodata

port = 50
parity = 6
<<< till here
*** output
#dontreadthis <<<< that´s the generated output through positions
this
= 4
ta
<<<< that´s the end
------------------------------------

When I try the output is:

#dontreadthis
baud = 4
#nodata

port = 50
parity = 6

*** output
#dontreadthis
baud = 4
#nodata

port = 50
parity = 6

So the problem isn't in the code snippet you posted. You could
try asking again in a newsgroup for you compiler.
the code looks like this:

#include <stdio.h>

int main( void )
{
fpos_t position[50];
FILE* fp;
char line[200];
int i, n;

fp = fopen("myfile.txt", "r");
if(!fp) perror("error opening file");
else {
i=0;
while(!feof(fp)) {
if(fgetpos(fp, &position) != 0) {
printf("error getting position\n");
exit(1);
}
if(fgets(line, 200, fp)==NULL)
break;
printf("%s", line);
i++;
}
}
n=i;
printf("\n*** output\n");
for(i=0; i<n; i++) {
if(fsetpos(fp, &position) != 0) {
printf("error setting position\n");
exit(1);
}
fgets(line, 200, fp);
printf("%s", line);
}
fclose(fp);

return 0;
}

Nick.
 
N

nrk

C. Sengstock said:
CBFalconer said:
All complete C programs include an "int main(...) {" line, and
code cannot be generated outside of a function, which, in turn,
usually requires some sort of return statement.
Yep ... copy paste ready cody ...

#include<stdio.h>
#include<stdlib.h>
int main() {
fpos_t position[50];
FILE* fp;
char line[200];
int i, n;

fp = fopen("myfile.txt", "r");
if(!fp) perror("error opening file");
You most probably want to exit as well at this point (or atleast avoid
reading from fp later on down below, by initializing i to 0).
else {
i=0;
while(!feof(fp)) {
if(fgetpos(fp, &position) != 0) {
printf("error getting position\n");
exit(1);

exit(EXIT_FAILURE);
}
if(fgets(line, 200, fp)==NULL)
I would personally prefer to use sizeof(line) instead in this case.
break;
printf("%s", line);
i++;
}
}
n=i;
printf("\n*** output\n");
for(i=0; i<n; i++) {
if(fsetpos(fp, &position) != 0) {
printf("error setting position\n");
exit(1); exit(EXIT_FAILURE);
}
fgets(line, 200, fp);

See above.
printf("%s", line);
}
fclose(fp);
return 0;
}

Thanks, Chris

What exactly is the problem you are encountering with this code? Are you
100% certain that none of your lines are more than 200 characters in
length?

-nrk.
 
C

C. Sengstock

nrk said:
What exactly is the problem you are encountering with this code? Are you
100% certain that none of your lines are more than 200 characters in
length?
Yep, size of line isn´t greater than 200. I really think the problem has to
do with the compiler (tcc or bcc32, both make this failure!). I tried a lot
of tests with fgetpos() and fsetpos(), but they all went wrong!

Chris
 
C

CBFalconer

C. Sengstock said:
Yep ... copy paste ready cody ...

#include<stdio.h>
#include<stdlib.h>
int main() {

int main(void) is better, but only a nit.
fpos_t position[50];
FILE* fp;
char line[200];
int i, n;

fp = fopen("myfile.txt", "r");
if(!fp) perror("error opening file");

here, after detecting the error, you continue with the output.
This term should contain an exit(EXIT_FAILURE); statement.
else {
i=0;
while(!feof(fp)) {
if(fgetpos(fp, &position) != 0) {
printf("error getting position\n");
exit(1);
}
if(fgets(line, 200, fp)==NULL)
break;
printf("%s", line);
i++;
}
}
n=i;
printf("\n*** output\n");
for(i=0; i<n; i++) {
if(fsetpos(fp, &position) != 0) {
printf("error setting position\n");
exit(1);
}
fgets(line, 200, fp);
printf("%s", line);
}
fclose(fp);
return 0;
}


Now I pretty well have to look at it. :) So I took your message,
snipped off the beginning and end, and:

[1] c:\c\junk>gcc -o fpos.exe fpos.c

[1] c:\c\junk>type fpostst.txt
#dontreadthis
baud = 4
#nodata

port = 50
parity = 6

[1] c:\c\junk>fpos
#dontreadthis
baud = 4
#nodata

port = 50
parity = 6

*** output
#dontreadthis
baud = 4
#nodata

port = 50
parity = 6

This is the results here, after changing the input file name. It
seems to do what you wanted, so you should look to your system.

Now you see how much easier it is for people to help if you give
them something to work with. I could criticise the lack of blanks
around tokens, but I won't :)

One thought strikes me - does your test file have its last line
properly terminated with a \n? If not the funny termination
conditions might be causing the problem.

Of course the reread tells you nothing, since it is done in
sequence and thus the fsetpos doesn't have any work to do. Try
doing the reads in reverse order in the output section, i.e:

for (i = n-1; i >= 0; i--) {
 
C

Chris Torek

in comp.lang.c i read:
the feof test is redundant.

Actually, it is not redundant: it is wrong. :)

If fgets() returns NULL, we know that getc(infp) -- or some equivalent
operation -- returned EOF, but there are *two* reasons that getc()
might return EOF. One is "end of file", which would also set the
EOF indicator on the stream, making the feof() call redundant in
this (usual) case. The other, however, is "error reading stream".
You might, for instance, have this occur when reading from a bad
floppy disk. In this case the EOF indicator is *not* set on the
stream. Instead, the error indicator -- the thing tested by
ferror(infp) -- is set.

Thus, when reading from a bad floppy, the loop:

while (fgets(buf, MAXLINE, infp) != NULL && !feof(infp))

may run forever, calling fgets(), getting NULL due to ferror(infp),
and then seeing that feof(infp) is still 0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top