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 years ago.
Improve this question
Eventhough there are similar questions like here or here, I have a question about a different case.
By using C/C++, I want to write some bytes to a file. Initially file has data. Simply, I update the content of file : I open, write and close. However, if it fails during write and if we are unable to handle the failure (for example, application crash, interrupt, electricity shutdown etc.), what is guaranteed in output file between the list below? Which situation can happen, which cannot?
File may be empty (Deleted existing values and couldn't write new ones)
File stays locked
File may contain both old values and new values (i.e. first 5 lines are new values, last 5 lines are old values)
File may contain old value.
Anything other that I don't expect?
If you can give me OS independent approaches, I would be glad
Thanks
Write the new data to a file with the same name, but with a 'tmp' extension. Flush and close the tmp file. Delete the original file. Rename the tmp file to the original file name.
On startup, scan the folder for all files. Delete all tmp files whose name part matches an existing 'source' file, (ie. system was interrupted during the tmp file write). Rename any tmp file whose name part does not match an existing source file, (the tmp file was written, the original file was deleted but the system was interrupted before the rename).
This system depends upon the atomicity of deleting the original file. If it succeeds, you get the new data, if it fails, you get the old data. You should never get bits of each.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I was planning to use IFileOperation::DeleteItems(items) and IFileOperationProgressSink::UpdateProgress(workTotal, workSoFar) to track the progress of moving files and folders to recycle bin. But this only works well when I call it on a list of files to be deleted/move to recycle bin. Then UpdateProgress() is called correctly after each file, returning gradually increasing number of deleted items. But when I try to delete one large folder containing multiple nested subfolders and thousand of files, UpdateProgress() keeps returning 0 (as the number of files done) and then suddenly it returns for example 8000 (like 8000 files in the large folder were deleted). There is no gradual progress, it just jumps from 0% to 100 %. Is this the normal behavior? Or am I doing something wrong. I would like to show the code but even the relevant snippet is terribly long.
I simply modified the sample on Windows-classic-samples and tried at least 8,000 files in the same subfile. It works for the UpdateProgress method, the process first discover all the items, then delete it in process.
This is the sample I used.
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 5 years ago.
Improve this question
I'm doing a program where I have to read information from a file and it can be a file with any extension. Do you know any way in C++ to read and display the information from a file different than the .txt extension?
There is actually no real difference that would be made by a file extension, so you can - in theory - open any file you want, for example by using std::ifstream. However, files that are not using a human-readable encoding (like txt/json/... files), you propably want to open it in binary mode (you can specify this to std::ifstream).
However, if you actually want some usefull information of some specific, not human readable file (like for example the dimensions of a images saved as a png file), you need way more detailled code. To read, for example, information from an png file, you have to open it in binary mode using std::ifstream, and then interpret the read bytes yourself to get any usefull information out of it. So you actually have to know how the specific file format you want to read is encoded, and need to have (or to implement yourself) a decoder for that specific file format.
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 6 years ago.
Improve this question
Put simply, I have a browser widget I'm using in FLTK Gui toolkit which can be loaded using a file like so.
browser::load("textfile.txt");
The problem is I don't want to create a physical text file, just an invisible one so I can use it as an argument for browser::load above. I plan to use this invisible text file by loading it with the values I'm going to place in my browser....then use it like this.
browser::load("invisible_textfile.txt");
Is it possible to do this in C++?
I have already tried using ifstream::rdbuf() which probably has nothing to do with this. I'm not even sure what to call this so I'm just calling it an invisible textfile for now.
I'm using windows 7 64 bit. MinGW compiler.
Let's say what you want to add is equivalent of text file like this:
One
Two
Three
But you don't want to have the text file. So one piece of code which would do the same thing is this:
const char *lines[] = { "One", "Two", "Three", 0 };
for(int i = 0 ; lines[i] != 0 ; ++i)
browser.add(lines[i]);
Documentation link for that overload of add
Please refine your question, or perhaps ask a new question, if you want more help on how to get lines with your data.
It depends a lot on what browser::load() actually does internally, but let's assume that it will look for your filename and load it.
You probably already know how to read / write a standard file (e.g. http://www.cplusplus.com/doc/tutorial/files/). Now, if you just want to hide the file from the user, you can set OS specific hidden flags (e.g. windows). I'm assuming that's not what you actually want to accomplish here. (You could obviously also create a temporary file that you delete again, but it's not an elegant solution either.)
What you might want to use is a named pipe. Under Linux you can create these with mkfifo and then stream content through those file objects.
The general point is, though, unless the browser API allows you to pass it a complete string holding the text file or a stringstream, you will need a file object.
If your only target system is NTFS, there is a good answer on creating virtual files over here:
How to create a virtual file?
But in the end, you probably want to create an actual file (in your case probably a temporary one, though). I would recommend placing that file into the systems temporary path.
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 8 years ago.
Improve this question
I am busy writing a program that is constructing minions (from Despicable Me) using ASCII of the minions that are in a text file. What I want to do is be able to have the user input what features he/she wants the minion to have. Then, from this, the program will "decorate" this minion by putting the text files together as the minion he/she wanted.
For example:
The user inputs that he wants a tall minion with one eye and a big smile - I will have the features of the minions already created in text files with certain parts missing from the minion. What I want to do is "overlay" the tall minion text file on top of the one eyed minion text file, on top of the big smile minion text file so in the end it creates a full minion.
I was just wondering if this is possible to do? Could I construct minions like this or would I have to do it the hard way by changing the format of the text files completely to add in features using fstream.
This link is where these minion ascii is from: http://textart4u.blogspot.com/2013/08/minion-what-text-art-despicable-me.html
Ignore the issue of minions of different dimensions for a moment. Assuming you have some character that represents transparency (or not-part-of-the-feature characters, e.g. white space), you can assign each feature a depth, iterate over the selected feature files from background to foreground, and write each non-transparent character in the correct position to an ongoing minion text file.
Here's some very rough pseudo code that assumes each file has the same number of lines.
Write minion base (most background) to output file.
For each feature file, from background to foreground:
Seek to beginning of output file.
For each line in feature file / output file:
Extend output file's line to match feature file's line if necessary.
For each character in line of feature file / output file:
If feature file's char is not transparent:
Overwrite current char in output file with current char in feature file.
If this is not flexible enough, you can assign each character of a feature file a depth, and iterate over the feature files in any order, only overwriting the current output file's character if the feature file's character is closer to the foreground. You'd need to encode the depth somehow -- using a corresponding depth file is an obvious way.
The tricky part might be supporting minions of different dimensions. To handle that, it may be enough to specify vertical/horizontal offsets for every feature / minion body type combination, and use that to adjust where you write the feature in the ongoing output file. Alternatively, it may be easier to duplicate each feature file with appropriate modifications to make it fit a given minion body type. Both of these methods require some manual grunt-work, but if you don't have to worry about that many body types, these simple methods are probably preferable to more complicated but flexible methods.
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 5 months ago.
Improve this question
I'm we're trying to figure out if there would be a way to convert a .txt file to a .pdf file. Here's the catch. This needs to be done behind the scenes, and on the fly. Meaning, with a radio control selected, OnOK would create a .txt file. Behind the scenes, at run time, we would like for the .txt file to be converted to a .pdf file. Ideally we would like this to be done by running an executable in the background. The executable would take input "File.txt" and output "File.pdf". We're using C++ and Visual Studio 6.
Does anyone have any experience on this? Is this possible?
libHaru may do what you want. Demo.
This a2pdf tool will probably do the trick with minimal effort. Just be sure to turn off perl syntax highlighting.
http://perl.jonallen.info/projects/a2pdf
I recommend using this open source library.
Once you have the base for generating PDF documents programmatically, you would still need a method for converting the text to the PDF elements, while keeping the text flow and word wrapping. This article may help. Please pay attention to the DoText(StreamReader sr) function. It takes text and purge it into separate lines within the PDF document, keeping the rendered within the margins.
On of the simpler methods that has worked for 3 decades e.g. more than one quarter of a century is place a postscript header before the text then use ghostscript ps2pdf it is the same method as used by some commercial apps such as acrobat
at its most basic
Copy heading.ps file.txt printfile.ps
GS -sDEVICE=pdfwrite printfile.ps printfile.pdf
Master Example can be seen here
How to modify this plaintext-to-PDF-converting PostScript from 1992 to actually specify a page size?