fwrite/fread [newb]

This is a discussion on fwrite/fread [newb] within the C Programming forums, part of the General Programming Boards category; i.. newb here....need some advice n help I need to create a proggie to store student details in a .dat ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    7

    fwrite/fread [newb]

    i.. newb here....need some advice n help

    I need to create a proggie to store student details in a .dat file (binary format).
    each record is input individually.
    ex: 1 opti0n is add
    prompt all the fields needed for a record. then stored. next return to main menu.
    the max record can be stored in the file is 50...

    Q1.. how do i ensure that records added will not exceed 50?

    Q2.. not sure if my array used for input correct or not?

    Q3..fwrite code

    Q4.. si fread the same as fwrite?
    fread(rec,sizeof(struct student),1,ptr);

    Code:
    #include <Stdio.h>
    #include <conio.h>
    #define SIZE 50
    
    struct student{
    	char id;
    	char name[30];
    	char address[30];
    	struct date{
    		int mm,dd,yyyy;
    	};
    	struct date djoin;
    }rec[SIZE];
    
    void add(FILE *ptr)
    {
    	int x=0;
                    if((ptr=fopen("student.dat","a+"))==NULL)
    		ptr=fopen("student.dat","w");
    	printf("input id: ");
    	scanf("%s",&rec[x].id);
    	printf("input name: ");
    	scanf("%s",&rec[x].name);
    	printf("input address: ");
    	scanf("%s",&rec[x].address);
    	printf("input month: ");
    	scanf("%d",&rec[x].djoin.mm);
    	printf("input day: ");
    	scanf("%d",&rec[x].djoin.dd);
    	printf("input year: ");
    	scanf("%d",&rec[x].djoin.yyyy);
    	fwrite(rec,sizeof(struct student),1,ptr); // <- is the code correct?
    	x++;
    	fclose(ptr);
    }
    
    main(){
    .......
    .......
    case1: add(fpt);
    }
    thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > Q1.. how do i ensure that records added will not exceed 50?
    Surround the code with
    if ( x < SIZE )


    > Q2.. not sure if my array used for input correct or not?
    Seems OK


    > Q3..fwrite code
    Depends
    Do you want to write the whole array when you've finished all input, or write each record of the array as you finish input for a single student.

    You're kinda trying both at the moment

    > Q4.. si fread the same as fwrite?
    Yes

    One more thing
    Binary files should be opened like so
    ptr=fopen("student.dat","wb");

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    7
    > > Q3..fwrite code
    Depends
    Do you want to write the whole array when you've finished all input, or write each record of the array as you finish input for a single student.
    1) i want to write each record of the array after finish input for a single student. do i have to change my fwrite code?

    2) for the fread.. i'm not very clear how to write it
    let's say the records stored is around 30
    can i still use
    fread(rec,sizeof(struct student),50,read); // 50
    or i need to read each record n put it in array

    3) wat is the differences between
    fwrite(&record[x]...);
    fwrite(record,....);
    and also for the
    fread

    Code:
    void display(FILE *read)
    {
    	int y;
    	read=fopen("student","rb");
    	fread(rec,sizeof(struct student),50,read);
    	for (y=0;y<SIZE;y++)
    	{
    		printf("%s",rec[y].id);
    		printf("%s",rec[y].name);
    		printf("%s",rec[y].address);
    		printf("%d",rec[y].djoin.mm);
    		printf("%d",rec[y].djoin.dd);
    		printf("%d",rec[y].djoin.yyyy);
    	}
    	fclose(read);
    	getch();
    }

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,701
    fwrite(rec,sizeof(struct student),1,ptr);
    No.

    fwrite(&rec[x],sizeof(struct student),1,ptr);

    ...is the correct format.

    Second, if you are appending to the file, you must fseek to the end so that you don't overwrite the current entries. Also you stated that there musn't be more than 50 students in the file. Since you are appending, you will have to:

    1) measure the file first. Use ftell();

    2) divide the result by the sizeof(YOURSTRUCTURE)

    3) if the result of that calculation does not exceed 50, then subtract 50 - result to obtain the maximum number of students to be added.

    4) call fseek(ptr, 0L, SEEK_END);
    This is the requirement for binary mode appending.

    5) scan in the data and fwrite.
    fwrite will automatically move the file position indicator forward for you ( as will fread )





    wat is the differences between
    fwrite(&record[x]...);
    fwrite(record,....);

    The answer is simple. The first syntax is used with single objects whereas the latter is used with arrays.

    ie:

    STUDENT a;

    STUDENT b[10];

    ....

    fwrite( &a, sizeof(STUDENT), 1, ptr);

    fwrite( b, sizeof(STUDENT), 10, ptr); //..writes entire array...

    Same with fread...
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    7
    thanks!

    2) divide the result by the sizeof(YOURSTRUCTURE)
    do u mean like this?
    for the view part.

    Code:
    for(x=0;x<ftell(ptr)/sizeof(student);x++)
    {
    fread....
    .....
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [newb] how to use threads with sockets ?
    By jabka in forum C Programming
    Replies: 1
    Last Post: 08-07-2007, 11:51 AM
  2. [newb] Can someone explain the advantages of OOC?
    By TheUnknownFacto in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2007, 07:38 PM
  3. [newb] How is "!(1 && !(0 || 1))" true?
    By eddwills in forum C++ Programming
    Replies: 11
    Last Post: 02-18-2006, 07:19 AM
  4. struct not yet defined...[newb]
    By zguL in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2004, 06:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21