[cfe-dev] [libc++] Writing to a read-only std::filebuf

Benini Fulvio via cfe-dev cfe-dev at lists.llvm.org
Tue Apr 4 08:58:08 PDT 2017


I tried the following code on Ubuntu16.10 with the package 
libc++-dev:amd64 (3.7.0-1).
There is a different behavior between libstdc++ and libc++:

 >clang++-3.9 -stdlib=libstdc++ test.cpp && ./a.out
97 98 97 98 -1 -1

 >clang++-3.9 -stdlib=libc++ test.cpp && ./a.out
97 98 97 98 -1 100

Is this a bug?
Thanks,
Fulvio Benini



#include <fstream>
#include <iostream>

int main() {
{ //Create a temporary file.
   std::filebuf file;
   file.open("tmp_file", std::ios_base::in | std::ios_base::out
                         std::ios_base::trunc | std::ios_base::binary);
   std::cout << file.sputc('a') << ' ';
   std::cout << file.sputc('b') << ' ';
}
{ //Re-open the file read-only.
   std::filebuf file;
   file.open("tmp_file", std::ios_base::in | std::ios_base::binary);
   std::cout << file.sbumpc() << ' ';
   std::cout << file.sbumpc() << ' ';
   file.pubseekpos(0);
   std::cout << file.sputc('c') << ' ';
   //The next sputc() should return EOF?
   std::cout << file.sputc('d') << ' ';
}
   std::cout << std::endl;
}



More information about the cfe-dev mailing list