I have a project witch I have to back up my text files in binary mode with the destination that is getting from the user.
I was thinking to open my text files in binary and close them in the address that I have got from the user. but I don't know how to do that.
is there a way to close the files in a new address(saving them where ever I want) and not set an address directly because it's suppose to set by the user
Here is sample code to save the file:
#include <fstream>
int main () {
std::ofstream ofs;
ofs.open ("test.txt", std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
ofs << " data goes here";
ofs.close();
return 0;
}
The following is sample code to copy files:
ifstream source("from.txt", ios::binary);
ofstream dest("to.txt", ios::binary);
source.seekg(0, ios::end);
ifstream::pos_type size = source.tellg(); // file size
source.seekg(0);
char* buffer = new char[size]; // allocate memory for buffer
// copy file
source.read(buffer, size);
dest.write(buffer, size);
// clean up
delete[] buffer;
source.close();
dest.close();
Related
Im trying to code a program that reads a file then copies all of the data and stores it into another file. The code below does that but it adds extra bytes randomly in the file which makes the file 249KB instead of 243KB. Ive found that if i remove the "\n" it doesn't add any extra bytes but instead it removes bytes and makes it 237KB.
std::wifstream in_file("initfs_Win32", std::ios::binary);
in_file.seekg(0, std::ios::beg);
std::wstring in_data;
if (in_file.good()) {
while (std::getline(in_file, in_data))
str_buf += in_data + L"\n"
std::wofstream out_file("initfs");
out_file << str_buf;
out_file.close();
}
in_file.close();
Here is an image of the old (Right) and new (Left) files side by side:
image
If you want to copy one file into another file don't do it line by line, do it this way instead:
std::ifstream source("initfs_Win32", std::ios::binary);
if (not source.is_open())
{
std::cerr << "Error opening file" << std::endl;
}
std::ofstream destiny("initfs", std::ios::binary);
destiny << original.rdbuf();
destiny.close();
I'm writing a simple binary file that must contain the contents of another binary file and a string name of this (another) file at the end.
I found this sample code that uses QByteArray from the Qt library. My question is: is it possible to do the same with std c++ functions?
char buf;
QFile sourceFile( "c:/input.ofp" );
QFileInfo fileInfo(sourceFile);
QByteArray fileByteArray;
// Fill the QByteArray with the binary data of the file
fileByteArray = sourceFile.readAll();
sourceFile.close();
std::ofstream fout;
fout.open( "c:/test.bin", std::ios::binary );
// fill the output file with the binary data of the input file
for (int i = 0; i < fileByteArray.size(); i++) {
buf = fileByteArray.at(i);
fout.write(&buf, 1);
}
// Fill the file name QByteArray
QByteArray fileNameArray = fileInfo.fileName().toLatin1();
// fill the end of the output binary file with the input file name characters
for ( int i = 0; i < fileInfo.fileName().size();i++ ) {
buf = fileNameArray.at(i);
fout.write( &buf, 1 );
}
fout.close();
Open your files in binary mode and copy in "one shot" via rdbuf:
std::string inputFile = "c:/input.ofp";
std::ifstream source(input, std::ios::binary);
std::ofstream dest("c:/test.bin", std::ios::binary);
dest << source.rdbuf();
Then write filename at the end:
dest.write(input.c_str(), input.length());
You can find more ways here.
Yes, refer to fstream / ofstream. You could do it like this:
std::string text = "abcde"; // your text
std::ofstream ofstr; // stream object
ofstr.open("Test.txt"); // open your file
ofstr << text; // or: ofstr << "abcde"; // append text
I'm simply trying to read a small 1KB binary file into a buffer and then write the buffer back to the disk. It seems that for some files the outputfile is completely different from the Inputfile, what am I doing wrong?
Thank you very much.
std::ifstream myfile;
myfile.open (testinput.rar);
myfile.seekg (0, myfile.end);
filesize = myfile.tellg();
myfile.seekg (0, myfile.beg);
char *mybuffer= new char[filesize];
myfile.read(mybuffer,filesize);
myfile.close();
ofstream myfile3;
myfile3.open ("testoutput.rar");
for(unsigned int i=0; i<filesize; i++)
myfile3 << mybuffer[i];
myfile3.close();
You have to open the file as binary.
myfile.open ("testinput.rar", std::ios::binary);
myfile3 should be opened in binary mode:
myfile3.open("testoutput.rar", ios::out | ios::binary);
Additionally, you may want to consider using write() to modify files:
myfile3.write(mybuffer[i], sizeOfBuffer);
I'm trying to read a binary file and I need to determine its size, but regardless of the method I've tried, I'm getting a size of zero.
For example:
fstream cbf(address, ios::binary | ios::in | ios::ate);
fstream::pos_type size = cbf.tellg(); // Returns 0.
char* chunk = new char[size];
cbf.read(chunk, size);
//...
If I were to use the following:
#include <sys/stat.h>
struct stat st;
stat(address.c_str(),&st);
int size = st.st_size;
The size is still zero. I've also tried the following, but it's still zero.
File* fp;
fp = open(address.c_str(), "rb");
How do I get the size of the file?
Thanks for the responses... I've identified the problem:
The binary file I was trying to access was created during the execution, and I just had forgotten to close it before trying to read from it...
Neither of your examples checks for failure. This program, using your first method, works perfectly well for me. It correctly identifies the size of /etc/passwd and the non-existence of /etc/motd.
#include <fstream>
#include <iostream>
#include <string>
void printSize(const std::string& address) {
std::fstream motd(address.c_str(), std::ios::binary|std::ios::in|std::ios::ate);
if(motd) {
std::fstream::pos_type size = motd.tellg();
std::cout << address << " " << size << "\n";
} else {
perror(address.c_str());
}
}
int main () {
printSize("/etc/motd");
printSize("/etc/passwd");
}
Try to load the file in this method.
Note: Use ifstream insted of fstream in this line ifstream cbf(address, ios::binary | ios::in );
long size;
ifstream cbf(address, ios::binary | ios::in);
cbf.seekg(0, ios::end);
size=cbf.tellg();
cbf.seekg(0, ios::beg);
char* chunk = new char[size];
cbf.read(chunk, size);
#include <iostream>
#include <fstream>
int main() {
std::ofstream outfile("text.txt", ios::trunc);
std::ifstream infile("text.txt", ios::trunc);
outfile.seekp(0);
std::cout << "This is a file";
infile.seekg(0, ios::end);
int length = infile.tellg();
infile.read(0, length);
infile.close();
outfile.close();
return 0;
}
I think I get the idea behind this, but I feel like (and I'm pretty sure) I have no idea what I'm doing. I've looked it up and everything has confused me. I've read through a C++ reference, and then I googled it, but I still don't understand what I'm doing wrong.
#include <iostream>
#include <fstream>
#include <cstring>
int main() {
std::fstream file("text.txt", std::ios_base::in | std::ios_base::out);
file << "This is a file";
int length = file.tellg();
std::string uberstring;
file >> uberstring;
std::cout << uberstring;
char *buffer = new char[length + 1];
file.read(buffer, length);
buffer[length] = '\0';
file.close();
delete [] buffer;
return 0;
}
I tried this, but it isn't printing anything. Why isn't this working?
If you want to read and write to the same file, just use a normal std::fstream ... there is no need to attempt and open the same file as both a ifstream and ofstream. Also if you want to write data to the file, use the operator<< on the actual fstream instance object, not std::cout ... that will simply write to wherever std::cout is set, which is typically the console. Finally, the call to read has to go back into a buffer, you can't use NULL as an argument. So your code would change to the following:
int main()
{
std::fstream file("text.txt", ios_base::in | ios_base::out);
//outfile.seekp(0); <== not needed since you just opened the file
file << "This is a file"; //<== use the std::fstream instance "file"
//file.seekg(0, ios::end); <== not needed ... you're already at the end
int length = file.tellg();
//you have to read back into a buffer
char* buffer = new char[length + 1];
infile.read(buffer, length);
buffer[length] = '\0'; //<== NULL terminate the string
file.close();
delete [] buffer;
return 0;
}