Newbie on VC++5 : Problem reading files and putting them in CString 
Author Message
 Newbie on VC++5 : Problem reading files and putting them in CString

Hi all,

I'm new on VC++ and I come from VB.

I want to put a ASCII file into a CString, and I don't know how to do
dynamicly.

To day, I use that :

long FileInCString (CString myFileName, CString & Texte)
{
        if (FileExists (myFileName)==0) //a function which looks for the file
                {return -1;
                exit (0);
                }
        else
                {
                CFile myFile;
                CFileException efile;
                char
input[1000000];//////////////////////////////////////////////////////////
There's the problem : it's not dynamic
                long FileLen;

                if (!myFile.Open (myFileName, CFile::modeRead), &efile)
                        {       FileLen=myFile.Read (input, 1000000);///////  I'd prefer put the file
size instead of 1000000
                        Texte= input;
                        return 0;}
                else
                        cout << "***** Erreur dans l'ouverture du fichier.\n\n";
                        return -2;
        }

Quote:
}

I want to have a dynamic char input, that I can size with the GetLength()
member. I'm interested with other ways, of course.

So I pass in a parameter the CString, the FileName and when I quit this
function, I would like to have in the CString all the contents of the file.

I couldn't see how to do it, but I'm very new in C.

Thanks in advance.

Please answer at eMail.

Salutations

Christian Hugoud



Tue, 07 Mar 2000 03:00:00 GMT  
 Newbie on VC++5 : Problem reading files and putting them in CString

Hi Christian,

Try this:

#include <iostream.h>

long FileInCString( CString myFileName, CString& Texte )
{
        if ( FileExists( myFileName ) ) //a function which looks for the file
        {
                CFile myFile;
                CFileException eFile;
                if ( !myFile.Open( myFileName, CFile::modeRead, &eFile ) )
                {
                        eFile.ReportError();
                        cout << "Erreur: " << eFile.m_lOsError << "\n";
                        cout << "***** Erreur dans l'ouverture du fichier.\n\n";
                        return( -2 );
                }

                DWORD fLen= myFile.GetLength();
                LPTSTR spBuf= Texte.GetBuffer( fLen );
                if ( spBuf != NULL )
                {
                        if ( myFile.Read( spBuf, fLen ) != fLen )
                        {
                                cout << "Erreur\n";
                                Texte.ReleaseBuffer();
                                return( -3 );
                        }
                        Texte.ReleaseBuffer();
                }
                else
                {
                        cout << "Erreur\n";
                        return( -4 );
                }
                myFile.Close();
                return( 0 );
        }
        return( -1 );

Quote:
}

HTH
--
===================
Frank Hickman
SecurTech, Inc.


===================



Wed, 08 Mar 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Read file to array problem (Newbie for those wishing to avoid)

2. Newbie problem reading text files

3. Reading Hex from File and Putting it in a Packet

4. Reading a GIF file and putting it into a dialog

5. CString Read File help!!!!

6. Reading from file to CString

7. reading from a file to CString

8. Read a text file into a CString.

9. reading CString info from a text file

10. Reading fstream created text file into a CString

11. File reading problem - reading a text colormap.

12. Newbie: CString, string, cstring

 

 
Powered by phpBB® Forum Software