Solved

CFile Read using MFC

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 119
Unix / Linux grid computing 5 155
if loop in java 3 139
My project did see openJDK that I installed. What could be the problem 7 140
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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