La biblioteca C estándar escribe y lee archivos

API correspondiente: fopen (abrir archivo)
fwrite (escribir archivo)
fread (leer archivo)
fseek (mover el cursor)

#include <stdio.h>
#include <string.h>

int main()
{
    
    
        //FILE *fopen(const char *path, const char *mode);

        FILE *fp;
        char *str = "qingyanbeisheng  hen shuai";
        char readBuf[128] = {
    
    0};


        fp = fopen("./qybs.txt","w+");

        //size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);

        fwrite(str,sizeof(char),strlen(str),fp);

        //int fseek(FILE *stream, long offset, int whence);

        fseek(fp,0,SEEK_SET);

        //size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);     

        fread(readBuf,sizeof(char)*strlen(str),1,fp);

        printf("text content:%s\n",readBuf);

        return 0;
}

Nota: El segundo parámetro en fwrite es un puntero, recuerde agregar "" al configurar los permisos

Supongo que te gusta

Origin blog.csdn.net/m0_52953249/article/details/115277124
Recomendado
Clasificación