473,791 Members | 3,028 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
11 7506
"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.t xt", "r");
if(!fp) perror("error opening file");
here, after detecting the error, you continue with the output.
This term should contain an exit(EXIT_FAILU RE); 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********@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 #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(b uf, 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
4060
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
3181
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
4851
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
10427
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
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...
1
10155
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9029
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...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
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
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3718
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.