Solved

CFile Read using MFC

Posted on 2004-04-16
6
15,991 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 48
Strange date stored 22 106
How do you programatically show and hide the Windows 10 On-Screen-Keyboard? 3 533
dog bark java program 15 87
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

22 Experts available now in Live!

Get 1:1 Help Now