473,881 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C Text/Binary Files

The stdin/stdout files of C seem to be always in Text mode.

Is there any way of running a C program so that these (but especially
stdout) are in Binary mode instead?

(I'm in the process of wrapping a different language around C which doesn't
want the concept of text and binary files. But if I output a string such as
"ONE\nTWO\n ", this will behave differently between stdout and a regular
(binary) file. Examples on my OS:

"\n" Output 13,10 in text mode; 10 in binary mode
"\w" Output 13,13,10 in text mode; 13,10 in binary mode

(\w is a new escape code equivalent to \r\n). Workarounds will be awkward
(and I could never stop \n expanding to 13,10 for stdout) so would be nice
to avoid them)

--
Thanks,

Bartc
Jun 27 '08 #1
24 2866
Bartc wrote:
The stdin/stdout files of C seem to be always in Text mode.
That's what the standards say.

--
pete
Jun 27 '08 #2
Bartc wrote:
The stdin/stdout files of C seem to be always in Text mode.

Is there any way of running a C program so that these (but especially
stdout) are in Binary mode instead?
Yes, use freopen like this:

FILE *fin, *fout, *ferr;

fin = freopen(NULL, "rb", stdin);
fout = freopen(NULL, "ab", stdout);
ferr = freopen(NULL, "ab", stderr);

You could assign the return value to stdin, stdout and stderr itself,
but the standards says that they are not necessarily modifiable
lvalues. However it will probably work on most systems you would care
about.

See section 7.19.5.4 of the standard for details.

<snip>

Jun 27 '08 #3
santosh <sa*********@gm ail.comwrites:
Bartc wrote:
>The stdin/stdout files of C seem to be always in Text mode.

Is there any way of running a C program so that these (but especially
stdout) are in Binary mode instead?

Yes, use freopen like this:

FILE *fin, *fout, *ferr;

fin = freopen(NULL, "rb", stdin);
fout = freopen(NULL, "ab", stdout);
ferr = freopen(NULL, "ab", stderr);

You could assign the return value to stdin, stdout and stderr itself,
but the standards says that they are not necessarily modifiable
lvalues. However it will probably work on most systems you would care
about.
More importantly, freopen is not guaranteed to do what Bartc wants.
Thus the key information is not what the standard says but what
typical implementations do on systems where there is difference
between text and binary mode. I can give only one data point:
lcc-win32 returns NULL from the freopen call (for stdout).

--
Ben.
Jun 27 '08 #4
Ben Bacarisse wrote:
santosh <sa*********@gm ail.comwrites:
>Bartc wrote:
>>The stdin/stdout files of C seem to be always in Text mode.

Is there any way of running a C program so that these (but
especially stdout) are in Binary mode instead?

Yes, use freopen like this:

FILE *fin, *fout, *ferr;

fin = freopen(NULL, "rb", stdin);
fout = freopen(NULL, "ab", stdout);
ferr = freopen(NULL, "ab", stderr);

You could assign the return value to stdin, stdout and stderr itself,
but the standards says that they are not necessarily modifiable
lvalues. However it will probably work on most systems you would care
about.

More importantly, freopen is not guaranteed to do what Bartc wants.
Thus the key information is not what the standard says but what
typical implementations do on systems where there is difference
between text and binary mode. I can give only one data point:
lcc-win32 returns NULL from the freopen call (for stdout).
And it similarly fails for stdin too. It's perhaps surprising that it
should fail. What difficulty would an implementation like win-lcc have
with this?

Jun 27 '08 #5
>
See section 7.19.5.4 of the standard for details.

<snip>
Anyway, How can I find out standard's documents?
Jun 27 '08 #6
"Bartc" <bc@freeuk.comw rote in message
news:LC******** ***********@tex t.news.virginme dia.com...
The stdin/stdout files of C seem to be always in Text mode.
Thanks for the replies.

I think if I use exclusively "\w" for newlines (ie. "\r\n") in strings and
internal functions that generate newlines, then this will work for binary
files.

