-4

I have found this code here on forum, it works fine but I have no clue how it works, why it work. Can someone tell me a few words about this code please? I would like to understant it and know how to use it. Or if you know some better way, how to input data into bin. file from txt file, can you give me some advice please?

#include <stdio.h>
#define BLOCK_SIZE 256

int main(void) 
{
    char buf[BLOCK_SIZE];
    size_t bytes;

while(!feof(stdin)) {
    bytes = fread(buf, 1, BLOCK_SIZE, stdin);
    fwrite(buf, 1, bytes, stdout);
}
return 0;
}

Thank you very much.

Tehryn
  • 57
  • 1
  • 10
  • 1
    Have you at least read http://www.cplusplus.com/reference/cstdio/fread/ and http://www.cplusplus.com/reference/cstdio/fwrite/? – Adrian Krupa Sep 26 '15 at 17:14
  • 3
    Some else wrote this? Tell them `while(!feof(stdin))` is wrong http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Weather Vane Sep 26 '15 at 17:16
  • Why the C++ tag when this is a C language question? – Thomas Matthews Sep 26 '15 at 17:22
  • 2
    You can't believe all the code on Stackoverflow (or the internet) is correct. Some people who accept an answer, and those who provide the answer may be just plain wrong. This is the case for this code, which seems to come from an accepted answer here: http://stackoverflow.com/a/19986676/3857942 – Michael Petch Sep 26 '15 at 17:25
  • possible duplicate of [Import text file using terminal in program C programming](http://stackoverflow.com/questions/19986036/import-text-file-using-terminal-in-program-c-programming) – divy3993 Sep 26 '15 at 17:28
  • 1
    By the way, you can remove the `argv` and `argc` tags since these variables are not used in the program and they are not part of the question. The program author could replace the `main` declaration with this one: `int main(void)`. – Thomas Matthews Sep 26 '15 at 17:31
  • +adrian krupa yes, I checked the internet first. What I dont understand is the cycle itself. I mean, when he stops. Why it is wrong althrought it works (at least it looks like it works)? – Tehryn Sep 26 '15 at 17:57
  • @Tehryn Read the linked question from Weather Vane. It tells you why it's wrong. – PC Luddite Sep 26 '15 at 18:01

1 Answers1

0

The intent of your program is to copy from standard input to standard output in blocks of 256 characters.

The fread function reads a maximum of 256 {BLOCK_SIZE} characters into a memory buffer. The function returns the actual quantity read.

The fwrite function will write the contents of the buffer to the standard output.

This operation is repeated by placing into a loop.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • But how the cycle stop? When feof(stdin)=0? And if so, where is 0 taken from? Is that NULL character or something like this? This is not what I cant find or understand. – Tehryn Sep 26 '15 at 17:51
  • @Tehryn Did you look at the documentation for `feof`? It returns `1` when it encounters the "end of file" (EOF), which in this case is the end of the standard input stream (`stdin`). It returns `0` when the stream is not at its end, so `!feof(stdin)` ends the "cycle" when the stream ends. This is all very basic C stuff, and I recommend you read up on it. – PC Luddite Sep 26 '15 at 17:59
  • 1
    @Tehryn: that is why it was recommended to read up on the standard functions. `feof` is a function and [what it returns is documented](http://en.cppreference.com/w/c/io/feof). – Jongware Sep 26 '15 at 18:00
  • @PCLuddite no it does not. `feof()` informs when an attempt has been made to read *beyond* the end of the file. The big, recurrent, mistake is thinking it tells you when the end-of-file has been **reached**. That needs a **b** in front of it: when then the end-of-file has been **breached**. – Weather Vane Sep 26 '15 at 18:49
  • @WeatherVane Agreed, I did not phrase my explanation accurately in that regard. – PC Luddite Sep 26 '15 at 18:54