?
Solved

Console - Read Text File

Posted on 2004-09-21
15
Medium Priority
?
267 Views
Last Modified: 2010-04-01
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?  
0
Comment
Question by:fattumsdad
  • 6
  • 5
  • 4
15 Comments
 
LVL 19

Assisted Solution

by:mrwad99
mrwad99 earned 800 total points
ID: 12115635
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
 
LVL 86

Accepted Solution

by:
jkr earned 1200 total points
ID: 12115639
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
 
LVL 1

Author Comment

by:fattumsdad
ID: 12115715
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 12115753
>>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
 
LVL 1

Author Comment

by:fattumsdad
ID: 12115805
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
 
LVL 86

Expert Comment

by:jkr
ID: 12115834
Check again - it might just be something simple :o)
0
 
LVL 1

Author Comment

by:fattumsdad
ID: 12115902
I have it in exactly as you showed...  should I add:

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

Expert Comment

by:mrwad99
ID: 12115911
OMG

Yes.
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12115930
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
 
LVL 19

Expert Comment

by:mrwad99
ID: 12115948
>> CoutTextFile("c:/Test.txt");

Probably better to be CoutTextFile("c:\\Test.txt");
0
 
LVL 1

Author Comment

by:fattumsdad
ID: 12116014
Got it.  It seems to work the same both ways, "c:/" or "c:\\"...  is there a significant difference?
0
 
LVL 86

Expert Comment

by:jkr
ID: 12116026
>>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
 
LVL 19

Expert Comment

by:mrwad99
ID: 12116036
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
 
LVL 1

Author Comment

by:fattumsdad
ID: 12116064
Ok...  one more question...  actually, I'll repost to give more points :)  Thank for the help guys!
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 12120253
Glad to help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question