Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > direct access with fpos_t doesnīt work

Reply
Thread Tools

direct access with fpos_t doesnīt work

 
 
C. Sengstock
Guest
Posts: n/a
 
      10-08-2003
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[i].pos = pos;
fgets (linebuf, 255, pFile);
sscanf(linebuf, "%s", param[i].pname);
i++;
}
}

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

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


 
Reply With Quote
 
 
 
 
Nick Austin
Guest
Posts: n/a
 
      10-08-2003
On Wed, 8 Oct 2003 03:26:26 +0200, "C. Sengstock" <>
wrote:

>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[i].pos = pos;
> fgets (linebuf, 255, pFile);
> sscanf(linebuf, "%s", param[i].pname);
> i++;
> }
>}
>
>This routine reads the keywords and do a corresponding fscanf:
>***
>for(i=0; i<n; i++) {
> if(strcmp(param[i].pname, "keyword1") == 0) {
> pos = param[i].pos;
> fsetpos(pFile, &pos);
> fscanf(pFile, "%*s %d", &param1);
> }
> if(strcmp(param[i].pname, "keyword2") == 0) {
> ...
>
>Donīt know why the access through a fpos_t position doesnīt work.
>Thanks for any help, Chris


Nick.

 
Reply With Quote
 
 
 
 
hongky
Guest
Posts: n/a
 
      10-08-2003
about
http://www.eskimo.com/~scs/C-faq/q12.2.html
u can
while(fgets(buf, MAXLINE, infp) != NULL && !feof(infp))
{

}

"Nick Austin" <> wrote in message
news:...
> On Wed, 8 Oct 2003 03:26:26 +0200, "C. Sengstock" <>
> wrote:
>
> >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[i].pos = pos;
> > fgets (linebuf, 255, pFile);
> > sscanf(linebuf, "%s", param[i].pname);
> > i++;
> > }
> >}
> >
> >This routine reads the keywords and do a corresponding fscanf:
> >***
> >for(i=0; i<n; i++) {
> > if(strcmp(param[i].pname, "keyword1") == 0) {
> > pos = param[i].pos;
> > fsetpos(pFile, &pos);
> > fscanf(pFile, "%*s %d", &param1);
> > }
> > if(strcmp(param[i].pname, "keyword2") == 0) {
> > ...
> >
> >Donīt know why the access through a fpos_t position doesnīt work.
> >Thanks for any help, Chris

>
> Nick.
>



 
Reply With Quote
 
C. Sengstock
Guest
Posts: n/a
 
      10-08-2003

"Nick Austin" wrote:
> 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[i]) != 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[i]) != 0) {
printf("error setting position\n");
exit(1);
}
fgets(line, 200, fp);
printf("%s", line);
}
fclose(fp);
-----------------------------------------

Thanks, Chris


 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      10-08-2003
"C. Sengstock" wrote:
> "Nick Austin" wrote:
>
> > 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:
>

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

--
Chuck F () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
C. Sengstock
Guest
Posts: n/a
 
      10-08-2003

"CBFalconer" wrote:
> 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[i]) != 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[i]) != 0) {
printf("error setting position\n");
exit(1);
}
fgets(line, 200, fp);
printf("%s", line);
}
fclose(fp);
return 0;
}

Thanks, Chris


 
Reply With Quote
 
Nick Austin
Guest
Posts: n/a
 
      10-08-2003
On Wed, 8 Oct 2003 14:19:49 +0200, "C. Sengstock" <>
wrote:

>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[i]) != 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[i]) != 0) {
> printf("error setting position\n");
> exit(1);
> }
> fgets(line, 200, fp);
> printf("%s", line);
> }
> fclose(fp);

return 0;
}

Nick.


 
Reply With Quote
 
those who know me have no need of my name
Guest
Posts: n/a
 
      10-08-2003
in comp.lang.c i read:

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


the feof test is redundant.

--
a signature
 
Reply With Quote
 
nrk
Guest
Posts: n/a
 
      10-08-2003
C. Sengstock wrote:

>
> "CBFalconer" wrote:
>> 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[i]) != 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[i]) != 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.
 
Reply With Quote
 
C. Sengstock
Guest
Posts: n/a
 
      10-08-2003

"nrk" wrote:
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
For certain directories, protecting files from direct access that match a naming pattern OR mediating http access through my app Ken Fine ASP .Net 1 07-31-2007 06:49 AM
More: Deny direct access to jpg, swf... files, without authentication Matt ASP .Net 1 04-28-2005 12:03 AM
How to restrict direct access to JSP files, only allow access via servlet? Anan Java 8 12-08-2004 11:16 PM
Concurrent Web- and direct access to an Access DB CJM ASP General 12 07-08-2004 01:51 PM
Re: Is there a more direct way to access the Pix PDM? Tim Mavers Cisco 0 04-01-2004 02:07 PM



Advertisments