Solved

Reading a text file

Posted on 2003-11-10
4
3,693 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 50 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

This article will show, step by step, how to integrate R code into a R Sweave document
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

756 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