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);
}
复制代码

 

posted @   唐小喵  阅读(482)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示