473,407 Members | 2,312 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,407 software developers and data experts.

Reading a Binary file and outputing Ascii characters

1
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 outputting his code as hex. Can anyone help? Thanks!
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. char memblock;  
  6. ifstream::pos_type size;
  7. ifstream file;
  8.  
  9. int main()
  10.  
  11. {
  12.   ofstream out("Out.txt", ios::out);    //The imput file is very large so I'm writing the output to a file.
  13.   ofstream outChar("OutChar.txt", ios::out);    
  14.  
  15. int size, i;
  16. char n;
  17. char *chr;
  18.  
  19. file.open (mybinaryfile.uf, ios::in|ios::binary|ios::ate);
  20.  
  21. if (file.is_open())
  22. {
  23.  
  24. size = (int) file.tellg();
  25.  
  26.  
  27. file.seekg (0, ios::beg);
  28.  
  29. for (i=0; i<size; i++)
  30. {
  31. file.seekg (0, ios::cur);
  32. file.read (&memblock, 1);  
  33. out << (int) memblock;
  34. //here's where I need help.  How do I output to OutChar.txt so that the binary numbers 
  35. //are readable as ascii characters?
  36.  
  37. }
  38.  
  39. file.close();
  40. }
  41. else cout << "Unable to open file";
  42. return 0;
  43. }
  44.  
Jan 29 '07 #1
1 5342
horace1
1,510 Expert 1GB
try replacing
Expand|Select|Wrap|Line Numbers
  1. out <<  (int) memblock;
  2.  
which casts the char to an int and prints it to
Expand|Select|Wrap|Line Numbers
  1. out <<   memblock;
  2.  
which prints the character
Jan 29 '07 #2

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

Similar topics

10
by: J. Campbell | last post by:
OK...I'm in the process of learning C++. In my old (non-portable) programming days, I made use of binary files a lot...not worrying about endian issues. I'm starting to understand why C++ makes...
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...
40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
5
by: Lenard Gunda | last post by:
hi! I have the following problem. I need to read data from a TXT file our company receives. I would use StreamReader, and process it line by line using ReadLine, however, the following problem...
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...
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
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...
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...
6
by: Alexander Vasilevsky | last post by:
How do I know whether the file is text or binary? http://www.alvas.net - Audio tools for C# and VB.Net developers + Christmas discount
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
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.