Solved

Console - Read Text File

Posted on 2004-09-21
15
241 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

839 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