How to count number of logins in C language - arrays

Well I'm making a program that initially asks to login or register.
I need to make a counter for each time the program is accessed (after the login).
C language using the array of functions and file Login Register
The method to log in and register follows the one up.
My thing is due to the lifetime of the var, because I know the moment the program ends the var just restarts.
So far I tried many ways. By macros but once again soon as the program ends it restarts.
I'm starting now to make one saving in files.
I started just now so the function is very simple, but since I only have more 2 hours to deliver the work so I hope you guys help me.
Simple function:

At the definition of fp you should call the function fopen. From the documentation of fopen:
w+
Open for reading and writing. The file is created if it
does not exist, otherwise it is truncated. The stream is
positioned at the beginning of the file.
The file gets truncated and you need to read it before you open it for writing.
fp = fopen("contador.txt", "r");
if (!fp) {
perror("fopen");
return -1;
}
fscanf(fp, "%d", &contador);
fclose(fp);
fp = fopen("contador.txt", "w");
You can use fscanf for parsing the file and storing the value into your variable.

Related

BISON FLEX. Unexpected output when an input file is provided

I'm trying to create a calculator with flex and bison which gets a set of operations (results are assigned to variables). The calculator uses a memory to store these variables. When I do this calculations via terminal (stdin keyboard) everything works fine. However, when I try to use a file, nothing is stored in the memory.
I have a .y and .l
This is what I have specified in the .y.
extern FILE* yyin;
And in the main method:
yyin = fopen(argv[1], "rt");
yyparse ();
listTofile(list, argv[2]); //method that backups memory into a file
fclose(yyin);
When I for example type A = 2 + 2, the file will store the name variable and the result. This perfectly work when the prompt ask me an instruction.
However, when a file is provided parser detects all but does not store anything (checked using flex -d)
With flex, generally you need to call yyrestart(yyin); to switch to a new input source to reset all the buffering source. That said, if you never read anything from yyin before your fopen call, this should not make any difference, as there should not be any cached buffer contents to reset.

How to store a variable out of a program in C? [duplicate]

This question already has an answer here:
I want to store a value even after my program ends
(1 answer)
Closed 7 years ago.
I am writing a program to organize some items. It starts off by getting the date/time and randomly generating a list of some items. But the next time you run it, the program shouldn't generate any of the same items as last time. So basically the date and time is like a seed for random generation.
But is there a way to store the data outside of the program? That way I can close the program and my PC, but when I return it still remembers the variable.
I've thought about it and it seems like the only way is to go in the program and manually define the variable. So I haven't tried anything yet and it would be pointless to show my code. Please tell me if there is a way to externally store the variable or if you have any alternate solutions.
You have two choices :
Using a file
It's the easiest way to do this :
The first time, you just need to open (and create) a file (man fopen), write your variable in it (man fwrite).
Every next time, you'll need to open the file, and read your variable from it (man fread).
Using a database
Harder, but better if you need to store many datas. This isn't your case, so just go with a file
General Idea is to use Non Volatile RAM .
You can mimic the same by using a file.
Need to take care of writing the contents of the context before closing the program and reading the same during the program startup.
For programming with Files ,you can refer any of good sites.
To store the data externally, so that it is saved, use a file.(http://www.cplusplus.com/reference/cstdio/fopen/)
FILE *fp;
/* open a file in write mode */
if (fp = fopen("myfilename", "w")) {
fprintf(fp, "Hello world");
if (fclose(fp) == EOF) /* fclose returns EOF on error */
perror("fclose");
} else
perror("fopen"); /* error */
/* open the file in read mode */
char line[80];
if (fp = fopen("myfilename", "r")) {
fgets(line, 79, fp); /* read the first line */
printf("%s", line);
if (fclose(fp))
perror("fclose");
} else
perror("fopen"); /* error */
Classical way to save many variables of different types is serialization. But there are no standard serialization method in C, so you need to implement your own or use existent (for example, tpl or gwser). If you need to save only one-two variables it is simplier to use fopen() + printf() + fclose().

Completely close a file in c

I need to read, and then write to a file. I don't want to use "r+" because I completely overwrite it anyway. But I am unable to completely close the file. If I try to open the file for the second time, the applications crashes (can't open). Does anyone know how to completely close the file.
And this is not the actually code, just a summary of what I want to do:
FILE* f;
fopen_s(&f, "test.txt", "r");
// read file and edit data
fclose(f);
f = 0;
fopen_s(&f, "test.txt", "w");
fprintf(f, "%c", data);
fclose(f);
fclose() closes the file. It doesn't half close it. The notion of "completely closing" a file doesn't exist. A file is either open or closed. There's no in-between.
Although in your code you're using fopen_s() to open the file. I've no idea what that function is. I assume it works like fopen(), but instead of returning a FILE pointer, it instead stores it in its argument.
So the answer to your question is: to "completely" close a file, use fclose(). As you already do. That means the problem you're having lies elsewhere.

