Solved

Console - Read Text File

Posted on 2004-09-21
15
234 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 200 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 300 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now