当前位置: 代码迷 >> 综合 >> Linux下读写文件操作
  详细解决方案

Linux下读写文件操作

热度:16   发布时间:2024-03-06 02:28:55.0

 Linux下读写文件操作

#include<stdio.h>
typedef struct Student
{
    int no;
    char name[10];
    int score;
}Student;
int main(int args,char *argv[])
{
    //打开文件
    FILE *fp=fopen("test.txt","w");
    if(fp=NULL)
    {
        perror("fopen");
        return -1;
    }
    
    //写文件
    Student s1={1,"Mike",89};
    /*
    &s1:要写入内容的变量首地址
    sizeof(Student):往文件写入内容的块大小
    1:表示往文件写入的块数目,
    fp:表示操作的文件
    ret:写入成功时表示写入文件的块数目,否则返回0
    */
    int ret=fwrite(&s1,sizeof(Student),1,fp);
    
    printf("ret=%d",ret);
    
    //关闭文件
    fclose(fp);
    return 0;
}