Changes between Version 4 and Version 5 of Faq


Ignore:
Timestamp:
Sep 4, 2006, 1:29:11 AM (18 years ago)
Author:
bird
Comment:

end-of-line incomplete.

Legend:

Unmodified
Added
Removed
Modified
  • Faq

    v4 v5  
    6060Not really possible. You might use something like int my_variable asm("my_variable"); but that's hardly generic or portable. (todo: check if it's really 'asm' and not some __attribute__.)
    6161
     62
     63
     64End-Of-Line - O_BINARY / O_TEXT
     65================================
     66
     67
     68What's the story with the different EOL markers?
     69-------------------------------------------------
     70
     71See what wikipedia has to say on the subject of newline: http://en.wikipedia.org/wiki/Newline
     72
     73
     74Why doesn't read(fd, buf, size_of_file) return cbFile?
     75---------------------------------------------------
     76
     77Because you have opened the file in text mode and read will convert {{{\r\n}}} sequences to {{{\n}}}. An example (test this):
     78{{{
     79    FILE *pFile = fopen("myfile.txt", "w");
     80    fprintf(pFile, "hello\n");
     81    fclose(pFile);
     82
     83    struct stat st;
     84    stat("myfile.txt", &st);
     85    printf("st_size is %d.\n", st.st_size);
     86
     87    void *buf = alloca(st.st_size);
     88    int fd = open("myfile.txt", O_RDONLY);
     89    ssize_t rc = read(fd, buf, st.st_size);
     90    if (rc != st.st_size)
     91        printf("read returned %zd.\n", rc);
     92    close(fd);
    6293}}}
     94This should produce the output
     95{{{
     96st_size is 7.
     97read returned 6.
     98}}}
     99You will get the same result if you use {{{fseek(SEEK_END); ftell();}}} or {{{lseek(SEEK_END); tell();}}} to find the file size.
     100
     101There are two ways of fixing the problem. It depends on whether you wish to have the eol conversion or not.
     102If this is really a binary file and you certainly don't want to have eol conversion, add O_BINARY to the flags open flags ("b" in the case of fopen).
     103If this is a text file and you wish for eol conversion, you must alter the check for read success. TODO: suggest good ways of doing this.
     104
     105}}}