Changing behaviour of class in c++ via other class [closed] - c++

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.

Related

Efficient way to write same data to two files [closed]

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 years ago.
Improve this question
Just wondering, what would be the most efficient way to write same data to two files, on linux and C/C++.
For example, this is the most trivial way.
while(1) {
... getting data from somewhere ....
write(fd1, data, datalen);
write(fd2, data, datalen);
}
However, the disadvantage is that kernel needs to copy data twice even though the data is same.
Any thoughts?
what would be the most efficient way to write same data to two files
Write the data to one file only.
Copy that file to another. Use an OS call to do that efficiently (Copy a file in a sane, safe and efficient way).
Another way for step 2 would be to create a hard link (check link()).
However, please watch out of not becomning a victim of premature optimization. I this is not the bottleneck in your program, then just use the trivial, easy-it-read approach.

Ignore a block of code that fails c++ [closed]

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
Is there a way to just ignore a block of code only if it fails to execute in c++? Something like the try - except in python, it would help me a lot.
I'm trying to make a program that constantly reads some information of a file that contains a number, and then converts it to an integer with stoi().
The problem is that the file is constantly being modified by another program, and at some point the main program may read the file when it is being modified, giving an emty string and making the program fail when trying to convert it to an integer.
What I would like to do is make my program ignore all the loop if the stoi() fails, and simply wait until the loop is executed again to get actualized information. I know that this can be done in python with try and eccept, but I don't know how to do it in c++.
try block associates one or more exception handlers (catch-clauses) with a compound statement.
For more detail please refer try catch in c++

Save Values on Exit C++ [closed]

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 7 years ago.
Improve this question
I've been working on a C++ program which gets values and saves them for later use. The problem for me is saving the data on exit, and initializing it on opening. How can I do this?
You can use a lib like pugixml to easily write the data on a xml file and read it on program startup.
On linux, you can register a "program exiting" callback with the atexit function, this is the perfect place to put your xml creation code. There is probably something similar to the atexit function on windows :)
Edit:
Another alternative, like #molbdnilo said, is to leave the file writing code in the end of the main function, which would not require using global variables to hold the data that needs to be written.

what is this requirement telling me not to do? [closed]

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
Edit: I was only asking for a clarification on what my professor was requiring of me, I now understand what this requirement is saying and I believe that this has been thoroughly answered.
I have a project that i have to do for my c++ class and this is a requirement of the project.
Except for the print menu function, all user defined
functions(functions written by the programmer) must be called from
main. No user defined functions other than the print menu function can
be called from other user defined functions.
It is a strange requirement. During your homework you might write some functions. Or you might not, and your main() will be one unreadable big pile of lines that only you can read and understand, on a good day.
But, you can decide to pretify your code by moving some code from main() to some newly introduced functions. One function to read all data from input, one function to do something with that data, one function to write result to output. Such kind of things.
The requirement demands that all functions must be called only in main(). You must not call any function from any other function except main().
I guess this requirement makes it easy for the teacher to read your code.
That means you cannot nest functions, like this function definition
void A(){
int B();
}
then you are only able to call your functions from main()

structure reading C++ [closed]

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 have the following problem:
I have a configuration file that consists a description of fields , which I read it and then parse it. I want to move it into the code to compile it inside.
How would you do that as bug structure ??? or else ?
Thanks
I wouldn't move it into the code, I'd leave the configuration file as a configuration file.
If you really must do this, you can just embed the file as a string resource into the application and use that - that way you'd change only a minimal amount of existing code. The way you do this depends upon your platform.
If thats not feasible (for whatever reason) I'd set up a single configuration class / namespace to contain all the values.
It's not very clear what are you exactly asking.
If you are looking for on-the-fly code execution (like eval() function in some languages), then there is no such thing in C++. It's not an interpreted language which can be read and executed line-by-line, it needs to be compiled every time code changes. While it technically is possible to write self-changing code, it's probably not worth the effort.