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

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


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 () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


 
Reply With Quote
 
 
 
 
Chris Torek
Guest
Posts: n/a
 
      10-10-2003
In article <>
those who know me have no need of my name <not-a-real-> 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.
 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57