从平面文件中读取而不输出整个内容

时间:2014-10-21 08:37:39

标签: c database file structure database-scan

好的,这是一个函数,它将读取文件并将值粘贴到屏幕上,但是,我希望能够打印特定的行。现在,我应该创建一个打印整个文件的循环,但每次数据帐户不等于用户输入时都会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);
}

1 个答案:

答案 0 :(得分: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