Ignore a block of code that fails c++ [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 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++

Related

Why comment cannot execute and ignore by the compileres ,in which header file it's defination exit? [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 2 years ago.
Improve this question
I am unable to understand the behavior of comments in C++. Why comment not executed by the compiler , what's the main reason, and in which header file the definition of comment can exit.
Thanks
Comments (single line or multi lined) are not a part of the executable/ library. It is just in place, basically to document the code and make it easier to understand what's going on. Sometimes when the same source or header file is edited by multiple programmers, the other programmers might not know what a particular statement would do, how much it costs (performance and/ or memory), if it might throw an error and so on. The compiler basically ignores these statements as they are not needed to build the actual compiler output.
C++ has two main comment syntaxes,
//: Single line comments.
/* */: Multi line comments.

How can I know whether a file is being used by other process by using c++ under Linux? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 4 years ago.
Improve this question
I want to copy a file to another file. But it always failed. I checked my code carefully, but I still can not know what's wrong with it. So I guess may be one of the two files is being used by other process. So firstly, I want to check whether they are being used by other process.
If so, is there any solution to stop the procedure by using c++??
If your code always fails, I would assume that it's because there is a problem with the code, because that's where the problem is in approximately 99.999%1 of the cases.
That said, there isn't much point in checking if you can open a file before you try, since some other process might open (or lock, or remove, ...) the file between your checking and your opening it.
The reliable method is to just go ahead and try to open the file, and then handle failure gracefully (which you need to do anyway).
Footnotes:
1 Scientific fact that I just made up.

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 does this stanford lirbrary mean? C++ simpio.h getInteger() [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'm trying to understand this library that stanford uses for their CS course but i can't just use this feature in my CS course back in my home. How do i emulate this feature? i was wondering is this a getline(cin, integer)? or is this something to do with fstream? The reason being, i'm trying to go through the lecture series at UDEMY.
Here is the definition.
int getInteger(string prompt = "");
Reads a complete line from cin and scans it as an integer. If the scan
succeeds, the integer value is returned. If the argument is not a
legal integer or if extraneous characters (other than whitespace)
appear in the string, the user is given a chance to reenter the value.
If supplied, the optional prompt string is printed before reading the
value.
Usage:
int n = getInteger(prompt);
You can download simpio.h here: http://www.eecs.wsu.edu/~cs150/prog/libs.htm
If you can't or don't want to use the library, you could study its source code.

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.