I need to open and read a file that has a combined ascii and binary format. Don't ask me why it is this way; I think it is kind of stupid, but I didn't write the program that generated the file. Anyway, there is a short header that is written in ascii, then the data is in binary. I have tried opening the file in ascii format first with - ifstream data("filename", ios::in);
Then I read to a buffer until I reach the end of the header. Next, I store the location with - int location;
-
location = data.tellg();
I then close the file and reopen it with - ifstream data_binar("filename", ios::in | ios::binary);
However, when I read the next field and use cout to print it to the screen, it is a bunch of symbols as if it is still binary. What do I need to do to recognize this as an int (or float, etc.)?
Thanks,
Jeremiah
6 7841
I've played around with the binary mode some more, and I get confusing results. I have tried opening the file using - file.open(filename, ios::in | ios::binary);
. Then I use the seekg function to move to the beginning of the binary data. (I know where the position of the binary data is explicitly.) However, when I use the stream extraction operator, or the file.read function, I get the same jibberish as when I open the file without using ios::binary. Also, it reads the binary data at the head of the file correctly, as if it is not a binary stream at all.
Has anybody dealt with files that have both ascii and binary data in them?
You need to know the format of the file. An int of 123 followedby a float of 45.67 would look like 12345.67. You can't know there is an int and a float here.
I recommend a flat file. That is, a file with all records the same length and each field with a constant width. This way you know ahead of time that there is an int followed by a float. Just read the correct number of bytes base don yur record format.
Another trick is to use a field separator: 123|45.67| etc... This way you fetch bytes until you get the separator. Then convert your buffer into the correct type by using a conversion function.
The problem is that I already have the files in a certain format, and I want to use them as they are. I know that all of the data in the binary section of the file will be doubles. The data is written with the C command fwrite, so I am not sure what type of delimiter is used, but I am assuming that it is either a space or a null character (?).
I think there is also a possibility that there is an endian issue. I don't know very much about this, but the documentation for the files indicated that there was a character written to the file to determine whether it is big or little endian. Could that affect my ability to read binary data from the file?
Absolutely.
Little endian (Windows) writes an int LSB first so when the file is read the LSB is read first and put into the low memory location.
Big endian (Unix/Linux) writes the MSB first.
You will need to interrogate the endian character in the fiule inorder to kniow how to read the file.
If the variables have separators like a space or null character, then you should be able to read them without ambiguity.
Absolutely.
Little endian (Windows) writes an int LSB first so when the file is read the LSB is read first and put into the low memory location.
Big endian (Unix/Linux) writes the MSB first.
You will need to interrogate the endian character in the fiule inorder to kniow how to read the file.
If the variables have separators like a space or null character, then you should be able to read them without ambiguity.
The file was written on an SGI machine, and I'm trying to read it on a Linux box. How do I interrogate the endian character? Is it the first character of the file?
Also, what are the LSB and MSB?
Thanks for all your help.
The endian character should be one byte. Probably a char. Endian issues do not apply if there is one byte so you can read this indicator regardless. Based on what it says you read the reat of the file either little endial or big endian.
LSB is Least Significant Byte.
MSB is Most Significant Byte.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Albert Tu |
last post by:
Hi,
I am learning and pretty new to Python and I hope your guys can give me
a quick start.
I have an about 1G-byte binary file from a flat panel x-ray detector; I
know at the beggining there is a 128-byte header and the rest of the
file is integers in 2-byte format.
What I want to do is to save the binary data into several smaller files
|
by: john smith |
last post by:
Hi, I have a file format that is going to contain some parts in ascii, and
some parts with raw binary data. Should I open this file with ios::bin or
no?
For example:
filename: a.bin
number of points = 123
@@@begin data@@@
gibberish follows.....
|
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 array). I cannot
change anything in the program but can add some features by which I
can convert this array of data into a file. The easiest thing would be
to write the data into a file (in hard disk) and use it. But I will be
working on thousands...
|
by: jeff M via .NET 247 |
last post by:
I'm still having problems reading EBCDIC files. Currently itlooks like the lower range (0 to 127) is working. I have triedthe following code pages 20284, 20924, 1140, 37, 500 and 20127.By working I get the correct answer by taking the decimal valueand using that as an index to an array that will map to thecorrect EBCDIC value in hex.
By larger values, an example would be "AA" in EBCDIC hex wouldgive me the value of 63 in decimal (ASCII) when...
|
by: John |
last post by:
Hi,
I'm a beginner is using C# and .net.
I have big legacy files that stores various values (ints, bytes, strings) and want to read them into
a C# programme so that I can store them in a database. The files are written by a late 1980's PC
Pascal programme, for which I don't have the source code. I've managed to reverse engineer the file
format.
The strings are stored as Ascii in the file, with the first byte indicating the string...
| |
by: Shalaka Joshi |
last post by:
Hi,
I have binary file say, "test.bin". I write "FF" in the file and expect
my code to read 255 for me.
char * lbuf;
int lreadBytes ;
long lData;
FILE *pFile = fopen ("c:\\testbin","rb");
|
by: JanaB |
last post by:
I am writing a code that needs to read in a binary file. At the moment I don't have to do anything with the data, I just need to view the contents to compare its structure with another binary file. Right now all I'm getting in my output is a series of numbers such as:
2-4-73015010000000245-36-12800115011500-1280-1280-12
I adapted my script from a post I saw earlier today from member UnknownBlue who was trying to do a similar thing, but...
|
by: Freddy Coal |
last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all
the file in a String.
I make this function for make that:
'--------------------------
Public Shared Function Read_bin(ByVal ruta As String)
Dim cadena As String = ""
Dim dato As Array
If File.Exists(ruta) = True Then
|
by: szclark |
last post by:
Hello, I am trying to extract information from a file I have so that I
can answer some questions on it, the problem I'm having is that it is
returning back mostly gibberish and I really don't know what I can do
to change it into proper text. Thre is a section at the bottom of my
output that is in proper english but I don't know about the rest. The
file I am reading is a.HDR file and this is the code:
#include <fstream>
#include...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |