读写文件的简单问题
我想通过串口传送文件,现在串口通讯正常了,怎么读写文件呢?
[解决办法]
自己定义文件格式
[解决办法]
用write和read函数读写文件就是了.
[解决办法]
写文件:
#include <stdio.h>
#include <string.h>
void main(void)
{
FILE *file;
char *path= "d:\\text.txt ";
char *buffer= "testtesttest ";
file=fopen(path, "wb ");
fwrite(buffer,sizeof(char),strlen(buffer),file);
}
续写文件:
#include <stdio.h>
#include <string.h>
void main(void)
{
FILE *file;
char *path= "d:\\text.txt ";
char *buffer= "\r\ntesttesttest ";
file=fopen(path, "ab+ ");
fwrite(buffer,sizeof(char),strlen(buffer),file);
fclose(file);
}
读文件:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main(void)
{
FILE *file;
char *path= "d:\\text.txt ";
file=fopen(path, "rb ");
fseek(file,0,SEEK_END);
int length=ftell(file);
fseek(file,0,SEEK_SET);
char *buffer=(char *)malloc(length);
fread(buffer,sizeof(char),length,file);
printf( "%s\n ",buffer);
fclose(file);
}