将字符串数组写入到txt文件中
有五个文件名存储在字符串数组中,欲将其逐行写入到txt文件中保存到磁盘上。
利用fprintf对文件进行格式化输出
void Filewrite()
{
FILE *fp;
char * name[] = {"filen1", "file2", "file3", "file4", "file4"};
fp = fopen("E://test.txt", "w");
for (int i = 0; i < 5; i++)
{
fprintf(fp, "%s\n", name[i]);
}
fclose(fp);
}
使用fputs函数逐行写入
char InputStr="this is ok";
FILE fp = fopen("C:\1.txt", "wt");
if(fp != NULL)
{
fseek(fp, 0, SEEK_END);
fputs(InputStr, fp);
fputs("\r\n",fp);
fclose(fp);
}
逐行读取txt文件中的字符串 text.txt文件内容如下 file1 file2 file3 file4 file4 欲将其读入到一个字符串数组中 利用fscanf对文件进行格式化输入 void Fileread() { int i = 0; FILE *fp; fp = fopen("E://test.txt", "r"); char name[6][10];
while(!feof(fp))
{
fscanf(fp,"%s", name[i]);
printf("%s", name[i]);
i++;
}
fclose(fp);
}
删除文件中第n行: sed 'Nd' file
删最后一行 sed '$d' sed-demo.txt
删除5-7行 sed '5,7d' sed-demo.txt
替换第5行的30为50
sed -i '5 s/30/50/' /etc/test.txt
其他sed相关用法可参考: 参考链接:https://juejin.im/post/5d669f4bf265da03ea5a8f82