我用C创建了一个战舰游戏,但是在这个函数中用于在记录文件上放置一条新记录,并保持其顺序,该函数在文件中写入了许多随机字符。 结构如下:
typedef char nom_tipus [MAX_NOM];
typedef struct
{
nom_tipus nom;
int puntuacio;
} record_tipus;
这里是一个关于resut的例子:
testdeunum 300asdf 151aaa 150bbb 149ccc 148ddd 147eee 146fff 145ggg 144hhh 143iii 142jjj 141kkk 140lll 139mmm 138nnn 137ooo 136ppp 135qqq 134rrr 133sss 132ttt 131uuu 130vvv 129"
这是最终文件:
Name 360mfuÿ<a |ø` H 300f Iufu{<a ` †lÔwúÒwÿÿÿÿ$ xø` G»ðÛ— akhud ylu ø` …thu¸?muàø` ¢ihu ssgujsgu– b bu ylu´ø` 0.buLkÔw\1«uL 151akhud ylu ø` …thu¸?muàø` ¢ihu ssgujsgu– b bu ylu´ø` 0.buLkÔw\1«uL 150b bu ylu´ø` 0.buLkÔw\1«uL 149cù` 0 m1«uiÐw (ú` L ” d (ú` bu0ú` “ e Œ®PØ€ù` B¤Òw0p@ €,@ àù` 4@ 0p@ 5n ú` ’ fHì¯ -@ @ ø1n p.n Ôù` ®jÔw @ ‘ gkÔw\1«uL <kÔwéïªu 148d (ú` bu0ú` “ e Œ®PØ€ù` B¤Òw0p@ €,@ àù` 4@ 0p@ 5n ú` ’ fHì¯ -@ @ ø1n p.n Ôù` ®jÔw @ ‘ gkÔw\1«uL <kÔwéïªu 147e Œ®PØ€ù` B¤Òw0p@ €,@ àù` 4@ 0p@ 5n ú` ’ fHì¯ -@ @ ø1n p.n Ôù` ®jÔw @ ‘ gkÔw\1«uL <kÔwéïªu 146fHì¯ -@ @ ø1n p.n Ôù` ®jÔw @ ‘ gkÔw\1«uL <kÔwéïªu 145gkÔw\1«uL <kÔwéïªu 144h¼B3 0ú` $¨iuÄú` 8ú` Ãeu\ú` Pmup Ðü` ¬iu imu¬ú` ’³iu€ú`
Pmu€ú` $¨iuý` ˆú` ÃeuŽ jmup 143imu¬ú` ’³iu€ú`
Pmu€ú` $¨iuý` ˆú` ÃeuŽ jmup 142jmup 141k ¨ ý` -ú` B Œ l (mu ÿQ@ 6ïiu ýÿÿÿxý` @ d Cý` …üiu‹ mmumu :þiuPmup ¨ ÿÿÿ”û` tý` xý` °ý` P@ Š n °ý` ”û` ¤û` ‰ o muÜû ÿ IÄfu+?amud mu ˆ p @þ` ðÑguŸBþÿÿÿôû` 1iumumud „ü` ü` 6ïiu‡ qü` ¤iu
d þ` …üiuø Xü` Õ§fuܧfu¯8a † r Tü` Þ¬fuø ˜ü` UØfu €Øfuo8a … sü` Õ§fuܧfu_8a (mu ¤ü` Þ¬fuø „ tØfu €Øfu8a (mu 8a$ ´ü` Xý` ƒ uBþÿÿÿ€Øfu…3iu à 140l (mu ÿQ@ 6ïiu ýÿÿÿxý` @ d Cý` …üiu‹ mmumu :þiuPmup ¨ ÿÿÿ”û` tý` xý` °ý` P@ Š n °ý` ”û` ¤û` ‰ o muÜû ÿ IÄfu+?amud mu ˆ p @þ` ðÑguŸBþÿÿÿôû` 1iumumud „ü` ü` 6ïiu‡ qü` ¤iu
d þ` …üiuø Xü` Õ§fuܧfu¯8a † r Tü` Þ¬fuø ˜ü` UØfu €Øfuo8a … sü` Õ§fuܧfu_8a (mu ¤ü` Þ¬fuø „ tØfu €Øfu8a (mu 8a$ ´ü` Xý` ƒ uBþÿÿÿ€Øfu…3iu à 139mmumu :þiuPmup ¨ ÿÿÿ”û` tý` xý` °ý` P@ Š n °ý` ”û` ¤û` ‰ o muÜû ÿ IÄfu+?amud mu ˆ p @þ` ðÑguŸBþÿÿÿôû` 1iumumud „ü` ü` 6ïiu‡ qü` ¤iu
d þ` …üiuø Xü` Õ§fuܧfu¯8a † r Tü` Þ¬fuø ˜ü` UØfu €Øfuo8a … sü` Õ§fuܧfu_8a (mu ¤ü` Þ¬fuø „ tØfu €Øfu8a (mu 8a$ ´ü` Xý` ƒ uBþÿÿÿ€Øfu…3iu à 138n °ý` ”û` ¤û` ‰ o muÜû ÿ IÄfu+?amud mu ˆ p @þ` ðÑguŸBþÿÿÿôû` 1iumumud „ü` ü` 6ïiu‡ qü` ¤iu
d þ` …üiuø Xü` Õ§fuܧfu¯8a † r Tü` Þ¬fuø ˜ü` UØfu €Øfuo8a … sü` Õ§fuܧfu_8a (mu ¤ü` Þ¬fuø „ tØfu €Øfu8a (mu 8a$ ´ü` Xý` ƒ uBþÿÿÿ€Øfu…3iu à 137o muÜû ÿ IÄfu+?amud mu ˆ p @þ` ðÑguŸBþÿÿÿôû` 1iumumud „ü` ü` 6ïiu‡ qü` ¤iu
d þ` …üiuø Xü` Õ§fuܧfu¯8a † r Tü` Þ¬fuø ˜ü` UØfu €Øfuo8a … sü` Õ§fuܧfu_8a (mu ¤ü` Þ¬fuø „ tØfu €Øfu8a (mu 8a$ ´ü` Xý` ƒ uBþÿÿÿ€Øfu…3iu à 136p @þ` ðÑguŸBþÿÿÿôû` 1iumumud „ü` ü` 6ïiu‡ qü` ¤iu
d þ` …üiuø Xü` Õ§fuܧfu¯8a † r Tü` Þ¬fuø ˜ü` UØfu €Øfuo8a … sü` Õ§fuܧfu_8a (mu ¤ü` Þ¬fuø „ tØfu €Øfu8a (mu 8a$ ´ü` Xý` ƒ uBþÿÿÿ€Øfu…3iu à 135qü` ¤iu
d þ` …üiuø Xü` Õ§fuܧfu¯8a † r Tü` Þ¬fuø ˜ü` UØf"
以下是代码:
#define MAX_RECORDS 25
#define MAX_NOM 50
bool emmagatzema_records(int punts, nom_tipus nom) {
int i, j;
char n;
record_tipus records[MAX_RECORDS];
FILE *f;
f = fopen("fitxer_records.txt", "r");
i = 0;
while (i < MAX_RECORDS) {
j = 0;
fscanf(f, "%c", &n);
while (n != ' ' && j < MAX_NOM) {
records[i].nom[j] = n;
fscanf(f, "%c", &n);
}
fscanf(f, "%d", &records[i].puntuacio);
if (punts > records[i].puntuacio) {
if (i == MAX_RECORDS - 1) {
records[i].puntuacio = punts;
for (j = 0; j < MAX_NOM; j++) {
records[i].nom[j] = nom[j];
}
} else {
records[i + 1].puntuacio = records[i].puntuacio;
records[i].puntuacio = punts;
for (j = 0; j < MAX_NOM; j++) {
records[i + 1].nom[j] = records[i].nom[j];
}
for (j = 0; j < MAX_NOM; j++) {
records[i].nom[j] = nom[j];
}
punts = 0;
i = i + 1;
}
}
i = i + 1;
}
fclose(f);
f = fopen("fitxer_records.txt", "w");
for (i = 0; i < MAX_RECORDS; i++) {
j = 0;
while (records[i].nom[j] != ' ' && j < MAX_NOM) {
fprintf(f, "%c", records[i].nom[j]);
j = j + 1;
}
fprintf(f, "%c", ' ');
fprintf(f, "%d", records[i].puntuacio);
}
return true;
}
答案 0 :(得分:2)
一次读取一个字符时,你永远不会增加j
:
while (n!=' ' && j<MAX_NOM)
{
records[i].nom[j++]=n; // note `j` changed to `j++`
fscanf(f, "%c", &n);
}
当然,这并不是读取字符串的常用方法。您应该使用类似fgets
的内容。你真的应该检查I / O错误。