在文件input.csv文件中,我们有数据如下
Apple
Pear
Litchis
Pineapple
Watermelon
现在我们将input.csv文件下的读取并写入到output.csv文件,我们会用到fopen函数
函数原型:FILE * fopen(const char * path,const char * mode)
#include <stdio.h>
#include <stdlib.h>
#include <string.h> int main(){
char line[];
FILE *in=fopen("input.csv","r");//fopen可以创建一条数据流;r,表示读
FILE *out=fopen("output.csv","a");//a,表示追加数据到文件
while(fscanf(in,"%79[^\n]\n",line)==){
fprintf(out,"from input:%s\n",line);
}
//用完数据流之后,需要关闭他们,即使他们自己会关闭,因为通常情况下一个进程最多可以有256个数据流,数量是有限的
fclose(in);
fclose(out);
return ;
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAagAAACBCAIAAAAaBLndAAATDklEQVR4nO2d73MT17nH9Yf4jd/yOiYGnOGS3tLMNc1NU5w7nTaN3UzAhmsCASsYbDAVAUSgKMa/ZFuWcDD+LcmADVzi1kohMR0PN2lTg5MmN8FxkxhIcWKoTTOj+2Kt1e6e3bMr7dndI+33mc8wQtrVnn12z0dnfz32PEAgrIxkMjmLMBxVVVVObzFXhCeJMB3ShDrdFu5idnb28UAJMEgkEoH7bAiP079w+RDinprE6EYeEB/Ex2d4JhCmQ9hZhU7udFv4CogP4uMzPE53jXwIiE8rID6Ij8/wON018iEgPq0QcuL0Tp5LAfHZEx6nuwbvkUgkxsbG4vH48PDw4ODgwMBAf39/b29vT09PJBKJx+MTbhWfkcxAfEKMhifOnxyM+8Kxgx2x/a2x2ubo3tPDe04Nveof2Hqo91C3OCXEZ094yB36xdDsfwXnNp3+8pnAlz85JeOZwJc/bZr9defsnoHPbe5mTsWlS5fGx8fHxsZGRkZisVg0GhX6eV9fX3d3dzAYnNATX77m00hmSPEd656sa5965eSNF4/e+NURGb8+dmPrqUl/5P3Rq+861R8sivOnohdaL42cGIy/8Xb8UDh2oCNW1x6tbR7efWpw+5GOl/aLU0J89oSH3KE3B+f2XX58NPFDcCp58Mo/g1NJKccT/3rl3IPNbXfs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K6oNroz1hPOM4rKoBeLpyzVQ5D1j8QkTGyofTyk9b2Trm0gpyB8YX9Wt9K+Z63wqp0Z8+Sy+3uqKgHimz1chjPvUc5WB+AwUec9SfIbKx2PEB8yTT+JD6XlirqjkPFrQT/nbF/IC8TrL1S/ynqX4jJWPp5SeN7D1zaQU5A0M/ubGqHhez+FDXZSeJ+eS3Uci/RJqgXjd5eoVec9WfIbKx2t9ubGtr7aNgOtgMOKr9K8RL25kbT024itM92HXl55X3gtC3LBCKRBvZLnUIu/Zi0/vmwsLVlFKzxvY+mrbCLgOXp/cyB/4fPYOAFcD8VkNxAcAd0B8VgPxAcAdEB8AwHXwJb4n++6XljwBAACWAvEBAFwHxAcAcB0QHwDAdUB8AADXAfEBAFwHG/Gln1rLtkIBxAcAsI3/B2+6NmnWyxnNAAAAAElFTkSuQmCC" alt="" />
fopen还有很多模式,比如
w,写入文件,如果文件不存在,就创建文件并写入,若文件存在,则将之前的数据覆盖
还有a+,w+,r+等等,但是有些编译器不支持,大家可以参考这里fopen