70,019
社区成员




#include <stdio.h>
#include <string.h>
int main()
{
char* file_from = "f.dat";
char* file_to = "t.dat";
char* be_del = "china";
FILE *fpf, *fpt;
char line_str[100];
fpf = fopen(file_from, "r");
fpt = fopen(file_to, "w");
if(fpf == NULL || fpt == NULL)
{
printf("open file error!\n");
return 1;
}
while(fscanf(fpf, "%s", line_str) > 0)
{
if(strcmp(be_del, line_str) != 0)
fprintf(fpt, "%s\n", line_str);
}
printf("operation done!\n");
fclose(fpf);
fclose(fpt);
return 0;
}
#include <stdio.h>
#include <string.h>
#define MAX_LINES 100
#define MAX_CHARS 100
int main()
{
char* file_name = "f.dat";
char* be_del = "china";
FILE *fp;
char line_str[MAX_CHARS];
char file_content[MAX_LINES][MAX_CHARS];
int i = 0, j;
fp = fopen(file_name, "r+");
if(fp == NULL)
{
printf("open file error!\n");
return 1;
}
while(fscanf(fp, "%s", line_str) > 0)
{
if(strcmp(be_del, line_str) != 0)
strncpy(file_content[i++], line_str, MAX_CHARS);
}
fclose(fp);
fp = fopen(file_name, "w");
if(fp == NULL)
{
printf("open file error!\n");
return 1;
}
for(j = 0; j < i; j++)
fprintf(fp, "%s\n", file_content[j]);
printf("operation done!\n");
fclose(fp);
return 0;
}