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

How to write integer values to a binary file?

Hi All,

There is an error here, can any body tell me how to do it right? (copy from text file to Binary file)

Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2. #include  "iostream.h"
  3. #include "fstream.h"
  4.  
  5. void main()
  6. {
  7. ifstream inFile;
  8.  
  9. inFile.open("c:\\file.txt");
  10.  
  11. FILE  *pBinaryFile;
  12. char buffer;
  13. //pTextFile = fopen("c:\\file.txt", "r");
  14. pBinaryFile = fopen("binaryfile.bin", "wb");
  15.  
  16. while(! inFile.eof())
  17. {
  18. buffer= inFile.get();
  19. fwrite(buffer, 1, 1, pBinaryFile);
  20. }
  21. fclose(inFile);
  22. fclose(pBinaryFile);
  23.  
  24. }
error C2664: 'fwrite' : cannot convert parameter 1 from 'char' to 'const void *'
May 12 '11 #1
2 12353
mac11
256 100+
The immediate answer is that fwrite wants a pointer and you've given it a char. So give it a pointer:
Expand|Select|Wrap|Line Numbers
  1. fwrite(&buffer, 1, 1, pBinaryFile);
  2.  
Further than that though, you might run into difficulty when it comes to text to binary conversion. That is, say infile.txt has a number in it "512"

You'll read the first byte "5", which is the ascii value of the character '5' and explicitly not the binary value of the number 5.

You really probably want to read the string "512" and convert that to a number 512 and write that in binary form to your file.

see http://www.parashift.com/c++-faq-lit...alization.html for a good read about text to binary conversion.

Why are you reading a text file and outputting it in "binary"? I mean, what are you going to do with it once its in binary form?
May 12 '11 #2
Thank you for your answer, and the reason why I am using binary files is that I have a big 3D Matrix of integer numbers and I wrote its values in a text file but the access to text file is slower that binary file so I want to convert it and then access it from another program as binary file and deal with it by processing its data and copying it in to several matrices(partitioning the data)
May 13 '11 #3

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...
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...
3
by: Tanuki | last post by:
Hi All: I encounter a programming problem recently. I need to read a binary file. I need to translate the binary data into useful information. I have the format at hand, like 1st byte = ID,...
18
by: David Buchan | last post by:
Hi guys, This may be a dumb question; I'm just getting into C language here. I wrote a program to unpack a binary file and write out the contents to a new file as a list of unsigned integers....
4
by: David Buchan | last post by:
Hello, I wonder if anyone could help me. I'm using vb.NET and I'd like to read a binary file, byte by byte, and then write to another file (making a duplicate, identical file). I'd then...
21
by: nicolasg | last post by:
does anyone know a module or something to convert numbers like integer to binary format ? for example I want to convert number 7 to 0111 so I can make some bitwise operations... Thanks
4
by: flgeyer | last post by:
Hello all, I'm trying to read in a binary file in C. I have the specification of the file format and everything. I can parse the file with a software called "010 Editor". This way I can check if...
7
by: zhouchengly | last post by:
when I use the following code to get integer values from file: void getIntegers() { vector<ItemType items; ifstream ifs("test.dat"); //¿¼ÂÇΪitemsÒ»´Î·ÖÅä¿Õ¼ä¡£ ItemType item ; while(...
2
by: kowndinya | last post by:
Hello, i want to read values from a binary file in VisualBasic 6.0 Initially i want to read only header of this binary file. I know this structure for this header. It is 100 bytes long and...
2
by: mohammed alhadi | last post by:
Hi All, I don't want to use the struct structure to access or represent the elements, I just want to save integer values to a binary file (now I have the data stored on a text file and the first...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
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...
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...

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.