473,246 Members | 1,291 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

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 1502
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

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

Similar topics

14
by: Erik Andersson | last post by:
Hi! I need to read a file (line-by-line) in reverse order, without reading the whole file into memory first. Is there an easy way of doing this? As in, is there a PHP function I could use? ...
8
by: Phil Slater | last post by:
I'm trying to process a collection of text files, reading word by word. The program run hangs whenever it encounters a word with an accented letter (like rôle or passé) - ie something that's not a...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
15
by: djj858 | last post by:
Another newbie question: How do I begin reading data, but starting from the xth line down a list? In other words, how do I skip the first lines and not read in those values?
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
16
by: Steven D'Aprano | last post by:
On Tue, 09 Sep 2008 14:59:19 -0700, castironpi wrote: You've created a solution to a problem which (probably) only affects a very small number of people, at least judging by your use-cases....
21
by: mdh | last post by:
Hi All, There is a line in the discussion of K&R (section 7-5) that says ... (end of 2nd paragraph, about the middle of the page)...in connection with the description of getc and putc...that ...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.