For stdout, this will generate (on my OS) 13,13,10, but for console output
that is not critical. The only problem will be when stdout is piped or
redirected to a file at the OS command line, then I will need to process the
output to take out the extra 13.

I can live with that.

I have tried freopen() as suggested, and that sort of works, but output is
then sent to a file. So this is an alternative perhaps to redirection by the
OS and the mode /will/ be binary.

--
Bartc
Jun 27 '08 #7
Ali Karaali <al****@gmail.c omwrites:
>>
See section 7.19.5.4 of the standard for details.

<snip>

Anyway, How can I find out standard's documents?
http://www.open-std.org/jtc1/sc22/wg...docs/n1256.pdf is a recent
draft of C99. The same site has lots of other useful documents.

--
Ben.
Jun 27 '08 #8
On Jun 23, 8:21 am, santosh <santosh....@gm ail.comwrote:
And it similarly fails for stdin too. It's perhaps surprising that it
should fail. What difficulty would an implementation like win-lcc have
with this?

The following works for me:
#include <stdio.h>
#include <stdlib.h>

int
main(void) {
stdout = freopen(NULL, "ab", stdout);
return 0;
}

I compiled that with gcc on Linux. It works probably because Linux/
Unix does not distinguish between text and binary mode.
Jun 27 '08 #9
santosh <sa*********@gm ail.comwrote:
Bartc wrote:
The stdin/stdout files of C seem to be always in Text mode.

Is there any way of running a C program so that these (but especially
stdout) are in Binary mode instead?

Yes, use freopen like this:

FILE *fin, *fout, *ferr;

fin = freopen(NULL, "rb", stdin);
fout = freopen(NULL, "ab", stdout);
ferr = freopen(NULL, "ab", stderr);
Note that freopen() with a null first argument is new in C99. In C89,
you had to give a new file name.

Richard
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
39536
by: Sami Viitanen | last post by:
Hello, How can I check if a file is binary or text? There was some easy way but I forgot it.. Thanks in adv.
12
4282
by: Aki Niimura | last post by:
Hello everyone, I started to use pickle to store the latest user settings for the tool I wrote. It writes out a pickled text file when it terminates and it restores the settings when it starts. It worked very nicely. However, I got a ValueError when I started the tool from Unix when I previously used the tool from Windows.
27
5052
by: Eric | last post by:
Assume that disk space is not an issue (the files will be small < 5k in general for the purpose of storing preferences) Assume that transportation to another OS may never occur. Are there any solid reasons to prefer text files over binary files files?
50
5060
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem -- the character input is a different matter, at least if I want to remain within the bounds of the standard library.
36
3682
by: Wei Su | last post by:
Hi, I have a text file abc.txt and it looks like: 12 34 56 23 45 56 33 56 78 ... .. .. ... .. .. I want to get how many rows totally in the text file, how to do this? Thanks.
16
2714
by: thenightfly | last post by:
Ok, I know all about how binary numbers translate into text characters. My question is what exactly IS a text character? Is it a bitmap?
12
5941
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: <gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish> etc. I want to remove the "g:\" from the file paths. I wrote a console app that successfully reads the file and writes a duplicate of it, but fails for some reason to do the "replacing" of the "g:\". The code...
10
3679
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in binary mode. according to me, notepad opens files and each byte of the file read, it converts that byte from ascii to its correct character and displays
4
9562
by: Florence | last post by:
How can a binary file be distinguished from a text file on Windows? Obviously I want a way that is more sophisicated that just looking at the dot extention in the filename. I want to write code that processes all text files in a directory but leaves binary files alone. -- http://www.florencesoft.com
7
3138
by: Hallvard B Furuseth | last post by:
I'm trying to clean up a program which does arithmetic on text file positions, and also reads text files in binary mode. I can't easily get rid of it all, so I'm wondering which of the following assumptions are, well, least unportable. In particular, do anyone know if there are real-life systems where the text file assumptions below don't hold? For text mode FILE*s,
0
9926
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9776
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10716
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10400
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9552
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7108
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5780
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.