C语言文件io读写
有时间就练习下,如果很难, 那就明天再来一次。 为了学习NDK 先把基础过了一遍, 虽然只是基础语法,但是不做练习,NDK无从下手。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <io.h>
/**
* @brief C语言 io读取
*
* @return int
*/
int main(){
//读取文件
char * filenameStr = ".\\cache\\readtext.txt";
FILE * file = fopen(filenameStr,"r"); //读取文件需要保证readtext.txt文件必须存在
if(!file){
printf("%s 文件打开失败\n",filenameStr);
}
char buffer[10];
while (fgets(buffer,10,file)){
printf("gets %s",buffer);
}
fclose(file);
//写入文件
char * writefilenameStr = ".\\cache\\wirtetext.txt";
FILE * wfile = fopen(writefilenameStr,"w"); //读取文件需要保证readtext.txt文件必须存在
if(!file){
printf("%s 文件打开失败\n",writefilenameStr);
}
fputs("wirte file Success!",wfile);//写入文件
fclose(wfile);
//文件的复制
FILE * fileCopyName = ".\\cache\\readtextcopy.txt";
FILE * filebefore = fopen(filenameStr,"rb"); //二进制文件读取
FILE * filecopy = fopen(fileCopyName,"wb"); //二进制文件写入
if(!filebefore||!filecopy){
printf("文件打开失败\n");
}
int buf[1024];
int len;
while ((len = fread(buf,sizeof(int),sizeof(buf)/sizeof(int),filebefore))!= 0){
fwrite(buf,sizeof(int),len,filecopy);
printf("读取len is %d \n"+len);
}
fclose(filebefore);
fclose(filecopy);
return 0;
}