Changes between Version 4 and Version 5 of Faq
- Timestamp:
- Sep 4, 2006, 1:29:11 AM (18 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Faq
v4 v5 60 60 Not 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__.) 61 61 62 63 64 End-Of-Line - O_BINARY / O_TEXT 65 ================================ 66 67 68 What's the story with the different EOL markers? 69 ------------------------------------------------- 70 71 See what wikipedia has to say on the subject of newline: http://en.wikipedia.org/wiki/Newline 72 73 74 Why doesn't read(fd, buf, size_of_file) return cbFile? 75 --------------------------------------------------- 76 77 Because 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); 62 93 }}} 94 This should produce the output 95 {{{ 96 st_size is 7. 97 read returned 6. 98 }}} 99 You will get the same result if you use {{{fseek(SEEK_END); ftell();}}} or {{{lseek(SEEK_END); tell();}}} to find the file size. 100 101 There are two ways of fixing the problem. It depends on whether you wish to have the eol conversion or not. 102 If 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). 103 If 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 }}}