EASY 500 POINTS!!! How do I read a text file?

I am using MS VIsual Studio 2005 and I created a dialog-based MFC APP (used MFC standard project style & MFC in a shared DLL).

How do I make it so that the app will read the content of the text file "C:\example.txt"?

Please provide code that I can cut & paste, and watch it work.  Thank you.
LVL 1
shaolinfunkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
Hi shaolinfunk,

you can do somehow like this:

>CString LoadTextFile( CString strFileName )
>{
> CStdioFile file;
> CString strText, strLine;
> if ( FALSE != file.Open( strFileName, CFile::modeRead | CFile::typeText ) )
> {
>  while ( FALSE != file.ReadLine( strLine ) )
>  {
>   strText += strLine + "\n";
>  }
> }
> return strText;
>}
>
> ...
> LoadTextFile( "C:\\example.txt" );


Hope that helps,

ZOPPO
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZoppoCommented:
Sorry, last line should be something like

> CString strTextFile = LoadTextFile( "C:\\example.txt" ); // now strTextFile contains the content of the text file or is empty if the file couldn't be read
0
ZoppoCommented:
Sorry, another error: Replace 'ReadLine' with 'ReadString' - such errors happen when writing code just from the mind :(
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

marcodalzottoCommented:
Try this code:

CFile mytxt;
CString filecontent;

if(!mytxt.Open("C:\\example.txt", CFile::modeRead) )
{
  // error opening file
}
else {
  char *p = (char *)malloc(mytxt.GetLength());
  mytxt.Read(p, mytxt.GetLength());
  filecontent = p;
  free(p);
  mytxt.Close();
}

// in filecontent you will find the text from example.txt
0
shaolinfunkAuthor Commented:
hi marcodalzotto,

should i be including anything? like include cstring?  i cut & pasted your code and got the error C2664:  'CFile::Open' : cannot convert parameter 1 from 'const char [16]' to 'LPCTSTR'  Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
0
marcodalzottoCommented:
Ok, try:

if(!mytxt.Open(_T("C:\\example.txt"), CFile::modeRead) )
0
marcodalzottoCommented:
Or you can write:

CString filename = "C:\\example.txt";
if(!mytxt.Open(filename , CFile::modeRead) )
0
ZoppoCommented:
@shaolinfunk: IMO you're using UNICODE.

In this case you have to write each string literal encloses with _T(...), so it even would have to be:

> CString filename = _T( "C:\\example.txt" );

Further, if you want to use 'marcodalzotto's function (mine would work without modification except the _T(...) in the function call) you'll have to convert the loaded characters pointed to by 'p' to unicode when you assign them to the CString - an easy way would be i.e.:

// outside any function
#include <atlconv.h>

// within the function
USES_CONVERSION;

...
char *p = (char *)malloc(mytxt.GetLength());
mytxt.Read(p, mytxt.GetLength());

filecontent = A2W( p );
...


ZOPPO
0
shaolinfunkAuthor Commented:
zoppo, i think you're right that i'm using unicode..i think i saw that in the settings when i created the MFC app.  should i not use unicode?  would it make things easier?

btw, i used marco's code because yours had the same errors as his and i figured if marco answered most recently his answer would solve the same problem that i have with both of your codes.  and it does.

however, your code is a little more complicated in that your 8:35am reply makes mention of things i don't understand eg USES_CONVERSION.  i find marco's code easier to use.  however,  both of you deserve the points.
0
shaolinfunkAuthor Commented:
I just checked...in the MFC App Wizard, "Use Unicode libraries" was indeed checked off.
0
ZoppoCommented:
Well, you have to decide if you need unicode or not - if you don't need it it would be easier not to use it.

As told the problem is any hardcoded string literal needs _T(...) when unicode is used, so both codes produced the same error.

The thing with 'USES_CONVERSION' is needed only if you use unicode since it's not possible to assign a 'char*' to a CString since in unicode CString is based on 'wchar_t' instead of 'char' - anyway the text file contains ASCII, so it has to be loaded as 'char' (in my sample code this is implicit handled by CStdioFile::ReadString).
0
ZoppoCommented:
Check it in the project's settings 'General->Character Set'
0
shaolinfunkAuthor Commented:
Ok, thanks.  Want more easy points?  I want to learn how to assign marco's code to a dialog button in an MFC app.  500 points --> http://www.experts-exchange.com/Programming/Editors_IDEs/C_CPP_CS/Visual_CPP/Q_25477846.html

Step by step instructions would be greatly appreciated, thanks! (I am trying to learning this via a Sams Teach Yourself book and EE).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.