linux C上機練習

來源:互聯網
上載者:User

檔案讀寫操作

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) //如果檔案不存在,自動建立。 不能少一個括弧!!    {        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);}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.