我正在爲程序編寫代碼。 這將有三個文本文件: -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;
}
這裏任何幫助,將不勝感激。
您能正確設置代碼格式並添加語言標記以吸引更多可以幫助您的人嗎? – Adalee
這是功課嗎? –
您是否擔心有些單詞可能會變得太長而無法存儲到數組中? –