2015-05-25 38 views
0

我正在爲程序編寫代碼。 這將有三個文本文件: -list1.txt有話像一列:將文本文件的第一個和第二個詞存儲到兩個數組中的最佳方法

Cat 
Dog 
Fish 
Computers 

-change.txt具有類似於

Dog Marbles 
Computers Store 

兩列正文-list2.txt爲空

該程序將採取list.txt,並將其單詞存儲在list2.txt中,只有當一個單詞是第一個更改的列字時,它纔會存儲第二列中的單詞。 這樣,當該程序將彙編中,list2.txt應該是這樣的:

Cat 
Marbles 
Fish 
Store 

我想這樣做的方法是做兩個字符數組,一個第一列字,和第二因爲......好吧,第二個。然後鬃毛一個很好,如果和寫第二個值如果是真的。 問題是我無法找到將這些單詞正確存儲在數組中的好方法。

這裏是我的代碼現在:

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

這裏任何幫助,將不勝感激。

+0

您能正確設置代碼格式並添加語言標記以吸引更多可以幫助您的人嗎? – Adalee

+0

這是功課嗎? –

+0

您是否擔心有些單詞可能會變得太長而無法存儲到數組中? –

回答

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; 
} 
+0

非常感謝! 該程序很好,我可以理解爲什麼。 對我來說唯一的小問題是「char acChange [LINE] [2] [WORDLEN] = {{{0}}}」的語法; 我的意思是這是變化詞的數組,但這是我第一次遇到一個3d數組 – aln447

0

嘗試將它轉換爲逗號分隔(trim whitespace)行而不是列,並將其存儲到數組一中。

當您想要檢查是否有變化時,使用新值創建另一個數組,並使用兩個數組的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的,所以應該是相當類似的。我給你一些邏輯讓你去

+0

我忘了添加,當你根據你想要的位置找到不同的位置時,查看數組中的push-pop方法。這會將您的元素帶到陣列的前面或後面 –

+0

感謝您的回覆。這是常規的C,對不起,忘了提。我想知道包含一個字符串,然後將change.txt中的文本存儲到它們的一維數組中。然後爲每個第二個元素創建一個for循環(所以使用這個鍵)。我不知道如何寫下比較algorythm代碼(用於IF)來確定。 – aln447

相關問題