|
At the moment I'm trying to make an effort to use std::string and std::ifstream in my work, instead of using "stdio.h" . My main objective is to write elegant code -- performance is not an issue!
I need to read a lot of blocks (of various sizes) from file, and append them to various string buffers. The problem is that functions like ifstream::read are designed to read into char* style buffers, not string s; however there seems to be some kind of relationship between streambuf s and string s that I figured might make my life easier...?
using namespace std;
...
string mystring;
ifstream inputfile;
int length = 20; ...
char* buffer = new char [length];
inputfile.read (buffer, length);
mystring += buffer;
delete[] buffer;
char ch;
for (int i=0; i<length; i++)
{
inputfile >> ch;
mystring += ch;
}
I have the gut feeling that there must be a really elegant way to do this, using some combination of streambuf / ifstream::operator>> / ifstream::read etc. I figured the whole point of using std::string was that it can worry about the allocation side of things behind the scenes. However I'm finding the documentation a little cryptic and generalised.
Any ideas?
It's quite possible I just want the moon on a stick and I should go back to BASIC...
|
|
|
|
|
LiquidEyes wrote:
Any ideas?
getline(inputfile, mystring); will read a string from a file. You can pass a third parameter (a character) that specifies a delimiter to use during the reading if you need to. By default, it stops after a newline character.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Thanks for the reply. However I don't see how it will know to stop after (say) 20 characters? Without a delimeter, will it read the whole file into the string?
I want to do the STL equivalent of
fread( mystring, sizeof(char), length, inputfile );
or failing that, use something more elegant than a char[] (which I have to allocate myself) as an intermediate buffer.
|
|
|
|
|
From memory I think you can do getline(infile, mystring, "\n") or something.
Kevin
|
|
|
|
|
I don't want it to stop on 'newline'. I want it to read a fixed number of bytes.
|
|
|
|
|
I couldn't find that getline with no. of bytes that Ray mentioned. I suppose you could use the getline that gets the whole line and then use a substring function. Not as elegant but still avoids char*.
Kevin
|
|
|
|
|
LiquidEyes wrote:
However I don't see how it will know to stop after (say) 20 characters?
It doesn't. You'll have to use the read() method of ifstream to do that.
LiquidEyes wrote:
Without a delimeter, will it read the whole file into the string?
No, as I said, the default delimiter is a newline - it will read a single line.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
You'll have to use the read() method of ifstream to do that.
Precisely. Which takes us back to my original question ... what structure can I read into instead of an old fashioned char* array?
I just thought there might be an elegant way, using vector s or string s or something, that didn't involve allocating char s manually. As I said, maybe I just want the moon on a stick.
|
|
|
|
|
LiquidEyes wrote:
what structure can I read into instead of an old fashioned char* array?
Just quickly looking at the docs, it appears you can use ifstream::getline(mystring, number_of_chars) . If you pass a size of, say, 10, it reads 9 characters and appends a NULL. Once again, you can add a third parameter which is the delimiter if you need it.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Hi all,
I'm running myProg.exe and while it is still process, I killed the process by either going to task manager or right click and close. Then sometime, when I rebuild it again, it failed to rebuild.
<br />
myProg Command line warning D4028 : minimal rebuild failure, reverting to normal build<br />
C:\myProg\myProg\myProg.cpp fatal error C1033: cannot open program database 'c:\myProg\myProg\deug\vc70.idb'<br />
C:\myProg\myProg\myProg.cpp fatal error C1033: cannot open program database 'c:\myProg\myProg\deug\myProg.pdb'<br />
<br />
I have to either delete the myProg.pdb manually or if I can't (sometime get the error message saying, myProg.pdb is still in used), I have to restart the computer in order to rebuild the project..
Is anyone of you have the same experience that I'm facing.. please tell me what is wrong with it?
Thank you.
|
|
|
|
|
pnpfriend wrote:
I killed the process by either going to task manager...sometime get the error message saying, myProg.pdb is still in used...
Are you killing the myprog.exe or msdev.exe process?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
|
If I create a window in another window in the non-client area then send the WM_PAINT message to the child window if the parent receives a WM_NCPAINT message, the client window doesn't redraw (the non-client area it is on is still garbled with the old pixels that were in those rect). How can I solve this?
|
|
|
|
|
hello
i want to make ActiveX control (VC++6.0) build from other ActiveX controls created in run time. i used MFC ActiveX Wizard to generate the code and now:
1) i have AfxEnableControlContainer() in InitInstance
2) in OnDraw i'm trying to do sth. like:
this->CreateControl("QButton.Button", "", WS_VISIBLE, rc, this, IDR_BATON, NULL, FALSE, NULL);
but it causes run time error (assertion failed with hwnd argument).
can anybody help me?
thanks in advance
--
s3b0
|
|
|
|
|
i have two vectors i am trying to compare the first is a string vector and the other is a char vector. i am trying to find for example the word hello in the grid vector and i can find the fist char but i dont know how to move or set up a loop to check all the possible directions for the next char in the string. here is a bit of my code. Once the first char is found i need to find the next which the next char could be in 9 possible directions.
-String Vector-
01234
0hello 1apple
2car
3me 4men
5ran
6woman
-Grid Vector-
01234
0cenad
1aatps
2rnrpj
3hello
4wmneo
/*------------------- A Word Checker ----------------------*/
for(int i=0; i < GridVector.size(); i++)
{
for(int j=0; j < GridVector.size(); j++)
{
if(StringVector[0][0] == GridVector[i][j])
{
{
}
}
|
|
|
|
|
Does anyone have a spell checker that they recommend for integration into Windows apps? We are looking for a quality once, it does NOT have to be free. It should be able to be integrated with several different technologies, including Borland C++, Delphi, and Microsoft C++ and .NET (so I presume that an API with a COM interface is the best way to go).
Thanks for any suggestions,
~Steve
|
|
|
|
|
Oops, forgot something... it would be a great benefit if the dictionary was available with medical terms already in it.
~Steve
|
|
|
|
|
There is one (International CorrectSpell) offered by Lernout & Hauspie Speech Products.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
David, thanks for the suggestion... I'll look into that one.
~Steve
|
|
|
|
|
DavidCrow wrote:
Lernout & Hauspie Speech Products.
That company went belly up long time ago.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Nemanja Trifunovic wrote:
That company went belly up long time ago.
Be that as it may (I can neither confirm nor deny), the product, at least the 1993 version, is still licensed to them and is still in use by Microsoft Office XP.
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
|
|
|
|
|
|
Thanks for the links, especially the componentsource one... that look like one that I'll have to keep on file for future projects.
~Steve
|
|
|
|
|
I handle the right click in a CTreeCtrl with
ON_NOTIFY( NM_RCLICK, IDC_TREE1, OnTreeRclick )
and
void CTestTreeDlg::OnTreeRclick( NMHDR* pNMHDR, LRESULT* pResult )
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
HTREEITEM hSelItem = pNMTreeView->itemNew.hItem;
}
When I right click on the tree background, not on a leaf or node, the hSelItem is not valid, and is not a "simple" value like NULL or zero.
Is there a way to check to see if the HTREEITEM item is valid ?
I tried the following to see if I can get the item based on the hitem the notification message gave back, it could "work" but it gives a "First-chance exception" when passing a bad hitem.
TVITEM item;
TCHAR szText[1024];
item.hItem = hSelItem;
item.mask = TVIF_TEXT | TVIF_HANDLE;
item.pszText = szText;
item.cchTextMax = 1024;
BOOL bWorked = m_Tree->GetItem(&item);
Thanks.
Max.
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
I would use:
void CTestTreeDlg::OnTreeRclick( NMHDR* pNMHDR, LRESULT* pResult )
{
CPoint point(GetMessagePos());
m_tree.ScreenToClient(&point);
<br>
HTREEITEM hi=m_tree.HitTest(point);
<br>
if (hi)
{
}
} where m_tree is the CTreeCtrl member associated with your control.
Maybe not the best method but it works.
"though nothing
will keep us together
we can beat them
for ever and ever"
rechi
|
|
|
|