如何解决从一个文件过滤数据并将该数据写入新文件
我正在尝试将文件 (customers.txt) 复制到新文件 (filtered_customers.txt) 中,同时过滤数据。现在我的程序只读取 "customers.txt" 文件中的所有内容并将其复制到新文件中。我正在尝试更改此设置,以便仅将第一列中具有“dublin”AND 最后一列中具有 1 的条目复制到新文件中。
我正在考虑使用 strcmp()
函数将第一列中的值与字符串“dublin”进行比较,并使用此方法将最后一列中的整数与“1”进行比较,但我已经以前从未真正进行过任何文件处理,所以我完全不知道从哪里开始。任何有关如何解决此问题的建议将不胜感激,谢谢
#include <stdio.h>
#include <stdlib.h> // For exit()
#include <conio.h>
int main()
{
FILE *fptr1;
FILE *fptr2;
char c;
char* filename = "C:\\Users\\uzair\\Desktop\\Comp-Programming 2\\customers.txt";
char* filename2 = "C:\\Users\\uzair\\Desktop\\Comp-Programming 2\\filtered_customers.txt";
// Open customers.txt file for reading
fptr1 = fopen(filename,"r");
if (fptr1 == NULL)
{
printf("Cannot open file %s \n",filename);
exit(0);
}
// Open file for writing
fptr2 = fopen(filename2,"w");
if (fptr2 == NULL)
{
printf("Cannot open file %s \n",filename2);
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c,fptr2);
c = fgetc(fptr1);
}
printf("\nContents copied From ---\n%s\nTo\n%s\nSuccessfully :)\n",filename,filename2);
fclose(fptr1);
fclose(fptr2);
return 0;
}
customers.txt
dublin Dunne 865463389 21 1
dublin Milford 865438990 22 0
dublin Mowlds 876765443 24 1
dublin Wang 873456789 22 1
dublin Smith 875432234 25 1
dublin Henry 876654429 22 0
dublin Gupta 896765443 24 1
Wicklow Monahan 865432245 22 1
Wicklow Brunsdon 865678894 22 1
Wexford Tyson 865434566 22 0
Wexford browne 865564766 20 1
dublin Dunne 890065443 27 1
dublin Connolly 876733999 20 0
Louth Jennings 987645673 60 1
Mayo Wang 876232123 29 1
解决方法
您只是逐个字符地复制文件。
输入是一系列具有单独列的行。
我们必须根据与列的匹配来接受/拒绝一行。
所以,首先,我们想一次阅读/检查整行。所以,我们想要 fgetc
fgets
然后,我们需要将行拆分为其列。有多种方法可以做到这一点,但最简单的是制作该行的复制。使用 strtok
拆分该副本。
然后,第一列可以与 Dublin
进行比较,最后一列可以与 1
进行比较。
如果两者匹配,我们可以用fputs
输出[原始]行
这是您重构的代码:
#include <stdio.h>
#include <stdlib.h> // For exit()
#include <string.h>
//#include <conio.h>
#define BUFMAX 1000
int
is_valid(const char *src)
{
char *cp;
char buf[BUFMAX];
int tokcnt = 0;
char *toklist[20];
int match;
// make a copy of the line (because strtok is destructive of the buffer)
strcpy(buf,src);
// split line into tokens/columns keeping track of the number of columns
while (1) {
cp = strtok((tokcnt == 0) ? buf : NULL," \t\n");
if (cp == NULL)
break;
toklist[tokcnt++] = cp;
}
// check for match
do {
// first column must be "Dublin"
match = (strcmp(toklist[0],"Dublin") == 0);
if (! match)
break;
// last column must be "1"
match = (strcmp(toklist[tokcnt - 1],"1") == 0);
if (! match)
break;
} while (0);
return match;
}
int
main()
{
FILE *finp;
FILE *fout;
char *filename = "customers.txt";
char *filename2 = "filtered_customers.txt";
// Open customers.txt file for reading
finp = fopen(filename,"r");
if (finp == NULL) {
printf("Cannot open file %s \n",filename);
exit(0);
}
// Open file for writing
fout = fopen(filename2,"w");
if (fout == NULL) {
printf("Cannot open file %s \n",filename2);
exit(0);
}
// Read contents from file
char buf[BUFMAX];
while (1) {
// read line
char *cp = fgets(buf,sizeof(buf),finp);
if (cp == NULL)
break;
// output the line if it matches our criteria
if (is_valid(buf))
fputs(buf,fout);
}
printf("\nContents Copied From ---\n%s\nTo\n%s\nSuccessfully :)\n",filename,filename2);
fclose(finp);
fclose(fout);
return 0;
}
,
这里有一些想法。请注意,如果最后一列之前的空格是制表符而不是空格,则这将完全失败,并且它不会验证每行中有 4 列。毫无疑问,它遗漏了许多其他边缘情况。留给读者的练习:
/* A limited implementation of awk '$1 == "Dublin" && $NF == 1' */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE * xfopen(const char *path,const char *mode);
int
main(int argc,char **argv)
{
FILE *fptr1 = xfopen( argc > 1 ? argv[1] : "-","r");
FILE *fptr2 = xfopen( argc > 2 ? argv[2] : "-","w");
char buf[256];
while( fgets(buf,sizeof buf,fptr1) != NULL ){
size_t len = strlen(buf);
if( buf[len - 1] != '\n' ){
fprintf(stderr,"input error: line too long\n");
exit(EXIT_FAILURE);
}
if( strncmp("Dublin",buf,6) == 0 &&
strncmp(buf + len - 3," 1\n",3) == 0 ){
fwrite(buf,1,len,fptr2);
}
}
fclose(fptr1);
fclose(fptr2);
return 0;
}
FILE *
xfopen(const char *path,const char *mode)
{
FILE *fp = path[0] != '-' || path[1] != '\0' ? fopen(path,mode) :
*mode == 'r' ? stdin : stdout;
if( fp == NULL ){
perror(path);
exit(EXIT_FAILURE);
}
return fp;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。