TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Lawrence Lucier
from: Mike Bilow
date: 1996-02-27 21:56:12
subject: File writes

Lawrence Lucier wrote in a message to All:

 LL>    Using the code written below, when a users name is
 LL> written to the disk file, the name element isn't cleared so
 LL> that parts of other names show up.   

 LL>    EG:   if the first name written to the file is Lawrence
 LL> Lucier and the second John Henry, then the first record will
 LL> hold the name "Lawrence Lucier", but the second record will
 LL> be "John Henry ier".

This is not an OS/2 problem, just a C problem.  I recommend C_ECHO.

 LL>     do
 LL>     {

 LL>         fread(&user, sizeof(struct username_in), 1, in);

 LL>         strcpy(mtag2.delim, "‚@");  
 LL>         strcpy(mtag2.name, user.name); 
 LL>         strcpy(mtag2.areas, "@1@2@4@NET");
 LL>         fwrite(&mtag2, sizeof(struct copy_name), 1, out);  

 LL>         if(feof(in))
 LL>             break;

 LL>     } while(!feof(in));

You should not use non-ASCII characters in strings, but code the values. 
In cases where this is ambiguous ("\x011\x012\x014NET"), you need
to use the compiler concatenation facility to remove the ambiguity.

C considers strings to be null-terminated, so copying char arrays which are
not null-terminated must use memcpy() rather than strcpy().

Your logic is also messed up, since you will do a write after getting EOF. 
On success, fread() returns the number of bytes read, which you should
check when reading from a fixed-block binary format file:

   while(sizeof(struct username_in) ==
         fread(&user, sizeof(struct username_in), 1, in)) {

      memset(mtag2, 0, sizeof(struct copy_name));    /* zero mtag2 */

      memcpy(mtag2.delim, "\x82\x01", sizeof(mtag2.delim));

      memcpy(mtag2.name, user.name, sizeof(mtag2.name));

      memcpy(mtag2.areas,
             "\x01" "1" "\x01" "2"
"\x01" "4NET",
             sizeof(mtag2.areas));

      fwrite(&mtag2, sizeof(struct copy_name), 1, out);
   }
 
-- Mike


--- 
* Origin: N1BEE BBS +1 401 944 8498 V.34/V.FC/V.32bis/HST16.8 (1:323/107)
SEEN-BY: 50/99 78/0 270/101 620/243 711/401 409 410 413 430 808 809 934 955
SEEN-BY: 712/407 515 517 628 713/888 800/1 7877/2809
@PATH: 323/107 170/400 396/1 270/101 712/515 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.