Project

General

Profile

Bug #8084

fseek/fwrite behavior is unexpected

Added by Alexander Pyhalov almost 3 years ago. Updated almost 3 years ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
-
Start date:
2017-04-19
Due date:
% Done:

0%

Estimated time:
Difficulty:
Medium
Tags:
needs-triage

Description

The following program behaves differently on illumos (as of April 8, 2017 , illumos-471a88e499), Solaris 11.3 and GNU/Linux.

#include <stdio.h>

int main()
{
   FILE *fl;
   char buffer[10];

   fl=fopen(".test","a+b");
   if(fl) {
      fseek(fl, 0, SEEK_SET);
      fread(buffer,sizeof(char), 3, fl);
      fwrite("0", 1, 1, fl);
      fseek(fl, 0, SEEK_END);
      fwrite("test", 4, 1, fl);
      fclose(fl);

   } else {
      printf("Can't open file .test\n");
   }
   return 0;
}

After program finishes, it creates '.test' file, which contain 'test' on illumos and '0test' on other systems (Solaris, Linux, MacOS X).
If I change it to

#include <stdio.h>

int main()
{
   FILE *fl;
   char buffer[10];

   fl=fopen(".test","a+b");
   if(fl) {
      fseek(fl, 0, SEEK_SET);
      fread(buffer,sizeof(char), 3, fl);
      fwrite("0", 1, 1, fl);
      fflush(fl); /* Inserted flush here */
      fseek(fl, 0, SEEK_END);
      fwrite("test", 4, 1, fl);
      fclose(fl);

   } else {
      printf("Can't open file .test\n");
   }
   return 0;
}

it seems to behave the same on all operating systems, creating file with '0test' contents.

History

#1

Updated by Alexander Pyhalov almost 3 years ago

  • Subject changed from feek/fwrite behavior is unexpected to fseek/fwrite behavior is unexpected

Also available in: Atom PDF