1

I'm reading from file to string with fread function and then printing the string. I defined the string as array of chars with LONGNUM size(pre defined value). I'm reading 1 element 1 byte size each time. when printing the string with : printf("the string that read is: %s\n",buffer); the output is : the string that read is b I don't get it, why in the end of the stringi get this values? when printing the string with : printf("the string that read is : %c\n",buffer[0]); I get the write output without values. please explain me why.

the code is

#include <stdio.h>
#define LONGNUM 1

void main(int argc,char *argv[])
{

    FILE * src;
    FILE * dst;
    int num;
    char buffer[LONGNUM];

    // argc is the number of the elements in argv
    // the first argv element is the name of the program

    if(argc < 3){
        printf("prototype error <source path> <dest path>\n");
        return;
    }
    printf ("source  path is : %s \n",argv[1]);
    printf ("dest path is : %s \n",argv[2]);

    src = fopen(argv[1],"r");

    while(!(feof(src))){
        num = fread(buffer,1,1,src);
        printf("the number of items read %d\n",num);
        printf("the string that read is %s\n",buffer);
        //printf("the string that read is %c\n",buffer[0]);

    }

}

I would like you to advise me what is the write way to do it. Thanks.

Omri Ziner
  • 83
  • 1
  • 6
  • 3
    `%s` needs at least two bytes — so `LONGNUM` should be at least 2, and you'd need to specify `%1s` to read one character into the string. You could use `%c` to read one character, but your 'string' would not be null terminated. More sensibly, set `LONGNUM` to 128 or 4096 or something else big. – Jonathan Leffler Dec 26 '16 at 07:40
  • 2
    while(!(feof(src)) is wrong because it tests for something that is irrelevant and fails to test for something that you need to know. The result is that you are erroneously executing code that assumes that it is accessing data that was read successfully, when in fact this never happened. – Sumit Gemini Dec 26 '16 at 07:40
  • @SumitGemini: the standard x-ref question is [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – Jonathan Leffler Dec 26 '16 at 07:47
  • @JonathanLeffler yes, i took the reference from that link, – Sumit Gemini Dec 26 '16 at 07:50
  • 1
    My first comment is perhaps more attuned to `scanf("… %s …", …)`. When you read a single character via `fread()`, it is not a null-terminated string, so you should be printing `buffer[0]` with a `%c` format. Basically, with both `scanf()` and `printf()`, a plain `%s` means that you're dealing with a null terminated string, and an array of length 1 can only hold a null byte and not anything else. So, you have to fix the code to avoid the problems. One way is `%.1s` or `%1.1s` in the `printf()` format. – Jonathan Leffler Dec 26 '16 at 07:50
  • 2
    @SumitGemini: OK — it is best to embed the link to the question in the comment: ```[`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong)```, which appears as [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – Jonathan Leffler Dec 26 '16 at 07:51
  • @JonathanLeffler thanks, i was not aware how to embed the link. henceforward i will make sure of it. – Sumit Gemini Dec 26 '16 at 07:57

1 Answers1

1

%s specifier expects null termination string. fread does not terminate string with null, so you don't see correct output. since buffer is of length 1, %c is the correct specifier to print.

foo_l
  • 591
  • 2
  • 10
  • 28
  • 2
    a little wrong you can do `printf("%.1s", buffer)`, [reference](http://stackoverflow.com/a/3767300/7076153). @alk fixed, thanks. – Stargateur Dec 26 '16 at 11:05