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

Best way to store the first and second word of a text file into two arrays

我正在为程序编写代码。 它将包含三个文本文件: -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 并写下第二个值 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;
}

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

尝试将其转换为逗号分隔(trim 空格)行而不是列,并将其存储到数组一中。

当您想检查是否有变化时,使用新值创建另一个数组并使用 for 循环比较两个数组的长度。

如果长度不同,则您比较的元素有变化。如果除了检查长度之外还想添加其他条件,您可以根据要过滤字符串的深度添加额外的条件。

我不知道您使用的是什么语言,但是有一些库可以使这项任务变得容易得多。例如,c# 有一个 exclude 方法来检查您从逗号分隔的文本文件中填充的列表中的差异。

我不确定这是否有帮助,但如果您能说得更具体一些,也许我可以提供更多帮助。

pseudo code
-open the text file and split every single word between each comma using a while
loop.
-store that in a 1d Array
-use that 1d array for the length you want to loop through
-store it into another array like this 
- (declare array 1)
  (declare array 2)


for (var i = 0; i < array.length; i++) {
                        {
                           newarray[i] == array[i]; 
                        }
for (var i = 0; i < array.length; i++) {
                        {
                          if (newarray[i].length < 1 )
                            { 
                    write down new values into your text file using
this new array

 }

我不知道 c 语法这是 javascript 所以它应该非常相似。我给了你一些让你前进的逻辑

这里使用数组存储最多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;
}