gpt4 book ai didi

c - 将文本文件的第一个和第二个单词存储到两个数组中的最佳方法

转载 作者:行者123 更新时间:2023-11-30 17:14:12 25 4
gpt4 key购买 nike

我正在为一个程序编写代码。它将包含三个文本文件:-list1.txt 有一列单词,例如:

Cat 
Dog
Fish
Computers

-change.txt 有两列文本,例如

Dog  Marbles
Computers Store

-list2.txt 为空

程序将获取list.txt,并将其单词存储在list2.txt中,只有当某个单词是更改中的第一列单词时,它才会存储第二列中的单词。这样,当程序编译时,list2.txt 将如下所示:

Cat 
Marbles
Fish
Store

我想到的方法是创建两个字符数组,一个用于第一列单词,第二个用于......好吧,第二个。然后编写一个漂亮的 if 并写入第二个值(如果为 true)。问题是我无法找到一种将这些单词正确存储在数组中的好方法。

这是我现在的代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
///////////OPENING THE TEXT FILES
FILE *sp ;
sp= fopen("list.txt", "r");
if(sp==NULL){
printf("can't open list");
exit(1);
}

FILE *change;
change = fopen("change.txt", "r");
if(change==NULL){
printf("can't open change");
exit(1);
}

FILE *sp2;
sp2 = fopen("list.txt", "w");
if(sp2==NULL){
printf("can't open list2");
exit(1);
}

char word[100];
char key[20]; //the char array for the element TO replace
char mod[20]; //the char array for the element TO BE replaced

while(fscanf(sp, "%s", word)==1){
fprintf(sp2, "%s\n", word);} //the copy machine

fclose(sp);
fclose(sp2);
return 0;
}

如有任何帮助,我们将不胜感激。

最佳答案

它使用一个数组来存储最多 10 行、2 个最多 19 个字符的字。将#define LINE 10 中的 10 更改为更大的数字以处理更多行。对于超过几百行,请考虑为change.txt单词对动态分配内存。
从change.txt 文件中读取单词对后,将逐字读取list1.txt 文件。将每个单词与变化单词对的第一个单词进行比较。如果找到匹配项,则存储匹配项的索引并用于将该对的第二个单词打印到 list2.txt 文件中。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define LINE 10
#define WORDLEN 20

int main()
{
//array for list of words up to LINE lines, two words of up to WORDLEN characters
char acChange[LINE][2][WORDLEN] = {{{0}}};
char word[WORDLEN] = {0};
int line = 0;
int each = 0;
int match = 0;
FILE *list1 = NULL;
FILE *list2 = NULL;
FILE *change = NULL;

if ( ( change = fopen ( "change.txt", "r")) == NULL) {
printf ( "could not open change.txt\n");
return 1;
}

while ( ( fscanf ( change, "%19s", word)) == 1) {//read the pairs of words
strcpy ( acChange[line][each], word);
each++;
if ( each == 2) {//got second word, advance to next line
each = 0;
line++;
}
if ( line >= LINE) {//do not go beyond array dimension
break;
}
}

fclose ( change);

if ( ( list1 = fopen ( "list1.txt", "r")) == NULL) {//open list1 for reading
printf ( "could not open list1.txt\n");
return 1;
}

if ( ( list2 = fopen ( "list2.txt", "w")) == NULL) {//open list2 for writing
printf ( "could not create list2.txt\n");
return 1;
}

while ( ( fscanf ( list1, "%19s", word)) == 1) {//read a word
match = -1;
for ( each = 0; each < line; each++) {
if ( strcmp ( acChange[each][0], word) == 0) {// is it in the change array
match = each;//save the index
break;
}
}
if ( match == -1) {
fprintf ( list2, "%s\n", word);//not in the change array
}
else {
fprintf ( list2, "%s\n", acChange[match][1]);//write the second word to the file
}
}

fclose ( list1);
fclose ( list2);

return 0;
}

关于c - 将文本文件的第一个和第二个单词存储到两个数组中的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30444652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com