How to print out Text File

I have a text file that has some ascii art in it, and I was wondering if there is any way to just print the contents into the console, it is in a txt file.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>

int main() {

    const char file_name[] = "ascii_art.txt" ;

    // print contents of the text file to stdout
    std::cout << std::ifstream(file_name).rdbuf() ;
}
For large files, could rdbuf fail where another method such as while (getline(f, line)) { cout << line << "\n";} would otherwise still work?
Open a command window and enter
type ascii_art.txt
None of these seem to work for me...
Open a command window, change directory to where you believe your file is located (use 'help cd' if you can't manage this), enter
dir
to check that it is there, and then enter
type ascii_art.txt
(obviously changing the designated file to whatever yours is called.)
Last edited on
I'm on a Mac (don't know if this changes anything)
But when I type
type boar.txt
I get -bash: type: boar.txt: not found
And I am in the file where it is.
I got some code here
1
2
3
4
5
6
7
8
9
10
11
12
13
void draw_gui(string file_name) {
    string line;
    ifstream myfile (file_name);
    if (myfile.is_open()) {
        while ( getline (myfile,line) )
        {
            cout << line << '\n';
        }
        myfile.close();
    } else {
        cout << "Unable to open file";
    }
}


I call it with
draw_gui("/Users/ME/Desktop/Ascii Art/boar.txt")
Is there anyway to shorten the url?
Where are you launching the program from? (looks like it's being launched from your root directory)

If you want to just be able to call draw_gui("boar.txt"), have the program be launched in the same directory as boar.txt (inside Ascii Art).
Last edited on
How would I do that?
Where is your executable file located? Move it to Ascii Art directory.

(PS: lastchance's tip of "type" command is equivalent to "cat" on unix/linux systems, e.g. "cat boar.txt").
Last edited on
I'm using Xcode, and the Ascii art file is in the workspace. I don't understand what you mean, sorry I'm new to C++
Oh, I see.
What you need to do is change the working directory for your project.

I don't personally use Xcode but perhaps try one of these?
http://meandmark.com/blog/2013/12/setting-the-current-working-directory-for-xcode-command-line-projects/
https://stackoverflow.com/questions/3396378/change-the-working-directory-in-xcode

and change your working directory to /Users/ME/Desktop/Ascii Art/
Thanks,
I got it to work :)
> For large files, could rdbuf fail where another method such as while (getline(f, line)) { cout << line << "\n";} would otherwise still work?

No. The steam buffer (in this case std::filebuf) handles buffering automatically.

When an attempt is made to read one or more characters from the stream buffer, and the input buffer is exhausted ( gptr() == egptr() ), the stream buffer would first call uflow().
Topic archived. No new replies allowed.