Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CFile Read using MFC

Posted on 2004-04-16
6
Medium Priority
?
16,339 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
[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
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Suggested Courses

636 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