Urgent/Strange  fstream & binary file

Posted on 1998-07-13
Last Modified: 2010-04-01
I have a very strange problem with fstream & binary file
( see the program added )
Every time i write the number 26 to the binary file
the program treats it like an EOF & exist.

#include <fstream.h>

fstream      g_File3;

void main()
      int xx, yy, i1;"c:\\Temp\\DD_UT.dat", ios::out);

      xx = 26;
      g_File3.write((unsigned char *)&xx, sizeof(int));
      g_File3.write((unsigned char *)&xx, sizeof(int));
      g_File3.write((unsigned char *)&xx, sizeof(int));
      g_File3.close();"c:\\Temp\\DD_UT.dat", ios::in);

      for (i1 = 0; i1 < 3; i1++)
            yy = 0;
   char *)&yy, 4);
            cout << g_File3.gcount() << endl << flush;
            if (g_File3.eof() == 1)



Question by:sector
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 22

Accepted Solution

nietod earned 100 total points
ID: 1167636
a 26 is a control-Z.  In the olden days of DOS, a control-Z was used to mark the end of a text file.  Apparently you are running into that.
LVL 22

Expert Comment

ID: 1167637
I suppect the problem lies in the fact that you have opened the file in text mode  (you say in the question title that it is binary mode, but the example shows it is opened in text mode.)  top open it in binary mode use"c:\\Temp\\DD_UT.dat", ios::in | ios:binary);

I hope this helps.  Let me know if not.

Expert Comment

ID: 1167638
Sorry, but I want to say that Mr. nietod's proposal can not overcome the problem. I have compiled the codes by myself and found that the problem is the data conversion. In the original code, the int type is casted to char by explicit type cast. But the result is meaningless to do it in this way. The correct method is to use Data Conversion routine. In this case, _itoa can be used like this:
char * xxc;
_itoa (xx, xxc, 10);

save xxc to the file instead of xx.

ilanmoshe, if you find the later answer is the right solution, pls do not forget to reject the original one so that the person who provide the right one can get the points.

Expert Comment

ID: 1167639
By the way, do you have some special reason that you do not use << and >> operators?
LVL 22

Expert Comment

ID: 1167640
>> In the original code, the int type is casted to char by explicit type cast

No, a POINTER to an int is cast to a POINTER to a char.  This is standard practice in binary read/write operations.  Also _itoa() converts the data to ASCII, but the point is to write it in binary, not ASCII.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Cross compile release version of c++ program for linux 2 147
Socket Programming (Unix) 8 160
C++ mouse_event mouse look 7 136
Indy 10 not Receiving UDP broadcast 3 57
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question