Solved

Urgent/Strange  fstream & binary file

Posted on 1998-07-13
5
270 Views
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;

      g_File3.open("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();

      g_File3.open("c:\\Temp\\DD_UT.dat", ios::in);

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

      }

}


0
Comment
Question by:sector
  • 3
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
Comment Utility
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.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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

g_File3.open("c:\\Temp\\DD_UT.dat", ios::in | ios:binary);

I hope this helps.  Let me know if not.
0
 
LVL 8

Expert Comment

by:trestan
Comment Utility
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.
0
 
LVL 8

Expert Comment

by:trestan
Comment Utility
By the way, do you have some special reason that you do not use << and >> operators?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> 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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now