How to print out Text File

Dec 27, 2017 at 4:45pm
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.
Dec 27, 2017 at 5:27pm
Dec 27, 2017 at 5:28pm
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() ;
}
Dec 27, 2017 at 5:31pm
For large files, could rdbuf fail where another method such as while (getline(f, line)) { cout << line << "\n";} would otherwise still work?
Dec 27, 2017 at 5:37pm
Open a command window and enter
type ascii_art.txt
Dec 27, 2017 at 5:50pm
None of these seem to work for me...
Dec 27, 2017 at 5:54pm
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 Dec 27, 2017 at 5:55pm
Dec 27, 2017 at 6:14pm
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.
Dec 27, 2017 at 6:34pm
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?
Dec 27, 2017 at 6:37pm
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 Dec 27, 2017 at 6:39pm
Dec 27, 2017 at 6:45pm
How would I do that?
Dec 27, 2017 at 6:52pm
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 Dec 27, 2017 at 6:54pm
Dec 27, 2017 at 6:59pm
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++
Dec 27, 2017 at 7:04pm
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/
Dec 27, 2017 at 7:27pm
Thanks,
I got it to work :)
Dec 28, 2017 at 2:01am
> 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.