File Handling - Binary Problems

HELLO ,I AM NEW HERE!!
I MADE A PROGRAM FOR MY SCHOOL PROJECT , BUT I AM FACING A PROBLEM(MORE LIKE A DOUBT)
I USED FSTREAM.H(Turbo C++)
AND OPENED A FILE IN BINARY MODE(ios::binary)

MY PROGRAM IS WORKING FINE BUT WHEN I OPEN MY FSTREAM FILE , IT JUST SHOWS TEXTS
ISN'T IT SUPPOSED TO SHOW A BUNCH OF ZEROS AND ONES??

THANK U IN ADVANCE

FOLLOWING IS A CODE SNIPPET

I HOPE U UNDERSTAND MY QUESTION
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<conio.h>
#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
char password[50],uname[50];
void main()
{
int i;
clrscr();
fstream pass;
pass.open("pass.dat",ios::binary|ios::out);
for(i=0;i<2;++i)
{
clrscr();
cout<<"Enter Details For user number "<<i+1;
cout<<"\nUsername: ";
gets(uname);
pass.write((char*)&uname,50);
cout<<"\nPassword: ";
gets(password);
pass.write((char*)&password,50);
}
cout<<"\n Success";
pass.close();
getch();
}
welcome!

you have a question. a doubt means you don't believe something is true.
all caps is considered to be unfriendly, please don't do that :)

no, a binary file is not in binary numbers :)
a text file IS a binary file, so when you write raw text to a binary file, the result is a readable text file. Binary files are really byte-wise, not bit-wise, and the bytes that represent letters are the same in both modes in ascii, so you end up with readable text in binary files when you do what you did.

numbers are a different story: if you put 1234 into an integer and write that in binary and open in a text editor, it won't be the text for 1234, it will be gibberish bytes. try it!

what you did could produce some noise -- writing 50 bytes as you did will write past the end of the string if you only had 10 letters that you typed into the program. You can try that too, set all the values of both strings to say the integer value 230 at the start of the program. Now run it again, and you will see extra junk in your text file.

when you write a string to a text file it isnt fixed width and you don't get the junk. try that too, after the above, and the junk goes away.

does this make sense?
Last edited on
> I USED FSTREAM.H(Turbo C++)
Well your first problem I guess is your compiler became obsolete in the early 1990's when Microsoft put the last nail in DOS's coffin.

Tell your tutor to teach you something relevant to this millennium. Your TC++ skills will only get you a job in a museum.

It was rendered further obsolete in 1998 when the first C++ standard was published, and things like
#include<fstream.h>
became
#include<fstream>

> void main()
This has never been valid - even if TC++ lazily accepted it.

> gets(uname);
Nowadays, this is a sackable offence.
NEVER EVER USE GETS()

It's been removed from all the recent C and C++ standards.
Perhaps I'm wrong, but I believe that the only difference between binary and text mode in a file is the way that newlines are handled.

C++ uses the \n character to indicate a new line, but different operating systems use different character sequences. Some use \n. Some use \n\r. Some use \r. What's a programmer to do?

To handle this, you can open a file in text mode. In this mode, the C++ library translates between the OS's newline format and C++'s newline format. This happens both while reading and while writing.

If you open the file in binary mode, then you get exactly what's in the file when you read, and you put exactly what's in the buffer when you write.

You can read or write binary data to/from a text file but the library will still translate newlines, even when they occur in the middle of binary data.
I UNDERSTOOD THE DIFFERENCE , THANK YOU..
AND REALLY THANK YOU FIR MARKING OUT MY MISTAKES.
😅😅
AND I CANT DO ANYTHING ABOUT MY COMPLIER SINCE IT IS THE ONLY WHICH WILL BE TAUGHT TO US . OUR EDUCATION BOARD USES Turbo C++
Last edited on
Perhaps I'm wrong, but I believe that the only difference between binary and text mode in a file is the way that newlines are handled.

possibly. But binary may also have length differences as I noted.

file << string; //writes string.length() bytes to the file.
you can do the same with
file.write(string, string.length())
but most binary looks more like the OP's code, where you write a fixed width that is big enough to hold the data, and sometimes oversized, so that all records are same size and its easier to jump around in the file. Random length fields in binary requires delimiters and other complexity to make it work, or it can't be randomly accessed (which is also OK if you want that).

or, in other words, binary files "often" (usually) work off fixed sized "records" while "text" files often work of "lines".

DoDo0503,
you may be forced to use a compiler old enough to be your father but you can still write better code.
use int main, not void main. void main is wrong, and turbo supports the correct style.
forget conio.h exists, because it won't when you get a job.

I highly advise you to rewrite your code again at home in a free G++ environment (cygwin on windows, g++ is built into unix/linux etc) and spend some time learning <string> and <vector> c++. Without these you will be unable to function after school.

Last edited on
Easy on the caps-lock key tiger.
http://www.catb.org/~esr/faqs/smart-questions.html#writewell

More students need to complain that they're being sold a crock. Do some research, discover just how far the world has moved on since 1990 and then get all your class-mates to complain in unison.

One way or another, you're paying for this 2nd rate education.

You don't want to wait another 3 or 4 years to then find out that whatever piece of paper you have isn't worth a bean.

By all means play along, but the skills you learn outside of class will be the ones that get you a job, and more importantly, keep that job.
Topic archived. No new replies allowed.