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

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 2495
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
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
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
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
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
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
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
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
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
7
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
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
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.