473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

direct access with fpos_t doesnīt work

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
Nov 13 '05 #1
11 7465
On Wed, 8 Oct 2003 03:26:26 +0200, "C. Sengstock" <sp**@sucks.com>
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.

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

}

"Nick Austin" <ni**********@nildram.co.uk> wrote in message
news:r3********************************@4ax.com...
On Wed, 8 Oct 2003 03:26:26 +0200, "C. Sengstock" <sp**@sucks.com>
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.

Nov 13 '05 #3

"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
Nov 13 '05 #4
"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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #5
On Wed, 8 Oct 2003 14:19:49 +0200, "C. Sengstock" <sp**@sucks.com>
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.
Nov 13 '05 #6

"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
Nov 13 '05 #7
in comp.lang.c i read:
while(fgets(buf, MAXLINE, infp) != NULL && !feof(infp))


the feof test is redundant.

--
a signature
Nov 13 '05 #8
nrk
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.
Nov 13 '05 #9

"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
Nov 13 '05 #10
"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() {


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[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;
}


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--) {

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #11
In article <m1*************@usa.net>
those who know me have no need of my name <no****************@usa.net> wrote:
in comp.lang.c i read:
while(fgets(buf, MAXLINE, infp) != NULL && !feof(infp))

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.
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jan Bols | last post by:
I'm using PHP 4.3 and APACHE2.0. I have a website that requires people to log in before they can download files from my website. A person is logged in if there is a session-variable $logged_in set...
3
by: Chuck | last post by:
Here is my setup. Netgear Router with a webserver and database server NAT'd behind the firewall. Microsoft Windows 2000, IIS 5 - Web Server Microsoft Windows 2000, MySQL - Database Server ...
12
by: CJM | last post by:
I'm setting up some web-based (ASP) reports that query an Access DB. I also want certain people to be able to access and manipulate the database directly. However, if the database is open in...
1
by: acrocker | last post by:
I would like to provide access to users who need to be taken directly to the relevant page without further navigation instructions. Unfortunately the page I want to access is built using a...
9
by: WalterR | last post by:
This is my first time here, so there may be earlier relevant threads of which I am unaware. Though my experience with DB2 is not extensive, such as it is was under OS/390 or equ. My main...
4
by: Jack O'Lantern | last post by:
Like video capture... When you download the Direct X 9 SDK, there are like 12 samples for progs for DirectShow in C++, but only 2 in managed languages. Can you do everything in c# you can do in...
4
by: Bo Peng | last post by:
Dear list, I am looking for a way to store a large amount of unique sequences that will be accessed by objects. The most important operations are: 1. Direct access to the sequences (from...
5
by: Rahul B | last post by:
Hi, I am having the following issues while trying to restrict the current user from creating any objects. Below is the privileges for the user and response when i try to create a table in that...
6
by: raymond_b_jimenez | last post by:
I need to download a file from an Intranet web site and feed it into a program on the PC where the browser is running. Browser is Internet Explorer. Both Javascript and VBscript are options. Which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.