Thread: Reading from a text file....

  1. #1
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    Reading from a text file....

    I have a common text format file ( .txt simple...not word or anything ), at a certain spot in my program I want to take what's in the text file and print it ( display ) to the screen, pausing after every screenful of information.

    Display the entire text file so the user can read it, but the file is to large to display on the screen all at once.
    The world is waiting. I must leave you now.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since this sounds like homework...

    1) for(counter=0;counter<23;counter++)
    2) Read a file a line at a time.
    fgets( buf, 80, myfile );

    3) Display a line.
    puts( buf );

    4) If end of file, quit.
    5) If counter is 22, counter = -1, give user a prompt

    6) Stir well, pour over steamed rice, enjoy.

    Quzah.

  3. #3
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    nope

    Not homework at all. Im not even in a programming course....

    Im doing a "lesuire.......for fun" program.
    The world is waiting. I must leave you now.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    There are basically two aproaches you might take: Read in all lines into an array, a structure, etc., ( but count the lines first-woops there goes my non-standard variable-initialized arrays! If you dissapprove of this, go to suggestion two! ) then obtain the modulus of the number of lines divided by the lines to be displayed and so utilize three loops such that:


    Code:
    int modulus = num_records % num_per_page; 
    
    int whole_page = num_records / num_per_page;
    
    int change = whole_page;
    
    for(i=0; i<num_records - modulus ; i++)
    { 
    for(j= change; j < change + whole_page ; j++)
    {
    //print "whole_page" of records
    }
    
    getch();
    
    change += whole;
    }
    
    
    
    
    for(i=0;i< modulus; i++)
    {
    //print last remaining records
    }
    If you are printing as you go, do as quzah said except more specifically use two loops, one to print till end of file and the inner loop to stop after each whole page is printed.

    My first suggestion does however on a non-ANSI method of declaring
    an array such that array[num_lines_in_file] is declared instead of array[1000](assuming that it WILL NOT exceed this size!!), or the more accepted practice of malloc'ing an array of structs/lines/etc multiplied times num_lines_in_file. Since my compiler DOESN"T complain I use the described method, but this may not be portable to your compiler(Get Blood-Shed Dev C/C++ Compiler!!).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    alrighty

    I've been keeping an eye on all the replies I have been getting to this new thread of mine. I'm going to be out today, but I still have been watching over them. Kind of letting a few accumlate you could say. Whoever wants to drop a line, then they'll do it. Once I come back home today I'll probably read them all and take it from their ( with the source open and ready to go ).

    As far as the text file I'm reading from. Let me do a line count on it. The text file i'm reading from has 42 lines in it.
    The world is waiting. I must leave you now.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I all but gave you every line of code you need. *mutter*

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM