I am trying to open a file which normally has content, for the purpose of testing i will like to initialize the program without the files being available/existing so then the program should create empty ones, but am having issues implementing it. This is my code originally
void loadFiles() {
fstream city;
city.open("city.txt", ios::...
I am trying to create a generic class to write and read Objects to/from file.
Called it ActiveRecord class
only has one method, which saves the class itself:
void ActiveRecord::saveRecord(){
string fileName = "data.dat";
ofstream stream(fileName.c_str(), ios::out);
if (!stream) {
cerr << "Error opening file: " << fileName << endl...
Hi,
I have a program which writes its output using ofstream. Everything works perfectly fine on Windows when compiled with Visual Studio, but it only writes empty file on Linux when compiled with GCC.
ofstream out(path_out_cstr, ofstream::out);
if(out.bad()){
cout << "Could not write the file" << flush;
}
else{
cout << "writing";
ou...
I am using g++ to compile some code. I wrote the following snippet:
bool WriteAccess = true;
string Name = "my_file.txt";
ofstream File;
ios_base::open_mode Mode = std::ios_base::in | std::ios_base::binary;
if(WriteAccess)
Mode |= std::ios_base::out | std::ios_base::trunc;
File.open(Name.data(), Mode);
And I receive these errors... ...
I have the following code, running on Suse 10.1 / G++ 4.1.0, and it doesn't write to the file:
#include <fstream>
#include <iostream>
int main(){
std::ofstream file("file.out");
file << "Hello world";
}
The file is correctly created and opened, but is empty.
If I change the code to:
#include <fstream>
#include <iostream>
in...
I'm trying to output a vector of string objects to a file. However, my code only
outputs the first two elements of each string.
The piece of code below writes:
1
1
to a file. Rather then:
01-Jul-09
01-Jul-10
which is what I need.
ofstream file("dates.out");
vector<string> Test(2);
Test[0] = "01-Jul-09";
Test[1...
I'm trying to write the contents of buf pointer to the file created by ofstream.
For some reason the file is empty, however the contents of buf is never empty... What am I doing wrong?
void DLog::Log(const char *fmt, ...)
{
va_list varptr;
va_start(varptr, fmt);
int n = ::_vscprintf(fmt, varptr);
char *buf = new char...
I'm trying to append some data to a file, but in some cases want to skip back a bit from the end to overwrite the tail end of the file. However, neither seekp( pos ) nor seekp( offset, relative ) is having any effect for me (except complaining when using a negative offset). Am I using them incorrectly or are they broken?
A little exampl...
Is there a way in C++ to check if an ostream object is cout or a ofstream object?
Something like:
ostream& output(ostream& out)
{
if (out == cout)
return out;
else
{
out << "something different because its not going to the console" << endl;
return out;
}
}
The reason I want to do this, is that ...
In the following C++ function:
void save_data(ofstream &csv){
csv << "a message";
}
something I cannot understand: if save_data is called, where does it write to? a file? How is it used exactlly? thanks!
...
I have a wstring declared as such:
// random wstring
std::wstring str = L"abcàdëefŸg€hhhhhhhµa";
The literal would be UTF-8 encoded, because my source file is.
[EDIT: According to Mark Ransom this is not necessarily the case, the compiler will decide what encoding to use - let us instead assume that I read this string from a file enc...
I noticed in my C++ code that anytime I close an std::ofstream object I'm unable to reopen the file I closed with std::ifstream. std::ifstream's open function will always fail.
Is there anything 'extra' I can do to ensure that my std::ofstream object closes properly?
Someone's probably going to ask to see my specific code so for the sa...