473,382 Members | 1,752 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,382 software developers and data experts.

How to Read from .txt file & Write into .bin file w/o weird char at the end

I am trying to create a .bin file of records from a .txt file. Initially the records are stored in a text file and I want to Read from the .txt file and Write to the .bin file.

This is the C++ code. The result write exactly but it has funny-looking unwanted characters attached at the end of the .bin file. How do I fix it?

#include <iostream>
#include <fstream>
using namespace std;

struct details {
int id;
string name;
int age;
string address;
string sport;
};

const int SIZE=100;

int main( int argc, char* argv[]) {
if( argc!=3) {
cout<<"illegal \n";
system ("pause");
return 1;
}
details x[SIZE];

ofstream out( argv[1], ios::ate|ios::out|ios::binary);
ifstream in ( argv[2], ios::in |ios::binary);

if(!in) {
cout<<"Sorry no such file\n";
system ("pause");
return -1;
}

if(in.good()) {
while(!in.eof()) {
in.read( (char*)(&x), sizeof(details)*SIZE);
}
}
in.close();

out.write( (char*)(&x), sizeof(details)*SIZE);

out.close();
system("pause");
return 0;
}
Oct 25 '06 #1
0 1865

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

Similar topics

4
by: Dany | last post by:
Hi When I open an Hexa file I have some weird char like when I open it with Notepad. I want to open it in a readeable form (ex: "FF25AB") like with Ultra edit. Thanks regards, Dany
8
by: Lu | last post by:
Hi there, I got a program to write data to a randomly accessed file (the program moves file pointer to a certain position of the file according the current "keyword" and then writes data). It...
3
by: John Flynn | last post by:
hi, having problems reading from and writing back to the same file. basically, i want to read lines of text from a file and reverse them and write them back to the same file.. it has to...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
1
by: vinothg | last post by:
I have a binary file,which contains strings of 30 bytes each.I need to open the file,read the strings one by one and if the string is not found i need to write it.But unfortunately both read and...
14
by: WStoreyII | last post by:
the following code is supposed to read a whole line upto a new line char from a file. however it does not work. it is producing weird results. please help. I had error checking in there for...
23
by: Jorge Peixoto | last post by:
In the answer to question 12.42 of the C FAQ, we have this code: putc((unsigned)((s.i32 >24) & 0xff), fp); putc((unsigned)((s.i32 >16) & 0xff), fp); putc((unsigned)((s.i32 >8) & 0xff), fp);...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
3
by: maneshborase | last post by:
Hi friends, I am facing one serious problem in my application. I am trying to open dicom image file (.dcm) has size around 400 MB. But I am getting and unhandy exceptions, Some time, ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.