Solved

Console - Read Text File

Posted on 2004-09-21
15
233 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

747 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

15 Experts available now in Live!

Get 1:1 Help Now