473,395 Members | 1,761 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,395 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 1506
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.