簡體   English   中英

將文本文件的第一個單詞和第二個單詞存儲到兩個數組中的最佳方法

[英]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

我想到的方法是制作兩個char數組,一個用於第一個列字,第二個用於....嗯,第二個。 然后鬃毛如果好,並寫第二個值,如果為真。 問題是我想不出一種將這些單詞正確存儲在數組中的好方法。

現在是我的代碼:

#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;
}

在這里的任何幫助,將不勝感激。

嘗試將其變成逗號分隔(修剪空格)行而不是列,並將其存儲到數組一中。

當您要檢查是否有更改時,請使用新值創建另一個數組,並使用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

 }

我不知道這是JavaScript的C語法,所以應該相當相似。 我給你一些邏輯讓你走

它使用一個數組來存儲最多10行,最多2個單詞,最多19個字符。 #define LINE 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM