Random file read/write operations

Source: Internet
Author: User
# Include <stdlib. h>
# Include <stdio. h>
# Include <conio. h>
# Define N 4
Extern unsigned _ floatconvert;
# Pragma extref _ floatconvert
Struct student
{
Int num;
Char name [10];
Char sex;
Int age;
Float grade;
} STU [N] ={{ 101, "Zhang", 'M', 19, 95.6 },
{102, "Wang", 'F', 18, 92.4 },
{103, "Zhao", 'M', 18, 91.5 },
{104, "Li", 'F', 17, 98.7 }};
Void save ()
{
File * FP;
Int I;
If (FP = fopen ("D: \ student. dat", "WB") = NULL)
{
Printf ("cannot open this file! \ N ");
Return;
}
For (I = 0; I <n; I ++)
If (fwrite (& STU [I], sizeof (struct student), 1, FP )! = 1)
Printf ("file write error! \ N ");
Fclose (FP );
}
Void read ()
{
File * FP;
Int I;
Struct student stud [N];
If (FP = fopen ("D: \ student. dat", "rb") = NULL)
{
Printf ("cannot open the file! \ N ");
Return;
}
For (I = 0; I <n; I ++)
{
Fread (& stud [I], sizeof (struct student), 1, FP );
Printf ("% d \ t % s \ t % C \ t % d \ t %. 1f \ n ", stud [I]. num, stud [I]. name, stud [I]. sex, stud [I]. age, stud [I]. grade );
}
Fclose (FP );
}

Void main ()
{
Clrscr ();
Save ();
Read ();
Getch ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.