473,782 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7501
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**********@n ildram.co.uk> wrote in message
news:r3******** *************** *********@4ax.c om...
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.t xt", "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********@yah oo.com) (cb********@wor ldnet.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:
-----------------------------
#dontreadthi s <<< this is the "myfile.txt " data
baud = 4
#nodata

port = 50
parity = 6
<<< till here
*** output
#dontreadthi s <<<< 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.t xt", "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.t xt", "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(bu f, 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.t xt", "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_FAILU RE);
}
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_FAILU RE); }
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

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

Similar topics

3
4059
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 to TRUE. How can I prevent people from downloading a file (f.e. myfile.doc) without being logged in when they know the direct link to the file (http://www.mysite.com/somedir/myfile.doc)? Putting the file in an obscure place by working with...
3
2218
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 What I would like to have is a web application served up off my WS (web server) and that application access my DS (db server) without
12
2383
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 Access, I cant access it via ASP: Microsoft JET Database Engine error '80004005' Could not use ''; file already in use.
1
2688
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 javascript query string within an html form. I do not have enough knowledge to decode how to build the direct URL. Here is the base page;- o http://full-time.thefa.com/gen/Index.do?league=8673133
9
3179
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 experience is IMS DB, which leads to my question. In IMS, there is an HDAM access method which can find a record without using an index as such. At initial database load, it first formats the entire space allocation into blocks of the given size. ...
4
3967
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 c++, in DirectX? How does one "convert" these c++ languages to c#? I'm not as familiar with c++ as I am with c#, and i'm not sure how I'd say "this line in c++ would be THIS line c#" and so forth.
4
3161
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 pointers stored in each object). Access through key lookup is not acceptable. 2. Given a new sequence, determine if it is already in the factory of sequences. If so, increase the reference count of the existing sequence
5
9412
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 user. Can anybody tell what is the difference between DIRECT SYSADM and Indirect SYSADM and why is Indirect SYSADM is assigned to user by default.
6
4850
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 would be my best options? Examples would be welcomed! rj
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9942
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8967
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.