Looping problem.

This is a discussion on Looping problem. within the C Programming forums, part of the General Programming Boards category; Hi guys, I have a problem. My program is reading a binary file and displaying pages and pages of output ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    20

    Looping problem.

    Hi guys,

    I have a problem. My program is reading a binary file and displaying pages and pages of output and I want to sort this out by displaying 20 or so lines a page


    Code:
    void dataPrint(void)
    {
    	FILE *inFile;
    	data_struct data;
    	inFile = openFile(inFileName, "rb+");
    	data= readData(inFile);
    	while(!feof(inFile))
    	{
    		displaydata(data);
    		data = readData(inFile);
    	}
    	closeFile(inFile);
    }
    Ok so it calls the readData function which reads the file and returns the data then another function readData is called to display the output on the screen.

    Im guessing the loop has to be within the while loop but I tried a If and while loops just before displaydata(data) but what I really cant figue out is how to display the first 20, then prompt for a keystroke to display ANOTHER 20 and so on until end of file.

    Any help would be appreciated,

    thanks,

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Maybe something like this:
    Code:
    #define LINES_TO_DISPLAY 20
    
    void dataPrint(void)
    {
       unsigned int lines = 0;
       FILE *inFile;
       data_struct data;
       inFile = openFile(inFileName, "rb+");
       data= readData(inFile);
       while ( !feof(inFile) )
       {
          displaydata(data);
          if ( lines++ % LINES_TO_DISPLAY == (LINES_TO_DISPLAY - 1) )
          {
             int ch;
             fputs("Press enter to continue...", stdout);
             fflush(stdout);
             while ( (ch = getchar()) != '\n' && ch != EOF )
                ;
          }
          data = readData(inFile);
       }
       closeFile(inFile);
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    20
    thanks dave that worked great, I dont really understand what you have done however.

    Code:
    unsigned int lines = 0;
    why an unsigned int? whats the difference between that and a normal int?


    Code:
    if ( lines++ % LINES_TO_DISPLAY == (LINES_TO_DISPLAY - 1) )
    So you increment lines by 1, then i dont really understand the rest

    Code:
    fputs("Press enter to continue...", stdout);
    fputs, just like printf but for stings right?. stdout = standard out? not sure what that means

    Code:
    fflush(stdout);
    fflush? clearing the value of stdout??


    Code:
    while ( (ch = getchar()) != '\n' && ch != EOF )
    lost me a bit here to...keep looping with user input or until EOF?

    thanks alot for the help

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by prepare
    Code:
    if ( lines++ % LINES_TO_DISPLAY == (LINES_TO_DISPLAY - 1) )
    So you increment lines by 1, then i dont really understand the rest
    This will be true every 20 times.
    • 0% 20 == 0
    • 1 %20 == 1
    • ...
    • 18 % 20 == 18
    • 19 % 20 == 19
    • 20 % 20 == 0
    • 21 % 20 == 1
    • ...
    [EDIT=2]
    Code:
    #include <stdio.h>
    
    #define MAGIC_NUMBER 5
    
    int main(void)
    {
       unsigned int i;
       for ( i = 0; i < 3 * MAGIC_NUMBER + 1; ++i )
       {
          printf("%2u %% %u == %u", i, MAGIC_NUMBER, i % MAGIC_NUMBER);
          if ( i % MAGIC_NUMBER == (MAGIC_NUMBER - 1) )
          {
             printf("<--");
          }
          putchar('\n');
       }
       return 0;
    }
    
    /* my output
     0 % 5 == 0
     1 % 5 == 1
     2 % 5 == 2
     3 % 5 == 3
     4 % 5 == 4<--
     5 % 5 == 0
     6 % 5 == 1
     7 % 5 == 2
     8 % 5 == 3
     9 % 5 == 4<--
    10 % 5 == 0
    11 % 5 == 1
    12 % 5 == 2
    13 % 5 == 3
    14 % 5 == 4<--
    15 % 5 == 0
    */
    [/EDIT]


    Quote Originally Posted by prepare
    Code:
    fputs("Press enter to continue...", stdout);
    fputs, just like printf but for stings right?. stdout = standard out? not sure what that means
    You pretty much nailed it.


    Quote Originally Posted by prepare
    Code:
    fflush(stdout);
    fflush? clearing the value of stdout??
    fflush(stdout)


    Quote Originally Posted by prepare
    Code:
    while ( (ch = getchar()) != '\n' && ch != EOF )
    lost me a bit here to...keep looping with user input or until EOF?
    How do I get my program to wait for a keypress?


    [EDIT=1]
    Quote Originally Posted by prepare
    why an unsigned int?
    If I don't expect negative values, I try to use a more appropriate type.
    Last edited by Dave_Sinkula; 10-06-2004 at 10:24 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Looping problem
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 02:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21