www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/05/21/01:17:25

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Problem writing in a file... the sequel
Date: Wed, 20 May 1998 20:12:19 -0400
Organization: Two pounds of chaos and a pinch of salt.
Lines: 61
Message-ID: <35637163.1896@cs.com>
References: <6jvpfo$oag$1 AT news4 DOT isdnet DOT net>
NNTP-Posting-Host: ppp130.cs.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

JCx wrote:
> 
>    Hi again... as many guys told me, I've modified my program (see a few
> posts before this one), which now creates a copy of the first one with the
> string added. But the problem is: the read/write loop never ends. Any idea ?

You are never actually checking for fgets() returning NULL.  The buffer
isn't changed, it's the return value of the function you must test.

Also, I don't have the slightest clue why you are doing your string
checking in such an awful way.  Let me give you some better code:

/* addv.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *source;
FILE *destination;
char texte[80];

int main(void)
{
   printf("\nOpening file.");
   source=fopen("test.ini","r");
   if ( source == NULL )
   {
      perror( "test.ini" );
      exit( 1 );
   }
   destination=fopen("new.ini","w");
   if ( destination == NULL )
   {
      perror( "new.ini" );
      exit(1);
   }
   printf("\nFile opened.");
   while ( fgets( texte, 80, source ) ) != NULL )
   {
      fputs(texte,destination);
      if ( strstr( texte, "[drivers32]" ) )
      {
         fprintf(destination,"test string\n");
         printf("\nString added.\n");
      }
   }
   fclose(source);
   fclose(destination);
   printf("\nModification OK.\n");
   return 0;
}


-- 
---------------------------------------------------------------------
|      John M. Aldrich       | History has the relation to truth    |
|       aka Fighteer I       | that theology has to religion--i.e., |
|   mailto:fighteer AT cs DOT com   | none to speak of.                    |
| http://www.cs.com/fighteer |                    -Lazarus Long     |
---------------------------------------------------------------------

- Raw text -


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