468,290 Members | 1,940 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

Reading from file without caring about \n

22
I have text files and want to read 1024 characters at the time, no matter if its \ns or something else.
Nov 20 '08 #1
6 1273
JosAH
11,448 Expert 8TB
Open your file using fopen() and the "rb" open mode and read all you want.

kind regards,

Jos
Nov 20 '08 #2
krreks
22
The fread works great for ascii/text files, but is no good for binary files (e.g. compiled c programs).

Is there a way that will work for all files?
Nov 20 '08 #3
JosAH
11,448 Expert 8TB
The fread works great for ascii/text files, but is no good for binary files (e.g. compiled c programs).
Who told you that nonsense? Read my reply again (see above).

kind regards,

Jos
Nov 20 '08 #4
krreks
22
I've opened the file with mode: rb, but I'm still not able to read it properly.

Expand|Select|Wrap|Line Numbers
  1. while(!feof(input)) {
  2.     if(!ferror(input)) {
  3.         if(fread(inputBuffer, 1, MSG_MAX_LEN-1, input)) {
  4.             printf("%d chars read \n", strlen(inputBuffer));
  5.             reads++;
  6.         }
  7.     }
  8. }
I'm trying to output this:
ELF    �4 8 4  ( $ !  4 4�4�      4 4�4�     � �8' 8'    / � �� �    / � �� �    H H�H�   Q�td   R�td / � �� �   /lib/ld-linux.so.2    GNU    % + '      %   &    * # $  (   "     )         !   )     ) �K���8h B  �   6  z  P  +  s  �    <  �  �  1    )  �  �  �  �  J  �  0  C  �  �  �  �  �  b  �  �  X  ]  �  �  �  $    7   l�   � Ċ  __gmon_start__ libc.so.6 _IO_stdin_used socket strcpy exit htons sprintf fopen strncpy puts fork __stack_chk_fail listen isspace strtol feof isprint strlen strstr bind chdir memcpy fclose setsockopt malloc strcat realpath bzero getcwd fread scandir alphasort accept strcmp __libc_start_main ferror write free __lxstat GLIBC_2.4 GLIBC_2.1 GLIBC_2.3 GLIBC_...
I know there a few thousand chars in the file, but the code above output:

7 chars read
0 chars read
4 chars read
0 chars read
6 chars read
12 chars read
1 chars read
15 chars read
17 chars read
28 chars read
0 chars read
0 chars read
4 chars read
54 chars read
8 chars read
0 chars read
4 chars read
0 chars read
12 chars read
//END OF OUTPUT
Why?

K
Nov 20 '08 #5
JosAH
11,448 Expert 8TB
Why?
Because fread doesn't read C strings, it reads just bytes. Same as you want to
ignore end-of-line characters, the fread function doesn't interpret \0 characters,
i.e. it just reads them. Use the return value of the fread function itself.

kind regards,

Jos
Nov 20 '08 #6
You are reading the proper number of bytes. Don't forget that strlen terminates on a null which is represented as a binary 0. if the 5th byte of 1000 is a zero, strlen will terminate and return a value of 5. fread with the rb flag reads binary, anything, not just strings.
Nov 20 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

14 posts views Thread by Erik Andersson | last post: by
8 posts views Thread by Phil Slater | last post: by
4 posts views Thread by Oliver Knoll | last post: by
15 posts views Thread by djj858 | last post: by
6 posts views Thread by KevinD | last post: by
6 posts views Thread by Rajorshi Biswas | last post: by
6 posts views Thread by efrenba | last post: by
16 posts views Thread by Steven D'Aprano | last post: by
21 posts views Thread by mdh | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.