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



我正在为一个程序编写代码。它将有三个文本文件:-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为真。问题是我想不出一个好方法来正确地将这些单词存储在数组中。

下面是我的代码:

#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, "%sn", word);} //the copy machine
fclose(sp);
fclose(sp2);
return 0;
}

这使用一个数组来存储最多10行,最多19个字符的2个单词。将#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.txtn");
        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.txtn");
        return 1;
    }
    if ( ( list2 = fopen ( "list2.txt", "w")) == NULL) {//open list2 for writing
        printf ( "could not create list2.txtn");
        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, "%sn", word);//not in the change array
        }
        else {
            fprintf ( list2, "%sn", acChange[match][1]);//write the second word to the file
        }
    }
    fclose ( list1);
    fclose ( list2);
    return 0;
}

尝试将其转换为逗号分隔的行而不是列,并将其存储到数组1中。

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

如果长度不同,则对所比较的元素进行更改。如果你想在检查长度之外添加另一个条件,你可以包括额外的条件,这取决于你想过滤字符串的深度。

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

我不确定这是否有帮助,但如果你能更具体一点,也许我能帮上更多的忙。

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,所以它应该相当相似。我给了你一些逻辑让你继续

最新更新