Connecting Tech Pros Worldwide Forums | Help | Site Map

Reading from file without caring about \n

Newbie
 
Join Date: Oct 2008
Posts: 22
#1: Nov 20 '08
I have text files and want to read 1024 characters at the time, no matter if its \ns or something else.

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Nov 20 '08

re: Reading from file without caring about \n


Open your file using fopen() and the "rb" open mode and read all you want.

kind regards,

Jos
Newbie
 
Join Date: Oct 2008
Posts: 22
#3: Nov 20 '08

re: Reading from file without caring about \n


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?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Nov 20 '08

re: Reading from file without caring about \n


Quote:

Originally Posted by krreks

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
Newbie
 
Join Date: Oct 2008
Posts: 22
#5: Nov 20 '08

re: Reading from file without caring about \n


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:
Quote:
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:

Quote:
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
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Nov 20 '08

re: Reading from file without caring about \n


Quote:

Originally Posted by krreks

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
Newbie
 
Join Date: Oct 2008
Posts: 11
#7: Nov 20 '08

re: Reading from file without caring about \n


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.
Reply