?
Solved

Reading a text file

Posted on 2003-11-10
4
Medium Priority
?
3,711 Views
Last Modified: 2008-02-01
I am vc++ beginner .  please help me reading text file line by line from an MFC application
0
Comment
Question by:Gopakumar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 11

Expert Comment

by:KurtVon
ID: 9715270
The simple method is ot open the file, read the lines (processing them however you want) and close the file.  Of course, there is a bunch of error-checking to do in C.  It would also help if you were a little more clear on exactly what you are doing. Is this just opening and reading a text file, or do you mean that you want the document object to be a text file read line-by-line?

Here's the code for doing it the simple way:

// Open the file for reading
FILE* file = fopen(strFilename, "r");
// make sure the open was successful
if (file != NULL)
{
    // Space for each line
    char nextline[MAX_LINE_LENGTH];

    // Read each line
    while (fgets(nextline, MAX_LINE_LENGTH, file) != NULL)
    {
        // Process the string in nextline, watching out for a null character.
        // If there is a null character in the file, it will appear in the middle
        // of the string, so be sure not to make assumptions about the line
        // length.
    }

    // Close the file
    fclose(file);
}
else
{
    // Do whatever you need to to let the user know the file didn't open
}

Hope that helps.
0
 
LVL 14

Accepted Solution

by:
Thandava Vallepalli earned 150 total points
ID: 9715314
Hi,

here an example source of reading information from a file (m_cSourceFile) and writing data into created new file (m_cDestFile). CStdioFile object has been used here.

CStdioFile m_cSourceFile;
CStdioFile m_cDestFile;


int CImportParam::ReadFile()
{
     CString sReadLine;
     int iLineNumber=1;
     CFileException* pError=NULL;
     if (m_cSourceFile.Open(m_sImportFileName,CFile::modeRead,pError) == 0)
     {
          return -1;
     }
     m_cSourceFile.SeekToBegin();
     while (m_cSourceFile.ReadString(sReadLine) == 1)
     {

          ... Do something with the retrived line (sReadLine) ....

          iLineNumber++;
     }
     m_cSourceFile.Close();
     return 2;
}

int CImportParam::CreateOutputFile()
{
     int iLineNumber=1;
     CFileException* pError=NULL;
     CString sReadLine;
     CString sWriteLine;
     int iFindChar;
     if (m_cSourceFile.Open(m_sImportFileName,CFile::modeRead,pError) == 0)
     {
          return -1;
     }
     m_cSourceFile.SeekToBegin();
     // Creates an empty file. if the file exist delete it.
     if (m_cDestFile.Open(m_sOutputPath,CFile::modeWrite | CFile::modeCreate,pError) == 0)
     {
          return -1;
     }
     while (m_cSourceFile.ReadString(sReadLine) == 1)
     {
          sWriteLine="";

          ... Do something...
          sWriteLine=sReadLine;
          m_cDestFile.WriteString(sWriteLine);
          iLineNumber++;
     }
     m_cSourceFile.Close();
     m_cDestFile.Close();
     return 1;
}

V.T.Krishna.
0
 
LVL 2

Expert Comment

by:venkaiah
ID: 10380195
Hi,

                CFile            File;
      CString            str;
      char            *pBuffer;
      CFileDialog OpenDlg(TRUE);
      if(OpenDlg.DoModal()!=IDOK)
            return;
      File.Open(OpenDlg.GetFileName(),CFile::modeRead);
      pBuffer=new char[File.GetLength()];
      File.Read(pBuffer,File.GetLength());
      pBuffer[File.GetLength()]='\0';
      str=pBuffer;
The above code is enough for reading the data from any type of file.
0
 
LVL 2

Expert Comment

by:jonnykeogh
ID: 10397550
KurtVon - Thank you SO much!! That really helped me finish my program!!

(I use Borland C++)

Jonny K
(I may be a kid - but I'm a mighty programmer!!)
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Progress
Suggested Courses
Course of the Month8 days, 14 hours left to enroll

765 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