Help with Structures+Input/Output with files

[somewhat urgent]Not sure how to put some things together and how to c

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 2284 Views - Last Post: 24 March 2008 - 05:32 AM Rate Topic: -----

#1 Izumi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 13-March 08

Help with Structures+Input/Output with files

Posted 18 March 2008 - 02:58 AM

The problem: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 2500 people who can use a given service – for example to use internet for free; knowing that those 2500 people are divided into four groups (numbers 1-700, 701-1401, 1402-2003, 2004-2500), 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).


So far I've got the following codes, but I'm not sure how to put them into one.

This one is letting me write in the person's number and times used the internet, but after closing and reopening the program it is not allowing me to add new information - it deletes the old one and writes in new information.
Also, I'm not sure how to go to a given person's number to add to their times used the internet.

#include <stdio.h>
#include <stdlib.h>
#define MAX 81
void main()
{char number [MAX], ch;
int page; FILE *fp;
fp=fopen("servicenumber.txt","r+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);
}




For checking the size of the file I use the following program, but I'm not sure how to include it in the big program's code and how to have that if it gets bigger than 4MB to get an error message:

#include <iostream.h>
#include <fstream.h>
using namespace std;

int main () {
  long begin,end;
  ifstream myfile ("servicenumber.txt");
  begin = myfile.tellg();
  myfile.seekg (0, ios::end);
  end = myfile.tellg();
  myfile.close();
  cout << "size is: " << (end-begin) << " bytes.\n";
  return 0;
}



Other things I am not sure how to do:
1. How to get the final results - how many people, how many times, how many were those with 10+ times using it,etc? It should get updated with each entry of the program.
2. Each time I compile the codes it gives me error for using the 'using namespace std;' :
Error PAGES1.CPP 3: Declaration syntax error
3. How should I order things so that the final information from (1) to be always ontop when I'm opening the file and the rest of the persons' numbers - times used internet to follow beneath it?
4. How do I devide the groups so that I know if someone is from group one and thus being allowed to use the service for more than 10 times without being included in the list for this.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Structures+Input/Output with files

#2 Izumi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 13-March 08

Re: Help with Structures+Input/Output with files

Posted 19 March 2008 - 12:33 AM

*sighs*
Was This Post Helpful? 0
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Help with Structures+Input/Output with files

Posted 19 March 2008 - 01:53 AM

Well assuming that the 4 MB limit exists only for the file of people, you can declare each person to be an integer and have a counter for each of them that holds a same integer. If the people are maxed then you have a total of 10,000 bytes (2 bytes per integer on a 16 bit system), which is roughly 1 kB.

Assuming again, that your code doesn't count toward the limit, you could even make the numbers long ints which is 4 bytes per each variable.

Try the following:

#include <iostream>
//instead of <iostream.h>


Was This Post Helpful? 0
  • +
  • -

#4 Izumi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 13-March 08

Re: Help with Structures+Input/Output with files

Posted 19 March 2008 - 02:59 AM

I actually tried this code, but it gives me error messages as if I haven't included any libraries, that's why I added the '.h'

And how exactly would the counter be coded?
something like:

 for (int i=1;i<=2500;i++)
? where i would be the count of people?
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Help with Structures+Input/Output with files

Posted 19 March 2008 - 04:42 AM

I suppose when you created all the people (as an array?):

peopleArray[2500]

for(int i = 0; i < 2500; i++)
	//something--filling the array
	peopleArray[i] = currentPerson

//Counter for # of times on internet
personLoggedOn++;
//I would make this as a separate function



etc....
Was This Post Helpful? 0
  • +
  • -

#6 Izumi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 13-March 08

Re: Help with Structures+Input/Output with files

Posted 19 March 2008 - 06:26 AM

Hm... I see... But if I use this it will count only the part of how many were all the people who used it. How can I devide it into people from group 1 and the rest of the people? And in the end how would the final counts be included in the file?
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Help with Structures+Input/Output with files

Posted 19 March 2008 - 07:35 AM

Have four different arrays:

groupOne [700]; //really 0-699

for(int i = 0; i < 700; i++)
	 //make group one

//repeat for other groups

//or make a multidimensional array


Was This Post Helpful? 0
  • +
  • -

#8 Izumi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 13-March 08

Re: Help with Structures+Input/Output with files

Posted 19 March 2008 - 08:06 AM

Okay. I think I understood.
And about the file problem? How can I add new things, find where the old ones are and update them?
Was This Post Helpful? 0
  • +
  • -

#9 Izumi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 13-March 08

Re: Help with Structures+Input/Output with files

Posted 19 March 2008 - 09:42 AM

I figured out adding new things to the file.
I need to figure out how to UPDATE the written in information.

#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);
}

Was This Post Helpful? 0
  • +
  • -

#10 Izumi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 13-March 08

Re: Help with Structures+Input/Output with files

Posted 19 March 2008 - 04:06 PM

*waits and hopes for more help*
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Help with Structures+Input/Output with files

Posted 19 March 2008 - 04:31 PM

Update the file that the information is written in or update the program with recently required information?
Was This Post Helpful? 0
  • +
  • -

#12 Izumi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 13-March 08

Re: Help with Structures+Input/Output with files

Posted 19 March 2008 - 04:40 PM

Update the file.
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.
Was This Post Helpful? 0
  • +
  • -

#13 Izumi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 13-March 08

Re: Help with Structures+Input/Output with files

Posted 20 March 2008 - 03:13 AM

?_?
Was This Post Helpful? 0
  • +
  • -

#14 Izumi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 13-March 08

Re: Help with Structures+Input/Output with files

Posted 21 March 2008 - 12:14 AM

*waits*
Was This Post Helpful? 0
  • +
  • -

#15 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Help with Structures+Input/Output with files

Posted 21 March 2008 - 05:06 AM

I'm not that familiar with those two functions, check out MSDN on them and it should tell you how to implement them.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3