473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

reading combined ascii & binary file in c++

4
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

Expand|Select|Wrap|Line Numbers
  1. 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

Expand|Select|Wrap|Line Numbers
  1. int location;
  2. location = data.tellg();
I then close the file and reopen it with

Expand|Select|Wrap|Line Numbers
  1. 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
Apr 13 '07 #1
6 7811
jjhall
4
I've played around with the binary mode some more, and I get confusing results. I have tried opening the file using
Expand|Select|Wrap|Line Numbers
  1. 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?
Apr 17 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 18 '07 #3
jjhall
4
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?
Apr 18 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 19 '07 #5
jjhall
4
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.
Apr 19 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
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.
Apr 20 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
4
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...
2
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...
9
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...
18
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...
8
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");
1
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....
11
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...
9
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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,...

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.