?
Solved

CFile Read using MFC

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Suggested Courses

800 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