Help with a program?

How am I supposed to create a file taking in mind that I have only 4MB for use for the entire program in which has to be written down the information about 2000 people who can use a given service – for example to use internet for free; knowing that those 2000 people are divided into four groups (numbers 1-500, 501-1001, 1002-1503, 1053-2000), each person being allowed to use the service for ten times? And in the beginning of each month this file should be cleaned (emptied from the information), but before being cleaned it gives information about how many people used the service, how many times the service was used as a whole, how many times people from the first group used it, and how many people used it over ten times (people from the first group are allowed to use it more, so they don’t get into this list).
Please, someone it's kind of an urgent.
Things I can't figure out:
The program should be able to:
1. Update the already written in information
(for example when let's say person #300 has used the service 1 time - we get the 300 - 1, but when he uses it again it should be updated to 300 - 2)
2. Adding new information

I guess it should work out with fseek and SEEK_SET? but I don't know how to use these ones.


So far I have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdlib.h>
#define MAX 81
void main(
)
{char number [MAX], ch;
int page; FILE *fp;
fp=fopen("my.txt","a+b");
if(fp==NULL)
{printf("File cannot be opened!\n");
exit (1);
}
printf ("Write in data or ctrl+z for end!\n");
printf ("PersonNumber:");
while (fgets(number, MAX-1,stdin)!=NULL)
{fputs(number,fp);
printf("Times used internet:");
scanf("%d", &page);
fflush(stdin);
fprintf(fp,"%d",page);
fputs("\n",fp); fputs("\n",fp);
printf("\nPersonNumber:");
}
fclose(fp);
fp=fopen("my.txt", "r+b");
if(fp==NULL)
{printf("File cannot be opened!\n");
exit(1);
}
printf("Content of the file:\n");
while(fgets (number, MAX-1,fp)!=NULL)
{fputs(number,stdout);
fscanf(fp,"%d",&page);
printf("%d", page);
ch=fgetc(fp); putchar(ch);
ch=fgetc(fp);putchar(ch);
}
fclose(fp);
}


Last edited on
Old C functions are lame. Learn how to use streams.
That's what I need help with.
I did. For about a billion times. Still don't understand it. I'm a complete newbie.
1
2
3
4
ofstream MyStream; //declare your "stream" (lets you write to a file)
MyStream.open("file.txt"); //opens a file called "file.txt"
MyStream<<"stuff"<<endl; //use it like cout, the file now contains stuff and a enter/return
MyStream.close(); //closes whatever file is currently open 


So if you wanted to do that with your code, you would probably make a function that initializes it, maybe using a for loop like:
1
2
3
4
5
6
7
8
9
10
bool init(void) { //returns true if succeeded, false if error
    ofstream stream;
    stream.open("log.txt");
    if(!stream.is_open()) return false;
    for(int a = 1; a <= 2000; a++) {
        stream<<a<<" 0"<<endl;
    }
    stream.close();
    return true;
}


And then make another function that is called when a person uses the program that would update the number...although that would require some chr/string manipulation...I don't remember exactly how I did it, maybe someone else could help you there.
Topic archived. No new replies allowed.