0

I was tasked to make a file copy over ipc shared memory. The problem is that getc randomly yeilds EOF after 32k char.

FILE* file;
int znak;

file = fopen("./source","r");

if(file != NULL)
{
    while(feof(file) == 0)
    {
        znak = getc(file);

        if(znak != EOF)
        {
            czekaj(0);
            *adres = znak;

            sygnal(1);
        }
    }

    wait(0);        //Wait for your turn
    *adres = EOF;
    signal(1);      //Let other process go
}

Writing part as requested

int znak
FILE *plik;
plik = fopen("./plik_klient", "w");
fclose(plik);

.....



plik = fopen("./result","a");


if(plik != NULL)
{
    while(znak != EOF)
    {

        wait(1);        //Opuszczenie semafora

        znak=*adres;

        if(znak != EOF)
        {
            fputc(znak,plik);

            signal(0);
        }

    }
}

As a result of work other process reads the info and writes it into file.

-rw-r--r--. 1 ficekba inf-17   32769 01-11 21:15 result
-rw-r--r--. 1 ficekba inf-17 1000000 01-11 21:13 source

As you can see result file has exactly 32k

Haito
  • 2,039
  • 1
  • 24
  • 35

1 Answers1

1

Code uses char znak when int znak is best.

getc() returns an int in the range of unsigned char and EOF. This is typically 257 different values: [-1 ... 255]. When code read the file source and may return a 255 and assigns that to a char znak, znak has the value of -1 which matches EOF in this case. This fools code into thinking copying is done. and so may end up with a rump result file.

Use int znak.


Also open the file in binary mode is source may be a binary file.

// file = fopen("./source","r");
file = fopen("./source","rb");
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • Done, no effect – Haito Jan 11 '18 at 20:37
  • 1
    _Code uses char znak when int znak is best_ -> `int znak` is required – Ed Heal Jan 11 '18 at 20:37
  • Yeah I've modified it. – Haito Jan 11 '18 at 20:42
  • @EdHeal `int znak` is not required - even though `int` is most often the best. The value from `getc()` is in the range of `unsigned char` and `EOF`. A type of `int` or `unsigned char/unsigned` should be used. On select platforms where `UCHAR_MAX > INT_MAX`, there is a difficulty distinguishing end-of-file from character `(unsigned char) EOF` and more complex code is needed. Such code often uses `unsigned char/unsigned` for the stored return value of `getc()`. – chux - Reinstate Monica Jan 11 '18 at 20:47
  • 1
    Please `getc` returns an `int` - so Store it in an `int`. i.e. over 8 bits. `unsigned char` /`char` is (usually) 8 bits. Please tell me what platform that `UCHAR_MAX > INT_MAX` is true – Ed Heal Jan 11 '18 at 20:51
  • @EdHeal Do you agree `UCHAR_MAX > INT_MAX` is allowed by the C spec? Else such a search and post would not be convincing. – chux - Reinstate Monica Jan 11 '18 at 20:55
  • @chux I think it is not allowed by C standard. UCHAR_MAX is required to be `2^CHAR_BITS - 1`, while I am pretty sure `int` is required to be wider than `char`....yet can't find by which clause... – Eugene Sh. Jan 11 '18 at 21:05
  • @EugeneSh. I do not think C specifies `UCHAR_MAX <= INT_MAX`. Some graphics processors have reported using `signed char, short, int` with the same signed range as well as `unsigned char, unsigned short, unsigned` with a common unsigned range. On such odd platforms `UCHAR_MAX > INT_MAX`. Now if that C compliant - this remains unresolved. IAC, it is rare. – chux - Reinstate Monica Jan 11 '18 at 21:07
  • @EugeneSh. See [Is it possible to confuse EOF with a normal byte value when using fgetc?](https://stackoverflow.com/a/32641434/2410359) – chux - Reinstate Monica Jan 11 '18 at 21:16
  • @EugeneSh. This reports a sample of [all the integer types (< `long`) same size](https://www.thecodingforums.com/threads/implementations-with-char_bit-32.440062/#post-2435265). C's very broad embrace of such odd (and now quaint) platforms appears to allow `UCHAR_MAX > INT_MAX`. It is dubious if a future platform would attempt that given the common view that `UCHAR_MAX <= INT_MAX` is a truism. – chux - Reinstate Monica Jan 11 '18 at 21:39