Thread: how to display a text file

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    how to display a text file

    i'm not going to post the entire code but i'll post the relevant part. the situation is i have a .txt file (input.txt), and the program reads that and counts the characters in it and displays the number of characters, spaces, digits, etc. but i also want it to display the actual text from the file. is there a way to have it display the file, without actually copying and pasting all of the text into the compiler?
    so instead of cout<<"1000000 words"
    cout<<input.txt
    something like that.
    so this is the first part of the code that opens the file. how do i make the program display the text when i run it?

    Code:
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    
    using namespace std;
    
    
    bool myIsAlpha( char ch );
    bool myIsDigit( char ch );
    bool myIsUpper( char ch );
    int countPunct = 0, //declaring int variables to be used.
    countAlpha = 0,
    countUp = 0,
    countWhite=0,
    countLow = 0,
    countDig = 0,
    countSpace = 0,
    totalChar;
    
    char ch; //declaring ch as char
    
    
    int main()
    {
    ifstream inputFile; 
    
    //opening the input file and if it fails to open, displaying and error message to the user and exiting the program
    
    
    
    inputFile.open( "input.txt" );
    if( inputFile.fail() )
    {
    cout << "input.txt failed to open";
    exit( -1 );
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    I assume the part of the code that you didn't show was the actual reading of characters, checking the type, etc. After you read a character, just append it to some string, which is used to show the entire contents of the file. For example,
    Code:
    string buffer = "";
    // ...
    ch = //read character from file
    buffer += ch; // append every character to the buffer
    // ...
    // finally print "buffer"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read and display contents from text file
    By spadez in forum C Programming
    Replies: 2
    Last Post: 02-03-2009, 03:25 PM
  2. Display selected words from text file
    By picasso in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2007, 09:06 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM