好吧,我正在編寫一些代碼,並且在測試開關函數時遇到了問題。它會從所選的所有案例中增加(我的解釋)。有人能幫我解釋這是爲什麼嗎?C中的開關函數做所有的情況
#include <stdio.h>
#include <stdlib.h>
#include "ratedzfunctions.h"
int main()
{
int selection, loop=1;
FILE* fajl;
//Opening the participants file
fajl=fopen("participants.txt","r+");
if (fajl==NULL)
{
printf("The file cannot be opened.\n");
}
//MENU
do
{
printf("\nMENU: \n------------\n1. RATEDZ\n\n2. STATISTICS\n\n3. EXIT\n\n==>");
scanf("%d", &selection);
switch (selection)
{
case 1:
ratedz(fajl);
case 2:
stats(fajl);
case 3:
loop=0;
}
}
while (loop==1);
fclose(fajl);
return 0;
}
//THIS IS FROM RATEDZFUNCTIONS.H
void ratedz(FILE *fajl)
{
printf("\nTEST RATEDZ");
}
void stats(FILE *fajl)
{
//Printing all participants
char *buffer=(char*) malloc(50);
while(fscanf(fajl,"%s %s %s", buffer)!=EOF)
{
printf("\n%s %s %s", buffer);
}
free(buffer);
}
非常感謝您! – Narraxus
你能告訴我爲什麼它只打印一次案例2中的.txt文件?當我再次選擇它時,它不會再執行一次嗎? – Narraxus
哦,這是因爲我沒有倒帶文件:P傻我。 – Narraxus