Solved

Convert Data....???

Posted on 2001-07-06
5
208 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
5 Comments
 
LVL 12

Accepted Solution

by:
migel earned 100 total points
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
not able to insert into temp table 68 150
maven archtype selection in eclipse 1 51
has22 challenge 11 71
sumHeights2  challenge 7 75
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

17 Experts available now in Live!

Get 1:1 Help Now