cdatabasefilestructuredatabase-scan

reading from a flatfile without outputting the entire thing


Ok, here is a function that will read a file and paste the values on screen, however, i want to be able to printf a specific row. Now, should i create a loop that printf the entire file but does a system("cls"); everytime datacount is not equal to the user input, or is there a better way of doing this?

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);
}

Solution

  • Just don't call printf, if you don't want to display anything.

    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