Android_NDK C文件操作
读取文本文件
void main(){//打开文件 (文件名,模式[r,w,,rb,wb(二进制读写)])char * filename = "C:\\a.text";FILE * file = fopen(filename,"r");if(file){char buffer[10];while(fgets(buffer,10,file)){printf("%s",buffer);}//关闭文件fclose(file);}
}
写文本文件
void main(){//打开文件 (文件名,模式[r,w,,rb,wb(二进制读写)])char * filename = "C:\\b.text";FILE * file = fopen(filename,"w");if(file){fputs("hello",file);//关闭文件fclose(file);}
}
拷贝文件
void main(){//打开文件 (文件名,模式[r,w,,rb,wb(二进制读写)])char * filenamea = "C:\\a.text";char * filenameb = "C:\\b.text";FILE * filea = fopen(filenamea,"rb");FILE * fileb = fopen(filenameb,"wb");if(filea && fileb){int buffer[1024];int len;//sizeof buffer = sizeof(buffer)while(len = freads(buffer,sizeof(int),1024,filae) != 0){fwrite(buffer,sizeof(int),len,fileb);}//关闭文件fclose(filea);fclose(fileb);}
}
文件大小获取
void main(){//打开文件 (文件名,模式[r,w,,rb,wb(二进制读写)])char * filename = "C:\\b.text";FILE * file = fopen(filename,"w");if(file){fseek(file,0,SEEK_END);long file_size = ftell(file);//关闭文件fclose(file);}
}
文件加密
void main(){//打开文件 (文件名,模式[r,w,,rb,wb(二进制读写)])char * filename = "C:\\b.text";FILE * file = fopen(filename,"rf");if(file){char f;while((f = fgetc(file)) != EOF){//操作}//关闭文件fclose(file);}
}
文件切割
void main(){//打开文件 (文件名,模式[r,w,,rb,wb(二进制读写)])char * filename = "C:\\b.text";char * filenameD = "C:\\b_%d.text";FILE * file = fopen(filename,"wf");long size = getfileSize(filename);int filecount = 3;long preFileSize = size / filecount;char ** filenames = (char**)malloc(sizeof(char*)*filecount);for(size_t i = 0;i<3;i++){filenames[i] = malloc(sizeof(char) * 30);sprintf(filenames[i], filenameD, i);}int i = 0;for (; i < file_number; i++){FILE* cur_file = fopen(file_names[i],"wb");int start = i*preFileSize;int end = (i + 1)*preFileSize;if (i == file_number - 1){end = file_size;}for (int j = start; j < end; j++){fputc(fgetc(file),cur_file);}fclose(cur_file);free(file_names[i]);}fclose(file);free(file_names);}