Questions tagged [ofstream]

Output file streams are C++ standard library objects used to write to files.

The C++ standard library type std::ofstream is a specialized std::ostream which uses a std::filebuf to write to a file. std::ofstream is declared in fstream.

Use this tag for questions about std::ofstream or the wide-character version std::wofstream, or the class template std::basic_ofstream. For questions about file I/O in C++ the tags , , and might be relevant. For more general questions about output streams use the or tags.

1006 questions
4
votes
2 answers

Can't we manage std::map?

I tried to create for outputting the timing results and call any ofstream from pre-defined string: #include #include…
mallea
  • 534
  • 6
  • 17
4
votes
1 answer

Reading and writing to the same file fstream

I would like to update existing json file. This is example json file: { "Foo": 51.32, "Number": 100, "Test": "Test1" } Logs from program: Operation successfully performed 100 "Test1" 51.32 46.32 Done Looks like everythink works as…
4
votes
3 answers

ofstream creating file but not writing to it in C++

I am writing an MFC program that has a dialog with an "Export" button that will take all of the data that has been entered into the file and export it to a .txt (at some point I want to change this to a .msg file...but that's a question for another…
Samuel Ohrenberg
  • 61
  • 1
  • 2
  • 7
4
votes
2 answers

C++ std::ofstream - Move the put pointer

I am writing some data to a file. Occasionally, I want to write a block of data from memory, and then move the put pointer along either 1, 2 or 3 bytes to maintain a 4 byte data boundary format. I could make a new block of data containing zeros and…
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
4
votes
3 answers

Strange error: cannot convert from 'int' to 'ios_base::openmode'

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 |=…
Dylan Klomparens
  • 2,853
  • 7
  • 35
  • 52
4
votes
1 answer

Read/Write to PPM Image File C++

Trying to read and write to/from a PPM Image file (.ppm) in the only way I know how: std::istream& operator >>(std::istream &inputStream, PPMObject &other) { inputStream.seekg(0, ios::end); int size = inputStream.tellg(); …
user4640007
  • 97
  • 1
  • 2
  • 7
4
votes
4 answers

How do you search a document for a string in c++?

Here's my code so far: #include #include #include using namespace std; int main() { int count = 0; string fileName; string keyWord; string word; cout << "Please make sure the document is in the same…
Soully
  • 859
  • 6
  • 16
  • 24
4
votes
4 answers

read and write a binary file in c++ with fstream

I'm trying to write simple c++ code to read and write a file. The problem is my output file is smaller than the original file, and I'm stuck finding the cause. I have a image with 6.6 kb and my output image is about 6.4 kb #include…
Hadi Rasekh
  • 2,622
  • 2
  • 20
  • 28
4
votes
4 answers

Partially truncating a stream (fstream or ofstream) in C++

I am trying to partially truncate (or shorten) an existing file, using fstream. I have tried writing an EOF character, but this seems to do nothing. Any help would be appreciated...
Paul Ridgway
  • 1,055
  • 2
  • 13
  • 23
4
votes
1 answer

initializer_lists of streams (C++11)

I'm trying to pass variable number of ofstreams over to a function that accepts an initializer_list but doesn't seem to work and throws all possible errors from the initializer_list structure and about how my function is with an array of ofstreams…
Jon Gan
  • 867
  • 1
  • 11
  • 22
4
votes
3 answers

C++ How to create a bitmap file

I am trying to figure out how to create a bitmap file in C++ VS. Currently I have taken in the file name and adding the ".bmp" extension to create the file. I want to know how I could change the pixels of the file by making it into different…
user2557642
  • 45
  • 1
  • 1
  • 4
4
votes
2 answers

Unexpected rounding of double types in C++ writing to file

I am working on a C++ program with a lot of numbers that are type double (values in the millions and billions with just a couple places to the right of the decimal point). I am performing calculations on these numbers and then printing the result…
brentf
  • 411
  • 2
  • 8
  • 14
4
votes
3 answers

What circumstances ostream::write or ostream::operator<< would fail under?

In my C++ code, I am constantly writing different values into a file. My question is that if there is any circumstances that write or << would fail under, considering the fact that file was opened successfully. Do I need to check every single call…
ipluto
  • 179
  • 1
  • 8
4
votes
4 answers

c++ debugging ofstream fail() states?

I've been having strange problems writing to file with ofstreams and have now that ofstream.fail() is returning true right after my ofstream.open() call. Are there some ways to get additional information, like more specifics on why the fail state…
user788171
  • 16,753
  • 40
  • 98
  • 125
4
votes
2 answers

Opening an existing .doc file using ofstream in C++

Assuming I have a file with .doc extension in Windows platform, how can I open the the file for outputting its contents on the screen using the ofstream object in C++? I am aware that the object can be used to open files in text and binary modes.…
user1832196
  • 41
  • 1
  • 2