Fstream does not create file if it is not there unless it is opened in an append mode. Append mode however forbids overwriting the original data, and I want to be able to seekp and seekg anywhere inside.
Unlike question asked here: std::fstream doesn't create file
I have a requirements for it not be in either append or truncate mods and still be able to to seekg and seekp anywhere in thise file.
It's a little more work, but you could attempt to open the fstream, and if it fails open (and close) an output file stream (ofstream) which creates the file, and then try to reopen the fstream.
Related
I want to open a file for read and write with std::filebuf, but create the file if it doesn't exist yet. If it already exists, do nothing. I want to freely write into the file, without any "always jump to end before each write" behavior. What open mode should be used?
According to the table , there appears to be no combination of flags to do that.
What's the best option to ensure this effect?
I'm trying to get the size of a text file in c++.
I know that you can seek to the end of the file, then do file.tellg(), but I read it doesn't work in files opened in text mode.
Do I have to open it in binary mode then? And then what? Open it again in text mode? That doesn't seem very efficient...
Is there a better way to do it?
Thanks.
Is it possible in C++ to change the output mode of an open FILE* without closing and reopening it?
I have a FILE* opened in mode std::ios_base::out and would like to switch to std::ios_base::app in a more elegant way.
If you want to write at the end of the file, simply seek to the end of the file, it's the easier solution.
On POSIX system (including linux, unix, ...) and most likely on Windows too, it is not possible to change the open mode of a file once it has been openned. Thus you have to close it and reopen it.
If your operating system has some particularities which permit this and you don't care about portability, you can achieve this by using the right system call in a std::filebuf subclass, then creating your own std::fstream subclass.
I am using VC++ to generate a text file and write (via fstream) data continuously to it. I have another application2 (NOT C++) which accesses that same file which c++ appends. At the instant application2 accesses the file, new data from C++ program cannot be written to it. It seems like the new data from c++ goes to some temporary file. when application2 closes the file, new data gets updated to the file. I want the data to be written to the file in real time and be read at the same time by application2. What should I do in c++ to make new data appear in the file which is opened by application2?
C++ side:
int realTimeValues // this variable is updated continuously
FILE * pFileTXT;
while(1)
{
pFileTXT = fopen ("realTimeData.txt","a"); // Opening file in append mode
fprintf (pFileTXT, "%d\n",realTimeValues); // saving values to file
fclose (pFileTXT) // Closing the file
}
On the application2 side I can't tell how exactly its opening this file. The application is Universal Real-Time Software Oscilloscope. In the menu there is an option "read from a file"
Ugh. There are a number of things at work:
By default, fprintf output may be buffered in a private ram buffer. This buffer is flushed only as needed or when you do fclose. If you want to force the data out, there's a setbuf call you can make (read the docs) or explicitly call fflush after each fprintf.
I do not know if fopen allows for simultaneous readers. Probably not, given the buffering behavior. If you want read/write sharing, you should look at the documentation to see if there is an explicit mode parameter to enable this.
Unfortunately, this only covers the part you can control. If the reader (an application you do not control) doesn't support shared reading, then you're out of luck.
When I use the std::ifstream to open a file that has been written in dos format, the ifstream does not seem to be able to open the file correctly since when I call good() on the stream afterwards it fails (returns false). I tried opening the file in binary mode as well as the default "in" mode and neither worked. If I convert the file to unix using dos2unix, everything works fine.
The goal behind being able to do this is that I want to be able to read a file and parse it, but I cannot guarantee that the file has not been saved in dos (Windows) or unix (Linux) format. Ideally, I would like to be able to use the ifstream.
Any suggestions?
Thanks!
The file format will NOT affect your ability to open it.
It is more likely that your path is not correct.
It seems extremely unlikely that GCC would do such a thing.
I suggest doing my_ifstream.exceptions( ios::failbit | ios::badbit ); before opening it, and running in the debugger. Then you can see where it ceased to be good.
Also, opening the file in binary mode (ios::in | ios::binary) should eliminate any possibility of the implementation being choosy over the contents of the file.