Linux C machine exercises

Source: Internet
Author: User

File read/write operations

Fopen fclose fgetc fputc

View code

# Include <stdio. h> # Include <Stdlib. h> Int  Main () {File * FP;  Char  Ch;  If (FP = fopen ( "  File.txt  " , "  W " ) = NULL) //  If the file does not exist, it is automatically created. No parentheses should be missing !!  {Printf (  "  Cannot open this file! \ N  "  ); Exit (  0  );}  Else  {Printf (  "  File Open successfully! ----- Writeonly \ n  " );} Printf (  "  Input character from keyboard and end input with character '#' \ n  "  );  While ( 1  ) {Ch = Getchar ();  If (CH = '  #  '  )  Break ;  Else  {Fputc (CH, FP );}}  If (Fclose (FP) = 0  ) {Printf (  "  File write successful and closed \ n  "  );}  If (FP = fopen ( "  File.txt  " , " R  " ) = Null) {printf (  "  Cannot open this file! \ N  "  ); Exit (  0  );}  Else  {Printf (  "  File Open successful ------- readonly! \ N  "  );} Printf ( "  Read from this file! \ N  "  );  While ( 1  ) {Ch = Fgetc (FP );  If (Ch! = EOF) printf (  "  % C  "  , CH );  Else              Break ;}  If (Fclose (FP) = 0  ) {Printf (  "  \ Nfile read successful and closed \ n  "  );}} 

Fgets fputs

View code

# Include <stdio. h> # Include <Stdlib. h> # Include < String . H>Typedef  Struct  {  Char Name [ 10  ];  Char College [ 20  ];} Student;  Int  Main () {File * FP; Student Stu; Student stu_read;  If (FP = fopen ( "  File.txt " , "  W  " ) = Null) {printf (  "  Cannot open this file! \ N  "  ); Exit (  0  );}  Else  {Printf (  "  File Open successfully! ----- Writeonly \ n "  );} Printf (  "  Name:  "  ); Fgets (STU. Name,  10  , Stdin); printf (  "  College:  "  ); Fgets (STU. College,  20  , Stdin );  If (Fputs (STU. Name, FP )! = EOF & fputs (STU. College, FP )! =EOF) {printf (  "  Write successful! \ N  "  );}  If (Fclose (FP) = 0  ) {Printf (  "  File close \ n  "  );}  If (FP = fopen ( "  File.txt  " , "  R  " ) = Null) {printf (  "  Can not open this file! \ N  "  ); Exit (  0  );}  Else  {Printf (  "  File Open successfully! ----- Readonly \ n  " );} Fgets (stu_read.name,  10  , FP); fgets (stu_read.college,  20  , FP); printf (  "  Student name is % s \ n  "  , Stu_read.name); printf (  "  Student college is % s \ n  "  , Stu_read.college );  If (Fclose (FP) = 0 ) {Printf (  "  \ Nfile read successful and closed \ n  "  );}} 

Fread fwrite

View code

# Include <stdio. h> # Include <Stdlib. h> Int  Main (){  Struct  Student {  Char Number [ 6 ];  Char Name [ 20  ];  Char  Sex;  Int  Age;  Int  Score;} s [  2 ] = {{ "  00001  " , "  Peter " , '  M  ' , 19 , 250 },{ "  00002  " , "  Betty  " , '  F  ' , 18 ,268  }};  Struct Student ss [ 2  ];  Int  I, J; File * FP;  If (FP = fopen ( "  File. dat  " , "  WB +  " ) =Null) {printf (  "  Cannot open this file! \ N  "  ); Exit (  0  );} J = Sizeof ( Struct  Student );  For (I = 0 ; I <= 1 ; I ++ ){  If (Fwrite (& S [I], J, 1 , FP )! = 1  ) {Printf (  "  Write error! \ N  "  ); Exit (  0  ) ;}} Fflush (FP); printf (  "  Write successful! \ N  "  ); Rewind (FP); printf (  " Begin to read file... \ n  "  );  For (I = 0 ; I <= 1 ; I ++ ) {Fread ( & SS [I], J, 1  , FP); printf (  "  % S, % s, % C, % d, % d \ n  "  , Ss [I]. number, ss [I]. name, ss [I]. sex, ss [I]. age, ss [I]. score) ;}fclose (FP );} 

 

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.