2,868 questions
3
votes
1
answer
138
views
Why fstream read and write functions expect char* and not unsigned char*?
The spec says the following for creating an object using an existing storage - [intro.object#3]:
If a complete object is created ([expr.new]) in storage associated with another object e of type “...
0
votes
0
answers
44
views
Different use of openMode in File. C++ [duplicate]
What is the difference between using this form of code fstream file{"foo.txt", ios::out}; and fstream foo{"foo.txt", fstream::out}; and this fstream file{"foo.txt", file....
4
votes
3
answers
143
views
How do I assign a fstream to an fstream* in C++?
I'm writing a function in C++ that has to return more than one value, and I know that I can't.
So I thought to pass the function two pointers to the files I want to open.
This is the code and then I'...
1
vote
0
answers
104
views
Coordinating access to a shared file between multiple processes
I want to coordinate access to a shared text file between two processes, specifically:
a writer that seldom updates the file;
a reader that frequently read the file.
I want a solution implemented ...
2
votes
3
answers
275
views
Faster file reading in C++
I need to read many different files in succession as fast as possible. It's not one big file, but many small ones. The files I try to read from are the stat files in /proc/<pid>/stat
I am using ...
-1
votes
1
answer
117
views
What is the reason for my code not outputting to a file? [closed]
I am running this through Visual Studio 2022 and using IrfanView 64 as the ppm viewer.
This code is refusing to output to a file of any sort (a txt or ppm is the goal here).
The goal of the code is to ...
1
vote
0
answers
118
views
C++ can't create txt files
Code does not create .txt files to output
Updated: this code is working on friend's PC, but on on my PC. We both installed same mingw file. Both have Windows 11, both tried on VS code and Far Manager
...
7
votes
1
answer
155
views
Is there a way to create a fstream, ifstream or ofstream object with a wchar_t* or wstring filename? (c++)
I am tring to compile some code wrote for the msvc compiler with msys2 gcc (msys2-msys2 not msys2-mingw) and it contains
wstring filename;
ifstream file(filename);
I tried to find a way to use ...
2
votes
1
answer
105
views
Why do c++ standard library template class functions say undefined reference while linking if they are defined in a header
When using <fstream> with g++ -D_GLIBCXX_HAVE__WFOPEN=1 -D_GLIBCXX_USE_WCHAR_T=1
(msys2 gcc 13.3.0) it throws an error
/usr/lib/gcc/x86_64-pc-msys/13.3.0/../../../../x86_64-pc-msys/bin/ld: /tmp/...
1
vote
1
answer
103
views
read double value from a file: std::invalid_argument after std::stod call
#include <iostream>
#include <fstream>
#include <sstream>
int main() {
std::ifstream file("data.txt");
std::string line, numStr;
// read first line
std:...
0
votes
0
answers
57
views
Access violation error when lib try to create file
I created logger lib, and its works fine in test projects. But in my .dll project when I use create file method, it throws runtime access violation error in open method fstream.h file.
basic_filebuf* ...
0
votes
0
answers
49
views
`std::fstream` can't overwrite content after read [duplicate]
I want to find a (fixed length) key in a file and update a value afterward. However, I found a strange behavior where I can't write to the file after reading from it. Here is an example:
#include <...
0
votes
2
answers
156
views
Cpp Get File Line by Index
I have a C++ program which reads a specific line from a file based on the index of that line. The index is calculated elsewhere in the program. My question is: can I open a file (i.e., a .txt) and ...
-1
votes
2
answers
125
views
How to create a proper system of searching/adding non-existent data to a txt file in c++? [closed]
I'm trying to create a system, which allows me to write data to a txt file if it doesn't exist in it. For some reason, it doesn't work with existing data, and I can't seem to understand why. It has no ...
0
votes
1
answer
111
views
Separating blank spaces from getline vs cin >> string
Context: This was an exercise for a "course", where I was given a file with ASCII numbers and was told to decode the ASCII numbers into words. Each word was written in one line, with each ...
0
votes
2
answers
83
views
Why is the "for" cycle not creating multiple files?
Why is this code not creating several files? ofstream should create a file when it encounters a file name that doesn't exist. However, for some reason, even though the names are different, it creates ...
-1
votes
1
answer
174
views
C++ writing and reading last line of file
I need to read numbers from file, calculate sum and write sum on last line, then read sum back from file. It works okayish up until i need to read last line back from file. Can't get it to work.
So, ...
1
vote
0
answers
49
views
How do I find my new created txt folder using fstream in a project folder that Visual Studio created?
I am new to Visual Studio and I found out that for Visual Studio to compile the folder you need to create a project folder that creates it inside the user folder, now I used fstream to try to create a ...
0
votes
1
answer
109
views
About writing/reading "\r\n" to/from a file
Why, if you write a string containing "\r\n" to a file opened in binary mode, and then read that string from the same file, but opened in text mode, then the string will contain '\n' ...
5
votes
1
answer
131
views
Why std::fstream::write() modifies tellg()?
Executing the following code
#include <iostream>
#include <fstream>
int main() {
std::fstream fs{"/dev/shm/test.bin", std::ios::in | std::ios::out | std::ios::binary | std::...
-3
votes
1
answer
74
views
I can't write a string variable to my file using fstream [closed]
When I click a button, I want a string value entered by a user to be stored in a file.
private: System::Void add_task_Click(System::Object^ sender, System::EventArgs^ e) {
ofstream ...
0
votes
0
answers
73
views
Opening file with fstream
I have written a class for handling text files and it looks like this.
#include <fstream>
#include "String.h"
class TextFile
{
std::fstream stream;
public:
TextFile(const ...
1
vote
2
answers
195
views
Should I prefer using basic_istream::tellg() or filesystem::file_size()?
Suppose I have a std::filesystem::path, which I then open as an std::ifstream, and suppose that I want to determine its size (e.g. maybe I want to read the whole file).
Should I prefer calling
auto ...
0
votes
0
answers
49
views
fstream get() function returns weird values
I am working on a maze solving algorithm. I input a text file formatted with 2 numbers at the top defining the width and height. Then a G for goal, S for start, # are walls, . are open spaces.
Example ...
-1
votes
3
answers
59
views
Overwrite the first line, add new ones after it in the file
I want to make a score system in my game that will save the individual score (int score) of every player (string input) as well as the sum of every player score (scoreTotal). To do that I want to keep ...
2
votes
2
answers
205
views
How to write/read std::chrono::zoned_seconds to/from a stream using chrono::parse?
I am trying to write a chrono::zoned_seconds object as text to a file and then retrieve it and construct another chrono::zoned_seconds object later. How can this be done in a fairly efficient way?
I ...
-1
votes
1
answer
125
views
unable to specify the reason for error when trying to read from an input file stream [closed]
assuming that the included file uses namespace std and contains the std library facilities .
i have written a program to write points on the coordinate plane to some input file stream and then use ...
-1
votes
1
answer
124
views
C++ exit(1) causes IO loss?
Here is my program:
#include <fstream>
int main() {
std::ofstream fout("data.dat", std::ios::binary);
for (int isam=0; isam<500; isam++) fout.write((char*) &isam, 4);
...
3
votes
1
answer
120
views
String seems to be deallocated after return from function
I am currently trying to make a simple function that reads a file's contents and stores it into either a std::string or char array. I have run into issues where the data is read completely fine within ...
0
votes
0
answers
59
views
Exeption thrown binary file C++ [duplicate]
EDIT:
Based on suggestion of Retired Ninja, I serialized the data when writing and reading resulting into another two functions
void writeString(std::fstream& file, const std::string& str)
{
...
0
votes
1
answer
109
views
Fstream not reading file properly
Im trying to write a program that will read a file and set a name and the 5 votes the person casts. The problem is that the fstream is not reading the file correctly. However if I open another object ...
0
votes
0
answers
48
views
SDL has disabled fstream file writing? [duplicate]
I am trying to draw a screen of 480 x 270 pixels on the screen, all of them randomly colored, and see how fast I can do this. I was originally trying DirectX, and this worked (which is how I know my ...
0
votes
0
answers
64
views
Reading hex data from a .bin file returns FFs at the start of the string
I am trying to read the HEX data of a .bin file to a string. At the start of the string there is a bunch of FFs and after that the actual data starts. I want to only read the data that the .bin file ...
-1
votes
2
answers
681
views
how do I add new methods to a class in the standard library
I want to extend the std::fstream class from the <fstream> header. I plan to add a couple functions like std::fsteam create(path), std::vector<std::string> read_lines(path), etc.
What Im ...
0
votes
1
answer
85
views
How to operator overload fstream in a new class? [duplicate]
I want to wrap a new class to generate a file and make some auto resource recycle and some additional operate with the new file.
So I make a new class like
class TmpFile {
private:
std::string ...
3
votes
0
answers
167
views
Get a descriptive reason why std::ofstream open failed
Is there a standard/cross-platform way to throw a descriptive exception with the reason why the file failed to be opened? (Eg: "Read-only directory", "Permission denied", "...
0
votes
0
answers
205
views
Outputing WEOF of wchar_t with std::wofstream causes .close() failure
I was trying to implement a simple text editor and was met with this problem.
When using std::wofstream to save buffer to file, I keep a WEOF at the end of the buffer for convenience of other parts in ...
-1
votes
1
answer
105
views
I'm having issues finding binary substring in my BMP file
so i have a small issue. I am working on a school assignment and i just can't figure out how to get the solution. So the assignment needs me to read a file in binary form and then find certain bit ...
3
votes
1
answer
186
views
Disappearing content c++ writing to file
Whenever I write the data into the file after rerunning the code the contents of the file disappears
#include <iostream>
#include <fstream>
using namespace std;
int main(){
string ...
0
votes
0
answers
95
views
ifstream stuck on `\n` when trying to read the previous character
I'm writing a brainfuck interpreter in C++. One of its language features is that in a loop if the current byte != 0 then go back to the beginning of the loop until it's == 0. My approach is reading a ...
0
votes
2
answers
127
views
Writing int type to a file and reading back string types from the same file (C++ i/o fstream)
I'm currently learning writing to and reading from a file in C++, and I stumbled across something I'm not sure I understand.
I'm writing 5 different integers to "ages.txt" file and then I'm ...
-1
votes
1
answer
222
views
Not able to open file using fstream
I think I may not have the file I'm trying to read in the right spot but i've put it everywhere possible in the IDE and it is still not working. Ive even put it next to the .exe for my program and it ...
0
votes
0
answers
28
views
is there a way to std::basic_istream::get with a basic_streambuf to also read the delimeter char?
Given
#include<string>
#include<stringstream>
#include <fstream>
int main(int argc, char* args[])
{
std::fstream file("MyFile.txt", std::fstream::in);
std::stringbuf ...
0
votes
1
answer
155
views
C++ std::fstream getline() stops reading file after first long line
I use std::fstream to read text files line by line with the member function getline(). It uses buffer of limited size, and sometimes lines on input are longer than the buffer. After the first reading ...
-1
votes
1
answer
70
views
How is this implementation of inheritance of C++ fstream for convienient binary output causing failure opening the file?
Code outputs, "file not open."
union converter_t {
char byte[4];
short int word[2];
int dword[1];
} converter;
class enhancedfstream: public fstream {
public:
enhancedfstream(...
0
votes
1
answer
117
views
How can I simultaneously read and write to a binary file?
How can I simultaneously read and write to binary files? I cannot seem to figure out why my program is erroring out and every bit of debugging I can think of does not yield me any good results. The ...
-3
votes
1
answer
97
views
How to solve problem when reading from a file in c++ [closed]
I'm trying to read the information from a .txt file but something very strange is happening.
Here's the code:
ifstream fichier(dir);
fichier.open(dir);
if (fichier.is_open())
cout << &...
1
vote
0
answers
69
views
C++/CLR write data from port in file
I established a port-communication that logs some data from the serial port in an text field.
The communication is successful an the text is displayed in the textfield.
Next, I would like to write the ...
0
votes
0
answers
36
views
C++'s file stream put method changes text file into binary file. Why would it be doing that?
This simple function, in C++17, changes a single character in a text file to a passed in tracking choice (0 or 1). The problem I'm having with this code is that the put statement is changing more than ...
-1
votes
1
answer
119
views
fstream does not save changes C++
I made a program that is supposed to be a simple text editor that can create (if not existed) a .cpp file, read the content of the file and display it on the console, and modify the content and save ...