-2

好的,这是一个可以读取文件并将值粘贴到屏幕上的函数,但是,我希望能够打印特定的行。现在,我应该创建一个循环来打印整个文件,但system("cls");每次数据计数不等于用户输入,还是有更好的方法来做到这一点?

void NameSelect(void)
{
     //Screen refresh
    DataCount=1;

    fFn=fopen("PDfirstN.txt","r"); //Opens student usernames
    fscanf(fFn,"%16[^\n]%*c", stockF[DataCount].def); //Scans first line
    fLn=fopen("PDlastN.txt","r"); //Opens student passwords
    fscanf(fLn,"%16[^\n]%*c", stockL[DataCount].def2); //scans first line

    do
    {  
        printf("%d.",  DataCount);                                             
        printf(" %s",  stockF[DataCount].def); 
        printf(" %s",  stockL[DataCount].def2);

        DataCount=DataCount+1; //Next line counter
        fscanf(fFn,    "%16[^\n]%*c", stockF[DataCount].def); //Scans next line
        fscanf(fLn,    "%16[^\n]%*c", stockL[DataCount].def2);
        printf("\n");
    }
    while(!feof(fFn)); //While there accounts in user name file

    fclose(fFn);
    fclose(fLn);
}
4

1 回答 1

1

如果您不想显示任何内容,请不要调用 printf。

do
{  
    if (DataCount == DATA_COUNT_I_WANT) {
        printf("%d.",  DataCount);                                             
        printf(" %s",  stockF[DataCount].def); 
        printf(" %s",  stockL[DataCount].def2);
        printf("\n");
    }

    DataCount=DataCount+1; //Next line counter
    fscanf(fFn,    "%16[^\n]%*c", stockF[DataCount].def); //Scans next line
    fscanf(fLn,    "%16[^\n]%*c", stockL[DataCount].def2);        
}
while(!feof(fFn)); //While there accounts in user name file
于 2014-10-21T08:47:08.200 回答