刽子手游戏中的eksternal文件,用C语言编写 [英] eksternal file in hangman game, in C language
本文介绍了刽子手游戏中的eksternal文件,用C语言编写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
中建立解决方案
作为一种简单的方法,您可以编写一个文本文件,每行只有一个单词,然后将单词读入数组,最终将数组自身洗牌,例如
#include < stdio.h >
< span class =code-keyword> #include < stdlib.h >
#include < string.h >
#define MAX_WORDS 1000
#define MAX_LEN 256
void shuffle( char * pword [], int 字, int 迭代)
{
< span class =code-keyword> int n;
if (单词< = 1 )返回跨度>;
for (n = 0 ; n< iterations; n ++)
{
int i1,i2;
i1 = rand()%words;
i2 = rand()%words;
char * tmp = pword [i1];
pword [i1] = pword [i2];
pword [i2] = tmp;
}
}
int main()
{
int n;
char buf [MAX_LEN];
char * pword [MAX_WORDS];
int words = 0 ;
FILE * fp = fopen( words.txt, r);
if (!fp) return - 1 跨度>;
while (!feof(fp))
{
if (words == MAX_WORDS) break ;
if (!fgets(buf,MAX_LEN,fp)) break ;
buf [strlen(buf) - 1 ] = ' \0' 跨度>; // 删除换行符
pword [words] = strdup(buf); // 这会动态分配内存
words ++;
}
fclose(fp);
shuffle(pword,words, 2 * words);
for (n = 0 ; n< words; n ++)
printf( %s \ n,pword [n]);
// cleanup
for (n = 0 ; n< words; n ++)
free(pword [n]);
return 0 ;
}
上述程序读取文件:
aaaaaaaaaaa
bbbbbbbb
cccccccccc
dd
eeeeeeeeeee
fffff
gggggggggggg
hhhhhhhhh
iiiiiiiiiii
jjjjjjjjjj
k
llllllllll
mmmmmmmm
nnnnnnn
ooooooo
p
qqqqqq
rrrrrrrr
sssss
tt
uuuuuu
vvvvvvv
wwwwwww
xxxxxxxxx
yyyyyyyyyyy
z
产生以下输出:
jjjjjjjjjj
aaaaaaaaaaa br / b >
rrrrrrrr
fffff
cccccccccc
sssss
qqqqqq
llllllllll
bbbbbbbb
nnnnnnn
uuuuuu
xxxxxxxxx
iiiiiiiiiii
hhhhhhhhh
eeeeeeeeeee
z
dd
k
wwwwwww
tt
mmmmmmmm
vvvvvvv
yyyyyyyyyyy
p
gggggggggggg
ooooooo
sorry for the question is too long, I''ve tried to make the other parts of hangman game , but I do not understand how to make a list of words through external files such as txt, list of words written in the file and will be loaded in compilier, but should be incorporated into the array first, that fast loaded, word must at random, please help to do it in the language of c, is established in a void
解决方案As a simple approach, you could write a text file with just one word per line, then read the words into an array, eventually shuffling the array itself, e.g.
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_WORDS 1000 #define MAX_LEN 256 void shuffle(char * pword[], int words, int iterations) { int n; if ( words <= 1) return; for (n=0; n<iterations; n++) { int i1, i2; i1 = rand()%words; i2 = rand()%words; char * tmp = pword[i1]; pword[i1] = pword[i2]; pword[i2] = tmp; } } int main() { int n; char buf[MAX_LEN]; char * pword[MAX_WORDS]; int words = 0; FILE * fp = fopen("words.txt", "r"); if ( ! fp ) return -1; while (!feof(fp)) { if (words == MAX_WORDS) break; if (! fgets(buf, MAX_LEN, fp) ) break; buf[strlen(buf)-1] = '\0'; // remove newline pword[words] = strdup(buf); // this dynamically allocates memory words++; } fclose(fp); shuffle(pword, words, 2*words); for (n=0; n<words; n++) printf("%s\n", pword[n]); // cleanup for (n=0; n<words; n++) free( pword[n]); return 0; }
The above program reads the file:
aaaaaaaaaaa
bbbbbbbb
cccccccccc
dd
eeeeeeeeeee
fffff
gggggggggggg
hhhhhhhhh
iiiiiiiiiii
jjjjjjjjjj
k
llllllllll
mmmmmmmm
nnnnnnn
ooooooo
p
qqqqqq
rrrrrrrr
sssss
tt
uuuuuu
vvvvvvv
wwwwwww
xxxxxxxxx
yyyyyyyyyyy
z
Producing the following output:
jjjjjjjjjj
aaaaaaaaaaa
rrrrrrrr
fffff
cccccccccc
sssss
qqqqqq
llllllllll
bbbbbbbb
nnnnnnn
uuuuuu
xxxxxxxxx
iiiiiiiiiii
hhhhhhhhh
eeeeeeeeeee
z
dd
k
wwwwwww
tt
mmmmmmmm
vvvvvvv
yyyyyyyyyyy
p
gggggggggggg
ooooooo
这篇关于刽子手游戏中的eksternal文件,用C语言编写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文