c, how do i detect if another process is writing to a file

I do open large files with fopen under c with a kind-of observed folder logic.
I'd like to fopen (or at least to fread) the file as soon as it has been copied completely to my observed folder.
How can i detect if another process is writing to a file?
Is that possible with standard-c?
Thanks
There's no way to do it with standard C. The best you can do is heuristically notice when file stops changing, e.g. read the last few blocks of the file, sleep n seconds, then read the blocks again and compare against the previous read. You could also try just watching the end of file seek pointer to see when it stops moving, but for large files (size greater than what will fit in a signed long) the POSIX function ftello() is required to do it portably.
It's kind of "rude" but you could keep trying to open it in write mode; as long as it's being written to by some other file, the fopen will fail. Something like this:
FILE* f;
while( (f = fopen( fname, "w" )) == NULL ) {
sleep( 100 ); // we want to be moderately polite by not continually hitting the file
}

C Programming fopen() while opening a file

I've been wondering about this one. Most books I've read shows that when you open a file and you found that the file is not existing, you should put an error that there's no such file then exit the system...
FILE *stream = NULL;
stream = fopen("student.txt", "rt");
if (stream==NULL) {
printf(“Cannot open input file\n”);
exit(1);
else {printf("\nReading the student list directory. Wait a moment please...");
But I thought that instead of doing that.. why not automatically create a new one when you found that the file you are opening is not existing. Even if you will not be writing on the file upon using the program (but will use it next time). I'm not sure if this is efficient or not. I'm just new here and have no programming experience whatsoever so I'm asking your opinion what are the advantages and disadvantages of creating a file upon trying to open it instead of exiting the system as usually being exampled on the books.
FILE *stream = NULL;
stream = fopen("student.txt", "rt");
if (stream == NULL) stream = fopen("student.txt", "wt");
else {
printf("\nReading the student list directory. Wait a moment please...");
Your opinion will be highly appreciated. Thank you.
Because from your example, it seems like it's an input file, if it doesn't exist, no point creating it.
For example if the program is supposed to open a file, then count how many vowels in it, then I don't see much sense of creating the file if it doesn't exist.
my $0.02 worth.
Argument mode:
``r'' Open text file for reading.
``r+'' Open for reading and writing.
``w'' Truncate file to zero length or create text file for writing.
``w+'' Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated.
``a'' Open for writing. The file is created if it does not exist.
``a+'' Open for reading and writing. The file is created if it does not
exist.
Your question is a simple case. Read above description, when you call fopen(), you should decide which mode shall be used. Please consider why a file is not created for "r" and "r+", and why a file is truncated for "w" and "w+", etc. All of these are reasonable designs.
If your program expects a file to exist and it doesn't, then creating one yourself doesn't make much sense, since it's going to be empty.
If OTOH, your program is OK with a file not existing and knows how to populate one from scratch, then it's perfectly fine to do so.
Either is fine as long as it makes sense for your program. Don't worry about efficiency here -- it's negligible. Worry about correctness first.
You may not have permission to create/write to a file in the directory that the user chooses. You will have to handle that error condition.

Resources