0

How can I display only a specific amount of columns on the screen. So when Im pressing a key I could browse between the pages.

#include<cstdio>
int main()

{
   printf("This database has got random info about different cars.\n");
   printf("==============================================\n");
   FILE *database ;
   char i ;
   database = fopen("andmebaas2.txt", "r");
   while (1)
   {
     i = fgetc(database); 
     for (char i = 0; i++;);
     {
        printf ("%c", i);
     }    
     if(feof(database))
     break ;
   }
   fclose(database);
}

For example the first page shows this.

Tesla Model S; Riik: USA; Tootmine: 2012; Kiirus: 249 km/h; Hind: $79 990
Corvette C8; Riik: USA; Tootmine: 2020; Kiirus: 312 km/h; Hind: $59 995
Dodge Demon; Riik: USA; Tootmine: 2018; Kiirus: 339 km/h; Hind: $84 995
Mercedes-AMG C63; Riik: Saksamaa; Tootmine: 2019; Kiirus: 289 km/h; Hind: $70 650
Aston Martin DB11; Riik: Suurbritannia; Tootmine: 2016; Kiirus: 321 km/h; Hind: $198 985

And when I press "n" it shows the next page.

Mercedes-Benz S 560; Riik: Saksamaa; Tootmine: 2020; Kiirus: 250 km/h; Hind: $104 450
Nissan GT-R; Riik: Jaapan; Tootmine: 2017; Kiirus: 307 km/h; Hind: $99 990
Lincoln Navigator; Riik: USA; Tootmine: 2018; Kiirus: 252 km/h; Hind: $76 185
Porsche 911 Turbo S; Riik: Saksamaa; Tootmine: 2021; Kiirus: 330 km/h; Hind: $203 500 
Lamborghini Aventador S; Riik: Itaalia; Tootmine: 2017; Kiirus: 349 km/h; Hind: $393 695

Thanks

Rasmus
  • 1
  • 1
  • First decide what *is* a "page"? Then figure out a way to stop after reading enough for one "page", and how to ask the user to go to next "page". Using separate functions could be useful. – Some programmer dude Mar 07 '21 at 15:32
  • 1
    Also, consider reading the file [line-by-line](https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c) instead of 1 character at a time, and use C++ file I/O facilities instead of C. Incidentally that makes the pagination task easier - just read and display N lines, the wait for user input, and repeat until no more lines are left. – rustyx Mar 07 '21 at 16:50

0 Answers0