【发布时间】:2021-12-30 22:57:26
【问题描述】:
我有这个代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* ptr = fopen("data.txt","r");
char filename[100];
if (ptr==NULL)
{
printf("no such file.");
return 0;
}
char buf[100];
while (fscanf(ptr,"%*s %*s %s ",buf)==1)
printf("%s\n", buf);
printf("Create a file \n");
scanf("%s", filename);
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n", filename);
exit(0);
}
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}
}
它将完整内容从一个文件复制到另一个文件。我只需要复制以5 作为最后一个字符的字符串(3 列)
例如 Data.txt 看起来像这样:
Alex 10B 4
John 10A 3
Kate 10C 5
在我将在执行期间创建的文件中只能复制Kate 10C 5 字符串。我已经尝试了几个小时,但我不知道该怎么做。你能帮帮我吗?
【问题讨论】:
标签: c string file copy multiple-columns