473,508 Members | 4,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fstream question

I have two files, the first one is an ascii file and the second is a
binary one.
I want to add the ascii file on the end of the binary file in order to
obtain a one binary file:

I have done something like this but doesn't work:

int main(int argc, char **argv)
{
double c;
char density[80]="density\0";
ofstream file_vtk(argv[1],ios::out | ios::binary | ios::app);
ifstream file_data(argv[2],ios::in | ios::binary);
while(!file_data)
{
file_data >> c;
file_vtk << c << " " ;
}

file_data.close();
file_vtk.close();
return 0;
}

I'm sure that there is a good way to do this, but I dont have it !!
any suggestions?
regards.
Yous
Dec 22 '05 #1
3 2501
Youssef Mesri wrote:
I have two files, the first one is an ascii file and the second is a
binary one.
I want to add the ascii file on the end of the binary file in order to
obtain a one binary file:

I have done something like this but doesn't work:

int main(int argc, char **argv)
{
double c;
char density[80]="density\0";
A string literal is an ascii-0 terminated array of approproate number
of characters. So you neednot append an ascii-0 character specifically
(unless you want an extra 0 character there)
ofstream file_vtk(argv[1],ios::out | ios::binary | ios::app);
ifstream file_data(argv[2],ios::in | ios::binary);
while(!file_data)
this means that while file_data is in FAIL state (and you mean exactly
the reverse)
So what you need is

while(file_data)
{
file_data >> c;
why do you want to use double ? Use a char. double will create problems
since the data read will get converted as a double.
file_vtk << c << " " ;
}

file_data.close();
file_vtk.close();
return 0;
}


HTH.

Dec 22 '05 #2
Neelesh Bodas wrote:
Youssef Mesri wrote:
I have two files, the first one is an ascii file and the second is a
binary one.
I want to add the ascii file on the end of the binary file in order to
obtain a one binary file:

I have done something like this but doesn't work:

int main(int argc, char **argv)
{
double c;
char density[80]="density\0";

A string literal is an ascii-0 terminated array of approproate number
of characters. So you neednot append an ascii-0 character specifically
(unless you want an extra 0 character there)

ofstream file_vtk(argv[1],ios::out | ios::binary | ios::app);
ifstream file_data(argv[2],ios::in | ios::binary);
while(!file_data)

this means that while file_data is in FAIL state (and you mean exactly
the reverse)
So what you need is

while(file_data)


soory, it was a typing error
{
file_data >> c;

why do you want to use double ? Use a char. double will create problems
since the data read will get converted as a double.

the data in file_data is double, I want to save it double.
my problem is located in the different types of data in the two files
(ascii and binary). If I open the result file is constructed with two
different type, the first part in binary and the second in the ascii
format. And I would like just a binary type!!
file_vtk << c << " " ;
}

file_data.close();
file_vtk.close();
return 0;
}

HTH.

Dec 22 '05 #3

Youssef Mesri wrote:
my problem is located in the different types of data in the two files
(ascii and binary).
Whether the contents are double or ints or Person details is all
relative to how you view it (and makes sense only at a higher level of
abstraction). For a stream, the whole information is seen as a sequence
of "char"s.
Eg: if you have 10 in your file, you may interprete it as "int",
"double" or "binary" or whatever - the stream will read it as a
sequence of characters at the lowest level. So you should use a char
here (assuming you are using ifstream and ofstream)
If I open the result file is constructed with two
different type, the first part in binary and the second in the ascii
format. And I would like just a binary type!!


What do you mean by "first part" and "second part"? What do you mean by
ascii and binary format? Note that these terms actually make sense with
the low-level file operations which decide how to open the file for
reading and writing etc. Since you have appended the binary file to a
text file, you will surely see first some ascii characters and then
some non-ascii characters.

Of course I am not an expert in C++. May be I missing something.
Corrections are most welcome.

Dec 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
15282
by: Maya | last post by:
Apart from being a pointer, what would be the benefit of using 'std::filebuf' than using the std::fstream? As far as I can see, I would use the same methods in 'filebuf' that I would when using...
1
4906
by: Newsgroup - Ann | last post by:
The fstream is supposed to be a replacement of the stdio.h and the exception is supposed to be a replacement of the return error code. So how do I replace my old codes of the style: if (output =...
11
5362
by: Charles L | last post by:
I have read that the inclusion of <fstream.h> makes the inclusion of <iostream.h> unnecessary. Is this correct? Charles L
2
1667
by: Bill Wayne | last post by:
Hi, I'm trying to figure out a way to convert some Dungeons and Dragons treasure lists into bracketed numbers. For example: 01-18 - - Protection +1 19-28 - - Feather falling ...
9
5268
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
1
5334
by: MForey | last post by:
I'm attempting to create a program that uses fstream objects to read/write to files. However, it is currently balky at best. The fstream.write call doesn't return an error, but the modified data...
6
17102
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
5
4146
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
7
9931
by: Soneji | last post by:
*sigh* Ok, I have two questions. First, my setup: Win-doze XP-SP2; & Dev-C++ 4.9.9.2; I recently started trying to use 'fstream' to work my input/output, and I noticed that using the...
6
3719
by: Gaijinco | last post by:
Should this do something? #include <fstream> #include <string> int main() { std::fstream filestr ("test.txt", std::fstream::in | std::fstream::out); std::string s="";
0
7228
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
7128
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...
1
7058
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
5635
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,...
0
4715
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...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1565
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 ...
0
426
bsmnconsultancy
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...

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.