Solved

Convert Data....???

Posted on 2001-07-06
5
212 Views
Last Modified: 2013-11-20
#include <fstream>
//Get data from text file (test.txt);
//Can I use CString to replace text file?
//And how?

ifstream m("test.txt");
CalcLexer lexer(m);

How to convert  data CString ->ifstream
(but I don't want to save CString ->textfile).
Thanks
0
Comment
Question by:pbtdanh
[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
5 Comments
 
LVL 12

Accepted Solution

by:
migel earned 100 total points
ID: 6259020
Hi!
to read data from file:
CString strBuffer;
// get size of the file
m.seekp(0, ios::end);
streampos lpos = m.tellp();
m.seekp(0, ios::beg);
// lpos now is the size of the stream
char* pszBuff = strBuffer.GetBufferSetLength(lpos + 1);
if (pszBuffer)
{
// read it in the buffer
ofs.read(pszBuffer, lpos);
strBuffer.ReleaseBuffer();
}

But if you already have data not in file
you can use stringstream intead ifstream  but iif your function uses ifstreamas parameter not basic_ios or basic_istream then you need rewrite it to use basic stream classes instead particular derived.
0
 
LVL 3

Expert Comment

by:pamboo
ID: 6259023
>>CalcLexer lexer(m);

will  u please explain ur problem elaborately

I think u want to read a file and store datas in a CString

0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 100 total points
ID: 6262544
pbtdanh,

You're using the wrong class to store/write data.
You need to use ostream or ofstream to write/ouput data.
You use istream or ifstream to read/input data.

Example:
#include <fstream>
#include <iostream>
#include <string>

void SomeFunction1(void)
{
std::ofstream m("test.txt");
CString data = "test 1";
m << (LPCTSTR)data;
m.close();
}

void SomeFunction2(void)
{
std::ofstream m("test.txt");
std::string data = "test 2";
m << data;
m.close();
}

If you want to read and write data, you can use fstream.

And as I previously stated, to conver a std::string to CString you can use the following method:
string szdata1 = "hello world";
CString szdata2 = szdata1.c_str();
0
 
LVL 30

Expert Comment

by:Axter
ID: 6262600
If you need to write to the stream, here are some examples:

void SomeFunction3(void)
{
     std::ifstream m("test.txt");
     std::string data;//Read complete line
     std::getline(m,data);
     m.close();
}

void SomeFunction4(void)
{
     std::ifstream m("test.txt");
     std::string data;
     m >> data;//Read until reaches first space
     m.close();
}

void SomeFunction5(void)
{
     std::ifstream m("test.txt");
     std::string data;
     for(;;) //Read everything
    {
          std::string tmp = "";
          std::getline(m,tmp);
          if (m.eof()) break;
          data += tmp + "\n";
    }
     m.close();
}
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9490246
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: Axter, migel (points to be split)

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 56
Smart Camera scanning and reading information 3 126
scoreUp challenge 14 74
Problem to App source 6 120
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 …
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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