I'd like to open a new readable and writable file with fstream. I tried to do like the code below, but it failed because the file didn't exist.
file.open(file_path, ios::in | ios::out | ios::binary);
Is there any good way to do this with a new file?
I wrote the code below but it seemed ugly.
file.open(file_path, ios::out | ios::binary);
file.close();
file.open(file_path, ios::in | ios::out | ios::binary);
If you want to overwrite file that already exists you should use:
file.open(file_path, ios::in | ios::out | ios::binary | ios::trunc);
Appending to such a file:
file.open(file_path, ios::in | ios::out | ios::binary | ios::app);
Related
I write someThing to the file, and that's fine, then when I write otherThing from the 4th byte on the same file, the file should say "someotherthing" but there are 4 empty bytes and then "otherthing". The file appears to have been opened in trunc mode. Is there a way to edit the binary file without reading the whole file and put it in a string, make the changes on the string and then write the string to the file?
string fileName = "fileName.bin", someThing = "something", otherThing = "otherthing";
fstream file;
file.open(fileName, ios::out | ios::binary);
file.write(someThing.c_str(), sizeof(someThing);
file.close();
file.open(fileName, ios::out | ios::binary);
file.seekp((streampos)(4));
file.write(otherThing.c_str(), sizeof(otherThing);
file.close();
I have 2 fstream objects called oldFile and newFile and I want to find the non-Null elements in oldFile and write them into newFile of a destructor. The name of the file would be exactly the same "file.txt" so I am essentially overwriting oldFile with newFile.
~Destructor(){
T foo;
fstream newFile;
oldFile.open(fileName, ios::in | ios::out | ios::binary);
oldFile.clear();
while(!oldFile.eof()){
foo.readFromFile(oldFile);
if(!foo.isNull()){
foo.writeToFile(newFile);
}
}
}
Where do I go from here? How do I name the newFile object to "file.txt" and make sure that the oldFile object is overwritten? Very basic file I/O, don't need anything too fancy.
I want to edit the first 100 characters of a file,
I do this, but the new characters override the previous ones (like the photo)
my code :
fstream fileStreamIn("text.txt", ios::in | ios::out | ios::binary);
int theSize = 100;
string theMainBuffer(theSize, '\0');
fileStreamIn.read(&theMainBuffer.front(), theSize);
theMainBuffer.resize(fileStreamIn.gcount());
//cout << theMainBuffer << endl;
fileStreamIn.close();
fileStreamIn.open("text.txt", ios::in | ios::out | ios::binary);
fileStreamIn << "blahblah ";
fileStreamIn.close();
I want "blahblah" to be added to the contents of the file and the previous contents of "helloworld" not to be deleted
output :
blahblahrld !
è !©ª}2•¼Ü²ùXkLÉ·ð„!ð–ç„ñWïðʃ¡ åñ·§Dß}ˆ¹mÐÕŠw:—*ËtMÒJf-Öù“hñ<³:rÛä‡ ”‘Ôyv-4mXþeߧzè’¬ŒŽ<¤‘“‰l'g‚Šâ¡;¬Èa|ÔÁ3îú€;‰±Ï.ÖLáÑȽ[ïÿÿúU%ã2§Ls§n~çˆÏÔäÔ™ 4øÒ‘Ö°,y•»Ô'`` ¬ÜgÜò`÷Tº^E1ØàùÛ÷i§d¨Ù`I5»7á8Zéz0¥Ž’3Y7Êœ¦}eíÝΦIm?óbÙOâ-ŸäëŠgýhýR
Â3‘†y±è±/VŠ¤?Ïù4?’ÑûIÆLQ~DãŠ?Ôêð#N ]³böPK ZQamë š PK 5 -
I want this output :
blahblah hello world !
è !©ª}2•¼Ü²ùXkLÉ·ð„!ð–ç„ñWïðʃ¡ åñ·§Dß}ˆ¹mÐÕŠw:—*ËtMÒJf-Öù“hñ<³:rÛä‡ ”‘Ôyv-4mXþeߧzè’¬ŒŽ<¤‘“‰l'g‚Šâ¡;¬Èa|ÔÁ3îú€;‰±Ï.ÖLáÑȽ[ïÿÿúU%ã2§Ls§n~çˆÏÔäÔ™ 4øÒ‘Ö°,y•»Ô'`` ¬ÜgÜò`÷Tº^E1ØàùÛ÷i§d¨Ù`I5»7á8Zéz0¥Ž’3Y7Êœ¦}eíÝΦIm?óbÙOâ-ŸäëŠgýhýR
Â3‘†y±è±/VŠ¤?Ïù4?’ÑûIÆLQ~DãŠ?Ôêð#N ]³böPK ZQamë š PK 5 -
What is the problem, how can I solve the problem?
thanks
If you don't care to keep the first 100 bytes, simply create 100 lengths of string, change some values and write it to the stream would be enough. Reading a file is not needed.
std::fstream fs("text.txt", ios_base::out | ios_base::binary);
string buffer(100, ' ');
string update="Hello";
buffer.replace(0, update.size(), update);
fs.seekp(20); // move to write position
fs.write(buffer.data(), buffer.size());
fs.close();
Use ios::trunc as the file open mode.
For more info check out this.
I am trying to use std::fstream for io to file, and I want to create the file if it doesn't already exist.
std::fstream my_stream
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
if(!my_stream)
std::cout<<"error"<<strerror(errorno);
I get this result:
"No such file or directory."
How can I create the file in this case?
You're specifying std::fstream::in in your call to fstream::open(). This is known to force it to require an existing file.
Either remove std::fstream::in from your mode argument, or specify std::fstream::trunc in addition to the other flags.
It's a little messy but works. Doesn't overwrite the file if it exists but creates a new one if the first open fails.
std::fstream my_stream
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
if(!my_stream)
{
my_stream.open("my_file_name",std::fstream::binary | std::fstream::trunc | std::fstream::out);
my_stream.close();
// re-open with original flags
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
}
else
{
// read something
}
// read/write here
I am trying to use std::fstream for io to file, and I want to create the file if it doesn't already exist.
std::fstream my_stream
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
if(!my_stream)
std::cout<<"error"<<strerror(errorno);
I get this result:
"No such file or directory."
How can I create the file in this case?
You're specifying std::fstream::in in your call to fstream::open(). This is known to force it to require an existing file.
Either remove std::fstream::in from your mode argument, or specify std::fstream::trunc in addition to the other flags.
It's a little messy but works. Doesn't overwrite the file if it exists but creates a new one if the first open fails.
std::fstream my_stream
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
if(!my_stream)
{
my_stream.open("my_file_name",std::fstream::binary | std::fstream::trunc | std::fstream::out);
my_stream.close();
// re-open with original flags
my_stream.open("my_file_name",std::fstream::binary | std::fstream::in | std::fstream::out);
}
else
{
// read something
}
// read/write here