I have a very simple program
#include <iostream>
#include <fstream>
void CHECK(std::iostream& s)
{
std::cout << "good(): " << s.good()
<< " fail(): " << s.fail()
<< " bad(): " << s.bad()
<< " eof(): " << s.eof() << std::endl;
}
int main(int argc, const char * argv[])
{
std::fstream ofs("test.txt", std::ios::out | std::ios::trunc);
std::cout << "opened" << std::endl;
CHECK(ofs);
ofs << "Hello, World!\n";
CHECK(ofs);
ofs.close();
std::cout << "closed" << std::endl;
CHECK(ofs);
ofs << "Hello, World!\n";
std::cout << "after operation" << std::endl;
CHECK(ofs);
return 0;
}
With libc++ I get the following last line:
good(): 1 fail(): 0 bad(): 0 eof(): 0
Expected (or with libstdc++):
good(): 0 fail(): 1 bad(): 1 eof(): 0
I have tested on OSX with Xcode 9.4.1 (or on Linux), but always the same. Can anybody explain me the situation here? Also the file content was not updated, because already closed. Why is the stream still good after closing and further operation?
What I suspect is happening is that the operations are stuffing the data into the rdbuf associated with the stream. That succeeds, as long as there is room in the buffer. Eventually, the buffer gets full, and the stream attempts to write to the file (which is closed) and that fails.
You can test that by making the last bit a loop:
ofs.close();
std::cout << "closed" << std::endl;
CHECK(ofs);
for (int i = 0; i < 500; ++i)
{
ofs << "Hello, World!\n";
std::cout << i << " ";
CHECK(ofs);
}
std::cout << "after operation" << std::endl;
On my machine, it fails after about 300 - and forever after that.
Is this correct behavior? (or even standards-compliant?)
I don't know.
[ Later: If I change libc++ do set the buffer size to 0 upon close, then the first write fails - so that suggests that my analysis is correct. However, I still haven't found anything in the standard about what this 'should' do. ]
Related
So I am creating a class that inherits from ifstream, and that gives me acces to the protected function set_rdbuf, and at first I just thought it allowed you to set your own filebuf that then would be used, but that does not seem to work for me. I wrote the following code:
class A : public std::fstream {
private:
std::filebuf m_file_buf;
public:
A() {
if (!m_file_buf.open("test_file.txt", ios_base::out | ios_base::in)) {
std::cout << "something went wrong";
}
// std::fstream::rdbuf()->swap(m_file_buf);
std::fstream::set_rdbuf(&m_file_buf);
std::cout << "m_ file buff is: " << m_file_buf.is_open() << "\n";
std::cout << "rd file buff is: " << std::fstream::rdbuf()->is_open() << "\n";
std::cout << "we are: " << std::fstream::is_open() << "\n";
}
};
int main() {
A a;
return 0;
}
This will log:
m_ file buff is: 1
rd file buff is: 0
we are: 0
I was under the assumption that when I called rdbuf() I would then get the one I had set it to. However, if I use the swap function that rdbuf() provides, then it prints as I expect it to
class A : public std::fstream {
private:
std::filebuf* m_file_buf;
public:
A() {
m_file_buf = new std::filebuf();
if (!m_file_buf->open("test_file.txt", ios_base::out | ios_base::in)) {
std::cout << "something went wrong";
}
std::fstream::rdbuf()->swap(*m_file_buf);
std::cout << "m_ file buff is: " << m_file_buf->is_open() << "\n";
std::cout << "rd file buff is: " << std::fstream::rdbuf()->is_open() << "\n";
std::cout << "we are: " << std::fstream::is_open() << "\n";
// Get it back again as we swapped it out
m_file_buf = std::fstream::rdbuf();
}
};
int main() {
A a;
return 0;
}
m_ file buff is: 0
rd file buff is: 1
we are: 1
//and when I at the end swap back, everything is 1/open
I swited to pointer in the swap code so that I could point to the return of rdbuf()
This was compiled on MSVC version 19.28 with arg /std:c++17
So am I wrong is how set_rdbuf is supposed to be used, or is there something else I am doing wrong?
I did two months search on the web for a proper file locking mechanism to be used in a C++ program.
I found a lot on "C and fnctl" which I could proof to work. But all really proper working locking mechanism, that I could proof to work in Linux are only based on file descriptors.
As this seems to be something really old fashined and in actual C++17 style of writing C++ code with file- and ip-streams not using that mechanism, I only came up with something that works with using what was presented here:
Not able to ofstream using __gnu_cxx::stdio_filebuf
My Question is, is this really the only mechanism working? To connect both worlds?
I looked in all these books to find anything about fcntl and C++, but was not successful:
[Der C++ Programmierer Cxx20]
(https://www.hanser-elibrary.com/doi/book/10.3139/9783446465510)
[The C++ Programming Language] (https://www.stroustrup.com/C++.html)
[C++ Das Umfassende Handbuch]
(https://www.rheinwerk-verlag.de/c-plusplus-das-umfassende-handbuch/)
[Modern C++ Programming Cookbook Second Edition]
(https://www.packtpub.com/product/modern-c-programming-cookbook-second-edition/9781800208988)
My question to the C++ gurus here is, if I missed something, or if the following code is, today, begin of 2021 the best we could do.
Short explanation of what the code is a proof for:
We have a C++ Code which adds usernames and its LSF-processes to a conf-file, which is read by SSH-server to allow user access to that machine. As at the same time two or more running processes of this code could lead to concurrent attempts of adding or deleting users from this file could occur, we have to proof that proper file locking is preventing that. Without using an extra "access" file, which also could be a solution.
This is some example code I tested:
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <fcntl.h>
#include <unistd.h>
#include <ext/stdio_filebuf.h>
using namespace std::this_thread; // for sleep_for
int main( ) {
// set unbuffered concole output
std::cout.setf(std::ios::unitbuf);
const char* filename {"testfile.txt"};
// get input from input_from_user
std::string input_from_user_string;
std::cout << "Please give input to change in the file: ";
std::cin >> input_from_user_string;
int add_1_del_2 = 0;
std::cout << "Please give 1 if you want to add to the file or 2 if you want to delete from file: ";
std::cin >> add_1_del_2;
int input_from_user_time;
std::cout << "Please give seconds to wait: ";
std::cin >> input_from_user_time;
// opening file
std::cout << "Opening File" << std::endl;
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH; //664
int fd;
fd = open(filename, O_RDWR | O_CREAT, mode);
// printing out information about file descriptor
std::cout << " Dexc:" << fd << std::endl;
// generating C++-streams on filedescriptor
__gnu_cxx::stdio_filebuf<char> sourcebufin(fd, std::ios::in);
__gnu_cxx::stdio_filebuf<char> sourcebufout(fd, std::ios::out);
std::istream myfilein(&sourcebufin);
std::ostream myfileout(&sourcebufout);
// -----------
// check for file Locking or exit
// -----------
// creating structure for file locking
struct flock fl;
fl.l_type = F_RDLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
// set file locking for read
fl.l_type = F_RDLCK;
std::cout << "Checking for Lock on file" << std::endl;
// check for file locking on file for read only once
(void) fcntl(fd, F_GETLK, &fl);
if (fl.l_type != F_UNLCK) {
std::cout << "File is locked for reading by process "
<< fl.l_pid
<< ", in status"
<< ((fl.l_type == F_WRLCK) ? 'W' : 'R')
<< ", start="
<< fl.l_start
<< ", end="
<< fl.l_len
<< std::endl;
}
else {
(void) printf("File is unlocked for reading\n");
}
// set file locking for write
fl.l_type = F_WRLCK;
// check for file locking on file for write in a loop
for (int i = 1; i < 11; i++) {
//printf("Checking for lock %d of 10 times...\n", i);
std::cout << "Checking for lock "
<< i
<< " of 10 times..."
<< std::endl;
(void) fcntl(fd, F_GETLK, &fl);
if (fl.l_type != F_UNLCK) {
//(void) printf("File is locked by process %d, in status %c, start=%8ld, end=%8ld\n", fl.l_pid,
// , fl.l_start, fl.l_len);
std::cout << "File is locked by process "
<< fl.l_pid
<< ", in status"
<< ((fl.l_type == F_WRLCK) ? 'W' : 'R')
<< ", start="
<< fl.l_start
<< ", end="
<< fl.l_len
<< std::endl;
sleep(10);
}
else {
(void) printf("File is unlocked\n");
break;
}
}
// -----------
// apply lock for write on file
// -----------
// locking file
std::cout << "Locking file for write" << std::endl;
// set file locking for write again, as checking on lock resets it
fl.l_type = F_WRLCK;
if (fcntl(fd, F_SETLKW, &fl) == -1) {
perror("fcntl");
abort();
}
// -----------
// wait some time
// -----------
std::cout << "Now waiting for " << input_from_user_time << " seconds, keeping the file locked..." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(input_from_user_time));
// -----------
// read from file
// -----------
std::cout << "Reading from file... " << std::endl;
myfilein.seekg(0, std::ios::end);
size_t size_before = myfilein.tellg();
myfilein.seekg(0);
std::string filecontent{""};
filecontent.reserve(size_before);
std::cout << "Length of file is: " << size_before << std::endl;
// read full content of file in string "filecontent"
filecontent.assign((std::istreambuf_iterator<char>(myfilein)),
std::istreambuf_iterator<char>());
// -----------
// print output about read data
// -----------
std::cout << "Length of filecontent-string: " << filecontent.size() << std::endl;
std::cout << "Content of File begin" << std::endl;
std::cout << "----------" << std::endl;
std::cout << filecontent << std::endl;
std::cout << "----------" << std::endl;
// -----------
// Apply changes on read in data depending on given input
// -----------
if (add_1_del_2 == 2) {
std::cout << "Runmode: Del" << std::endl;
std::string string_to_delete = input_from_user_string+"\n";
std::string::size_type pos_of_found_substring = filecontent.find(string_to_delete);
if (pos_of_found_substring != std::string::npos) {
filecontent.erase(pos_of_found_substring, string_to_delete.length());
}
else {
}
}
if (add_1_del_2 == 1) {
std::cout << "Runmode: Append" << std::endl;
filecontent.append(input_from_user_string);
}
std::cout << "Content of String after change" << std::endl;
std::cout << "----------" << std::endl;
std::cout << filecontent << std::endl;
std::cout << "----------" << std::endl;
// -----------
// write out to file, truncate before to length of new string
// -----------
std::cout << "Now starting the write out..." << std::endl;
myfilein.seekg(0);
ftruncate(fd,filecontent.length());
myfileout.seekp(0);
myfileout << filecontent;
myfileout.flush();
myfileout.clear();
// -----------
// read from file for a second time and printout content
// -----------
std::cout << "Reading from file again... " << std::endl;
myfilein.seekg(0, std::ios::end);
size_t size_after = myfilein.tellg();
myfilein.seekg(0);
std::string filecontent_after{""};
filecontent_after.reserve(size_after);
std::cout << "Length of file is now: " << size_after << std::endl;
// read full content of file in string "filecontent"
filecontent_after.assign((std::istreambuf_iterator<char>(myfilein)),
std::istreambuf_iterator<char>());
std::cout << "Length of filecontent_after-string: " << filecontent_after.size() << std::endl;
std::cout << "Content of File end" << std::endl;
std::cout << "----------" << std::endl;
std::cout << filecontent_after << std::endl;
std::cout << "----------" << std::endl;
// -----------
// unlocking file and close file
// -----------
printf("Unlocking...\n");
fl.l_type = F_UNLCK;
if (fcntl(fd, F_SETLK, &fl) == -1) {
perror("fcntl");
abort();
}
close(fd);
// -----------
// done
// -----------
std::cout << "done" << std::endl;
exit(0);
}
I ask for your comments on this or perhaps how to improve.
Alexander Bruns
I have this weird problem where SFML's sf::VideoMode::getFullscreenModes() method, which is supposed to only return valid video modes, actually only returns invalid video modes.
When I change my launch configuration to Release it works fine.
#include <SFML/Window.hpp>
#include <iostream>
int main(int argCount, char** argVector) {
std::vector<sf::VideoMode> videoModes;
videoModes = sf::VideoMode::getFullscreenModes();
sf::VideoMode videoMode;
for(unsigned i = 0; i < videoModes.size(); i++) {
if(videoModes[i].isValid())
std::cout << "Vanlid VideoMode: " << i << " - " << videoModes[i].width << "x" << videoModes[i].height << " b" << videoModes[i].bitsPerPixel << std::endl;
else
std::cout << "Invalid VideoMode: " << i << " - " << videoModes[i].width << "x" << videoModes[i].height << " b" << videoModes[i].bitsPerPixel << std::endl;
}
char input[1];
std::cin >> input;
return 0;
}
Console Output:
Invalid VideoMode: 0 - 3131961357x3131961357 b3131961357
Invalid VideoMode: 1 - 3131961357x3131961357 b3131961357
Invalid VideoMode: 2 - 3131961357x3131961357 b3131961357
Invalid VideoMode: 3 - 3131961357x3131961357 b3131961357
The answer is the same as on this question:
https://stackoverflow.com/a/17777615/2558778
You have to use the debug libraries in debug mode, and the release ones in release mode. Mixing them up can result in crashes.
I would like to know if and why seekg(0) is not supposed to clear the eofbit of a stream.
I am in a point where I have already read all the stream, thus EOF has been reached (but no failbit is set yet) and want to go back with seekg() to a valid position and read some chars again. In this case seekg(0) seems "to work" with the eofbit set, but as soon as I try to read from the stream, the failbit is set. Is this logic, correct or is my implementation bad? Am I supposed to recognize this case and clear the eofbit manually (if the failbit is not set)?
EDIT:
The following program provided by a reader gives different results in my implementation ( mingw32-c++.exe (TDM-2 mingw32) 4.4.1 ):
#include <sstream>
#include <iostream>
#include <string>
int main() {
std::istringstream foo("AAA");
std::string a;
foo >> a;
std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 0
foo.seekg(0);
std::cout << foo.eof() << " " << foo.fail() << std::endl; // 0 0
foo >> a;
std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 0
foo >> a;
std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 1
}
The comments above are from the user who tried that program in his implementation. I obtain these results:
1 0
1 0
1 1
1 1
According to the new standard clear() is supposed to reset the eofbit (§ 27.7.2.3):
basic_istream<charT,traits>& seekg(pos_type pos);
Effects: Behaves as an unformatted input function ..., except that the function first clears eofbit ...
But in the old standard (§ 27.6.1.3) there is no mention of clearing the eofbit!
And a simple test:
#include <sstream>
#include <iostream>
#include <string>
int main() {
std::istringstream foo("AAA");
std::string a;
foo >> a;
std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 0
foo.seekg(0);
std::cout << foo.eof() << " " << foo.fail() << std::endl; // 0 0
foo >> a;
std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 0
foo >> a;
std::cout << foo.eof() << " " << foo.fail() << std::endl; // 1 1
}
Why not just manually clear() the stream then go back once the eofbit has been set? EOF has been reached, why should seekg clear it automatically? Doing that would seem to cause more problems.
Some background information, for a homework assignment I had to write a polish notation calculator using binary trees, for this to work I had to parse command line input so that it would properly build the binary tree and then go over it to give a valid answer to the mathematical expression that was entered.
For the parsing I used a std::stringstream so that I would easily be able to convert the std::string I was handed into a valid float (or integer, double). The issue I ran across was the following code, which has the error showcased and how I solved the issue. I was hoping that somebody where would be able to tell me if I was doing something wrong and .clear() is not correct, or if this is a bug in the standard library in the way it handles this particular input (only happens for + and -).
#include <iostream>
#include <sstream>
#include <string>
int main() {
std::string mystring("+");
int num;
char op;
std::stringstream iss(mystring);
iss >> num;
// Seems it is not a number
if (iss.fail()) {
// This part does not work as you would expect it to
// We clear the error state of the stringstream
iss.clear();
std::cout << "iss fail bit: " << iss.fail() << std::endl;
iss.get(op);
std::cout << "op is: " << op << " iss is: " << iss.str() << std::endl;
std::cout << "iss fail bit: " << iss.fail() << std::endl;
// This however works as you would expect it to
std::stringstream oss(iss.str());
std::cout << "oss fail bit: " << oss.fail() << std::endl;
oss.get(op);
std::cout << "op is: " << op << " oss is: " << oss.str() << std::endl;
std::cout << "oss fail bit: " << oss.fail() << std::endl;
} else {
// We got a number
}
}
Sample output from the program:
iss fail bit: 0
op is: iss is: +
iss fail bit: 1
oss fail bit: 0
op is: + oss is: +
oss fail bit: 0
Maybe you guys will see something I missed, or if this is indeed a bug higher up beyond my program, in which case pointers as to where to report this would be greatly appreciated.
When you say:
iss.clear();
std::cout << "iss fail bit: " << iss.fail() << std::endl;
iss.get(op);
you are trying to read something that has already been read. You need to reset the streams read pointer:
iss.clear();
iss.seekg(0); // start again
std::cout << "iss fail bit: " << iss.fail() << std::endl;
iss.get(op);