Console - Read Text File

Let's say I have a text file called "Test.txt".  In this text file, there are numerous lines containing punctuation, numbers, etc.  How do I read that file and display it in the console?  
LVL 1
fattumsdadAsked:
Who is Participating?
 
jkrConnect With a Mentor Commented:
You could use

#include <fstream>
#include <iostream>
#include <string>
using namespace std;


//...

void CoutTextFile ( char* pszName) {

    string strLine
    ifstream is ( pszName);

    while ( !is.eof()) {

        getline ( is, strLine);
        cout << strLine.c_str();
    }

}

Or do you need any special formatting?
0
 
mrwad99Connect With a Mentor Commented:
C++ provides many classes and functions for reading from and writing to files.  I recommend you read this link:

http://www.cplusplus.com/doc/tutorial/tut6-1.html

It will explain what I would have otherwise typed :)

HTH
0
 
fattumsdadAuthor Commented:
jkr,

Do I have to link to a lib?  I get:

C:\MinGWStudio\MinGW\bin\..\lib\gcc-lib\mingw32\3.3.1\..\..\..\libmingw32.a(main.o)(.text+0x97):main.c: undefined reference to `WinMain@16'

As for the special formatting, is there a way to read it in exactly as it appears in the text file?
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
jkrCommented:
>>Do I have to link to a lib?

No, just set up your project to link a console app, not a GUI app :o)

>>As for the special formatting, is there a way to read it in exactly as it appears in the text file?

That's wht the above code fragment does. The only difference will be the line wrap when a line exceeeds the console width.
0
 
fattumsdadAuthor Commented:
This is really strange....  I have it set up as Win32 Console Application...  the same setting that I've been using the other million times you've helped me LOL.  Still getting the same error... :\
0
 
jkrCommented:
Check again - it might just be something simple :o)
0
 
fattumsdadAuthor Commented:
I have it in exactly as you showed...  should I add:

int main()
{
  CoutTextFile("c:/Test.txt");
  return 0;
}
0
 
mrwad99Commented:
OMG

Yes.
0
 
mrwad99Commented:
Sorry - I have been following this question and I am disgusted in myself that I did not spot that that was the problem.  I did not mean any offence to you :)

Back to the books for me :)
0
 
mrwad99Commented:
>> CoutTextFile("c:/Test.txt");

Probably better to be CoutTextFile("c:\\Test.txt");
0
 
fattumsdadAuthor Commented:
Got it.  It seems to work the same both ways, "c:/" or "c:\\"...  is there a significant difference?
0
 
jkrCommented:
>>It seems to work the same both ways, "c:/" or "c:\\"...  is there a significant difference?

No, not at all. Both are valid on Win32 platforms.
0
 
mrwad99Commented:
Not really.  Sometimes a backslash is preferred, but if you do you must use two (\\) as \ is an escape character.

Ta for the assist.
0
 
fattumsdadAuthor Commented:
Ok...  one more question...  actually, I'll repost to give more points :)  Thank for the help guys!
0
 
mrwad99Commented:
Glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.