Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I was reading http://www.cplusplus.com/reference/cstdio/fflush/ and I was curious about what it means. According to the website it says:
If the given stream was open for writing (or if it was open for updating and the last i/o operation was an output operation) any unwritten data in its output buffer is written to the file.
What does the output buffer to file mean?
Some streams buffer output data and do not write to the device immediately. fflush forces the contents of the stream's buffer, if there is one, to be written to the device and the buffer cleared.
Generally, when data is intended to be written to a file, it is stored in a construct known as a "buffer". Once the buffer has reached a certain threshold, all the data stored in the buffer will be written out to the file at once.
Fflush empties the buffer and forces all changes to be written to the file. This is particularly useful when you are done writing to the file, since it is good practice to flush the buffer before closing the file (thereby making sure that all data has been successfully written to the file).
This goes for other types of filestreams too.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
In C (or C++) is there a way to receive data from a socket, but instead of reading it into a buffer in memory, it "receives" it into a file. I know that the usual way to do this is to receive the data into a char buffer, then write the buffer into a file.
Is there a function like sendfile(), which transports the data directly between files and sockets, but instead of sending data from a file to a socket, it receives data from the socket into a file?
There is no equivalent to sendfile that works the other way around. But there is splice() that transfers between 2 file descriptors, one of them must refer to a file.
So what you can do is socket -> pipe, pipe -> file. Whether that is still a gain over a buffer you have to measure. Splice is limited by the pipe buffer size while a read/write can work in arbitrary units reducing the number of syscalls.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Recently, I've come across this video that shows how to use mmap() with file io. However, I can't find the video of his that documents the function. I don't have an understanding of what it is, why it exists, nor how it relates to files.
Too much of the jargon is flying over my head to make sense of it. I had the same problem with sites like Wikipedia.
Files are arrays of bytes stored in a filesystem.
"Memory" in this case is an array of bytes stored in RAM.
Memory mapping is something that an operating system does. It means that some range of bytes in memory has some special meaning.
Memory mapped file is generally a file in the file system, which has been mapped by the operating system to some range of bytes in memory of a process. When the process writes to the memory of the process, the operating system takes care that the bytes are written to the file, and when the process reads from the memory, operating system takes care that the file is read.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am writing a C++ application, which needs to call a library function that expects FILE* opened for read. I have no code of that function - it a black box to me.
The data that I need to pass to the function is in memory in char* buffer of a known size. Ideally, I need to wrap a my buffer in a FILE* structure and pass it in, but I need all the stdio functions that normally works on a FILE* to work on what I pass in, for I don't know which of the functions it calls - definitely fread, possibly fseek too.
The code is rather a performance-sensitive one, so I would like to avoid writing the buffer to disk just so that I could create a FILE* from it by fopen. Is there a way to make a FILE* that would allow from my buffer in memory?
Something like stringstream in c++?
My code needs to be able to run both on windows and linux.
I've seen quite a few questions where people try to write via FILE* to memory. My case is rather a reverse - I need to present the existing buffer as a readable FILE*.
Thanks a lot!
P.S. yes, Using a C string like a FILE* is exactly what I'm asking, somehow I couldn't find it earlier...
Still, if you could suggest a solution on windows, that would be very helpful!
Posix requires the fmemopen() function, which does exactly what you ask.
Glibc certainly has it.
Can't say for sure about Windows, but it appears that MinGW includes newlib, which also implements it. If that's any help.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I've seen in code a direct read into std::string where the contents are intended to be interpreted as a string as follows:
std::string note;
note.resize(n);
read( ¬e[0], n );
Assume that n is of a fixed size, as in a parsing scenario.
Are there are any issues with reading directly into a string? I have seen a lot of uses of ifstreams, but it seems excessive in this case.
First, if it's a text file made by several lines, I find the std::string class not a good choice as a "container"; I would prefer just a std::vector<char>, or if you want to do some additional parsing and break the file into its single lines, a std::vector<std::string>.
I'd also pay attention to the encoding used by the file: is it UTF-8? Is it some other char-based encoding?
For example, if the file is UTF-16, reading it as a raw sequence of bytes into a std::string would be very misleading (and bug prone).
Moreover, it's important also to pay attention to the size of the file. If you have a gigantic text file (e.g. 5GB) and you are building a 32-bit Windows application, your code won't work (as 32-bit processes on Windows are limited to 2GB by default). In such cases, reading the file content in smaller chunks (or using memory-mapped file techniques with smaller "views" on the file) may be a better alternative.
Look at it this way: “What's the worst that could happen?”
Are you obtaining a file from the local user? And if they supply a file that's too big, perhaps their machine will thrash, or even kill your program with an out-of-memory error?
Do you expect the user to do that often enough to worry about?
Alternatively:
Are you obtaining the file from a network source or untrusted user? Would giving that user the ability to potentially thrash your system or kill your application constitute a risk?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
We all know that we have ifstream and ofstream classes with their own functionality: reading, writing, line by line reading etc.
ifstream input_file("test.in") ;
ofstream output_file;
output_file.open("test.out");
So, let assume now we want to extend reading/writing functional via creating some class (lets call it BackUp) and somehow make ifstream and ofstream to use BackUp's reading/writing instead (only). As far as I understood this principle is called acquaintance?
The main difference should be that when opening a file a copy of it should be created somewhere. Then we work with this starting file like usually, we overwrite it by our results and if the procedure was successful only then the temporary copy is deleted.
Yes, I know, that it's probadly better just to write and use a common function, but this is not my goal.
I have also made a schemes to understand the logic:
Before:
After:
Goal
I want the target file to be overwritten if the entire operation is successful, so if the program crashes, if one excidently turns off the PC etc. the data from the original file would be saved atleast somewhere.
Question itself
I need to grasp the idea itself how this can be done in general case but It would be nice if one would use this particular this example to explain.