linux C上机练习

文件读写操作

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);
}
原文地址:https://www.cnblogs.com/tangzhengyue/p/2668727.html