Okay, I know it sounds crazy, but I really need to know if this is possible. My question first isn't complete, for that I wanna continue here.. So, how do I create a program that in some how a piece of code run only first time and not only, even if you close and run the program again I want that piece of code to not execute. So for example if the first time the program execute let's say it will ask you to input a new password, then if the user closes the program and run it again the program should ask only the password was given at the very first time, and not ask user to set a password again. How to make that program remember that the pass was set and needs to go over that.. ?
even if you close and run the program again I want that piece of code to not execute
You have to store the state (memory of past events) somewhere. Generally, you'd store it in a file on your computer, and read from this file to determine if you need to re-run parts of the code.
Keep on a file a 0 or 1 depending on whether the password has been set.
Could also have a default password and when a check reveals that hasn't been changed, ask for a new password to replace it and allow execution to proceed beyond that point, or keep going with no request required.
Okay, until here all clear and understand the principle and I thought the same think, to use a boolean flag and store it in a file, and next time program executes reads the file and if the flag is set to true it jumps over the that code and go from that point ahead.
So the first code is to search for that file, and if the flag doesn't exist, I mean a void text.txt to set the first value by asking the user to set a new password. When password is set a true flag is saved on the file and next time the program executes is searching for that flag and if it's true user won't set a new password instead user will be asked for the password it was set for the first time right?
Well to think about the logic of doing things is quite easy, the problem comes when start to write code, maybe for some of you guys is a piece of cake but I'm still a beginner and I like what I'm doing and I am really fascinating, but certain things are very hard to understand, could anyone show me where to start at list how to store that flag on a file ?
Thank you !
For a password, obviously it should be stored encrypted. And where this code displays the entered correct password for testing, the required code would go there.
Nope, I don't have troubles with no one, I just wanna add this code to a prev. program I posted last days with info file and address.. blah blah and somehow I was thinking to add this piece of code when deleting files so when try to delete something, a password is requested ,
That's why I asked for... anyway my prev. code runs with or without this idea, but was pretty cool stuff to add some extra.
Ouu,, oauu , that's awesome. I only have a question
I tried your code but one thing to ask remains .. do I have to define __STDC_WANT_LIB_EXT1__
before I include the <stdio.h> cause I can't use the gets_s(char *str, rsize_t n), I can only use gets(char *str), since this is removed in C11 ?
Yes - for some compilers. You don't seem to need to for MS VS but seems like you need to for your compiler.
As with all bounds-checked functions, gets_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including stdio.h.
If you don't have gets_s() then you can use a #define
The code works just fine... I have found only 1 problem
On line 31 constint l = strlen(pass) - 1;
I've changed -1 with -2.
When the pass.txt is saved, after the new password another 2 new lines are created, so when the new password is requested the new pass and the pass won't match.