Solved

CFile Read using MFC

Posted on 2004-04-16
6
15,935 Views
Last Modified: 2013-11-20
Hello!
I have a file on hard disk and file has 2 numbers separated by "," .I want to read the numbers from the file using CFile and store the numbers in  2 different int variables.
I am not able to read the numbers from file.Please can u help me out in doing this work.I am pasting my code here.
Regards.

// Multiply.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Multiply.h"
#include <iostream>;
using namespace std;

Multiply::Multiply()
{
      
      CFile file2;
               
               CString str;
              CString buff;



TCHAR* pBuff;
str=file2.Open("c:\\def.txt", CFile::modeRead);
      

int len = file2.GetLength();

pBuff=buff.GetBuffer(len+1);


str=file2.Read(pBuff, len);
 

buff=str;
file2.Close();
pBuff[len]='\0';
buff.ReleaseBuffer();

pBuff=NULL;

file1.Close();

cout<<str;
}
void main()
{
Multiply mul;
}






0
Comment
Question by:SarwanGul
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10845183
Use CStdioFile class and CStdioFile::ReadString function:

BOOL ReadString(CString& rString);

Having CString variable use sscanf to read two int variables from it.
0
 
LVL 7

Expert Comment

by:leflon
ID: 10845202
Hi SarwanGul,

your code looks a little weird right now.
let me see if i got your problem right.

you have a file (say 'def.txt') which consits of two numbers seperated by a ','.

[def.txt]
123,456
or
[def.txt]
12.34,56.78

you like to read this numbers and store them to two variables (lets say two double vars).

is this correct??

if so
you should
- open the file for reading (you did this, but the return value of Open func is not a CString but a BOOL telling you if the open was successful)
- read the line to a string(you have to decide if you like to use MFC obejcts and functions -CFile, CString etc.- or the C++ specific string and iostream rountines)
- split the string and assign numbers to values

need any help with this??

leflon
0
 
LVL 2

Accepted Solution

by:
dxdat99 earned 500 total points
ID: 10847625
Hi!
U can use this code

----------
    CFile   pfile;
    CString str;
    CString buff;
    long    lNum1 = 0;
    long    lNum2 = 0;
    TCHAR *tcData;
    if(!pfile.Open("d:\\Text.txt", CFile::modeRead))
    {
        return;
    }
    int len = pfile.GetLength();      
    tcData = new TCHAR[len + 1];
    tcData[len] = '\0';
    pfile.Read(tcData, len);
    CString szBuff(tcData);
    int pos = szBuff.Find(_T(","));
    if(pos > 0)  // if found ',' character
    {
        lNum1 = _ttol(szBuff.Left(pos + 1));
        lNum2 = _ttol(szBuff.Right(szBuff.GetLength() - (pos + 1)));
    }
    long lMul = lNum1 * lNum2;  // result
    delete[] tcData;
    pfile.Close();  
----------
Good luck
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

19 Experts available now in Live!

Get 1:1 Help Now