delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/09/08/06:33:31

Newsgroups: comp.os.msdos.djgpp
From: Elliott Oti <oti AT phys DOT uu DOT nl>
Subject: Re: Problem with DJGPP
Sender: usenet AT phys DOT uu DOT nl (News system Tijgertje)
Message-ID: <Pine.OSF.3.95.980908102338.6228B-100000@ruunat.phys.uu.nl>
In-Reply-To: <MPG.105e4129bf125abf989680@news.swbell.net>
Date: Tue, 8 Sep 1998 08:30:16 GMT
References: <MPG DOT 105e4129bf125abf989680 AT news DOT swbell DOT net>
Mime-Version: 1.0
Organization: Physics and Astronomy, University of Utrecht, The Netherlands
Lines: 68
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Mon, 7 Sep 1998, Larry Ochoa wrote:


You can't read from and write to a file simultaeneously. Each time you
write to the file  the file pointer is positioned directly after the last
byte you wrote. When you call the fgets function you are scanning in
rubbish, and messing up the file pointer for the next write.
Write all the data to the file first, close the file properly or
reposition the file pointer at the beginning of the file with fseek [
fseek(file,0,SEEK_SET) ], and then read all your data back in again.
 


> Hi, 
>   When I try to compile the following program:
> 
> #include <stdio.h>
> #include <stdlib.h>
> 
> main(void)
> {
>   FILE *file;
>   char name[]= "file.txt";
>   char blah[5];
>   enum {success, fail};
>   int i, f_s = success;
> 
>   if ((file = fopen(name, "w+")) == NULL) {
>     printf("Cannot open %s.\n", name);
>     f_s = fail;
>   } else {
>     for(i=1;i<=100;i++) {
>       fprintf(file,"%d\n",i);
>       fgets(blah, 5, file);
>       printf("%s\n", blah);
>     }
>   }
>   fclose(file);
>   return f_s;
> }
> 
> 
> with djgpp, something happens to "file.txt". Instead of just putting the 
> number like this:
> 
> 1
> 2
> 3
> 4
> 
> I get this(file is attached):
> 
> 1
> ¼ €2
> „±  3
> h`< 4
> 




  Elliott Oti
  http://www.fys.ruu.nl/~oti
  "Q: What did the elephant say to the naked man?
   A: It's cute but can you pick up peanuts with it?"


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019