|
|
Hi,dear all,
I have a project created using C++ 6.0. Now I need to update it to VS2008, now I have a problem when I try to open a file.
In C++ 6.0
#include <fstream.h>
ifstream INPFile(INPFileName, ios::nocreate);
In VS2008
#include <fstream>
ifstream INPFile(INPFileName, ios::nocreate);
the second line get the following error:
Error 23 error C2039: 'nocreate' : is not a member of 'std::basic_ios<_Elem,_Traits>'
Error 24 error C2065: 'nocreate' : undeclared identifier
If I change the second line to the following:
ifstream INPFile(INPFileName)
then get new error as following
error 2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
How can I solve this issue?
Thanks!
|
|
|
|
|
This talks about the reason for the first error:
http://www.devx.com/tips/Tip/14544[^]
After your change however, the error about the private member doesn't make sense, you probably need to show a bit more of your code.
|
|
|
|
|
Albert, thanks for your quick reply.
My project has around 50 files, I already search all header files to check if any function pass fstream as argument, and modify them all to be passed as reference instead of as object.
I don't understand why if I use following code:
ifstream fin(OutputF, ios::in,ios::nocreate);
I got error like "C2039: 'nocreate' : is not...", but if I modify it to:
ifstream fin(OutputF, ios::in,ios::_Nocreate);
I got other kind of error messge:
"error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private.."
Thanks!
|
|
|
|
|
Wait, if you're doing an ifstream (input), why are you specifiying "no create" at all? That doesn't make sense, it's not going to get created if it can't find it. That's only even applicable at all if you're opening in read/write or write modes.
|
|
|
|
|
I do not think the nocreate option exists anymore, and it should not be necessary on an input stream. You need to use the latest definitions as described here[^].
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Thanks, Albert and Richard,
Both of you are right, yes, there is no nocreate.
I solve it by remove it, but the main reason I got the " cannot access private member ..." error is that I have some functions that pass ifstream object as value, they should be passed by reference.
Thanks again!
|
|
|
|
|
Hi,
I solve my issue posted yesterday, but today has another problem related to it.
When I migrate another small project from C++ to VS2008, I double check all the function delcaration to make sure fstream object is passed by reference or pointer, but when I compile program, I still got two error messages, they are same and the only two errors.
C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
In this case how should I find where the error come from?
|
|
|
|
|
I found some functions that pass std::istream or std::istrstream by value, not by reference, is that the reason to cause the problem?
|
|
|
|
|
Yes, they also need to be passed by reference, now can be compile now.
|
|
|
|
|
I would suppose that passing the fstream by value in 6.0 was wrong too. It might have worked but only by luck.
A fstream is a representation of a computer resource. Passing by value implicitly duplicates the representation without duplicating the actual resource. Thus it represents a broken design/implementation.
|
|
|
|
|
PLease show me how to search for a word in a text file using a listbox, String objects and Indexof, after i opened it in a richtextbox ,in visual C++, please help!!
|
|
|
|
|
byank wrote: PLease show me how to search for a word in a text file using a listbox...
What does a listbox have to do with searching a file?
Are you using MFC or Win32?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
it's just that i've seen here on codeproject the code for searching words in a text files using vb, i think. I need it in c++. thanks
|
|
|
|
|
I understand that, but what does a listbox have to do with anything? It just seems to be "noise" in the overall problem.
Have you tried:
open the file
while not EOF
read line from file
if line contains text, exit loop
end
close file The details deopend on whether you are using MFC, STL, Win32, or some combination thereof.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
If you are searching for a string in a text file do this:
CString oneLine; bool flag = FALSE; CStdioFile fS (_path_, CFile::modeRead ); int lineNumber = 0; while( fS.readLine(oneLine)) {
if( oneLine.Find(L"String")!= -1 ) { flag = TRUE; break; }
lineNumber++; }
See also:
Find
CStdioFile
|
|
|
|
|
Shaunak De wrote: If you are searching for a string in a text file...
Which I'm not, but thanks anyway, though.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
tahnks for your help ...i need this code in visual c++ and i use visual c++ 2008/2010. could you help me with this? I place the word that i want to search in a textbox and after i click on a button(search) i want to see where the word is. The file is loaded in a richtextbox . thanks a lot
|
|
|
|
|
|
your help was appreciated but what i wana do is to write the word in a text box and after i click on a button(search) it will show me where the word is (in my richtextbox where the text file is loaded) all these in my windows form. any idea? thanks a lot
|
|
|
|
|
Hello,
I need a function that would do a parse exact on a CString - given a format - and return a date (or an exception!)
Eg
COleDateTime timParsed;
timParsed.ParseExact("2002 10 3","yyyy mm d");
I am aware of ParseDateTime... but I need something where you actually specify the expected format! And I knoe ParseExact is in .net but hey I am stuck in the nineties!
Any idea?
Thanks!
Jerry
|
|
|
|
|
You may use time_from_string() from boost::posix_time , port the OpenSource code of the BSD strptime() function, or write a parser yourself (e.g. using sscanf() ).
|
|
|
|
|
I have had some success in the past using strftime() [^].
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
Thanks Jochen And Richard,
Yes I used an OpenSource version of strptime() that I found here: http://plibc.sourceforge.net/doxygen/strptime_8c-source.html[^]- and I have transformef the char(s) into TCHAR, added _T, etc....
and I ave written this:
{
...
CString strFormatCpp = TranslateFormatDate(strFormatVB);
tm timeDate;
memset(&timeDate,0,sizeof(tm ));
TCHAR * pRes = strptime (strMyDate, strFormatCpp, &timeDate);
if ( pRes != NULL )
{
COleDateTime oleDate;
oleDate.SetDateTime(1900 + timeDate.tm_year,timeDate.tm_mon+1,timeDate.tm_mday,timeDate.tm_hour,timeDate.tm_min,timeDate.tm_sec);
}
}
CString TranslateFormatDate(const CString & strFormat)
{
CString strResult;
int nChar = 0;
while ( nChar < strFormat.GetLength() )
{
switch ( strFormat[nChar] )
{
case 'A':
if ( MatchFormatKey(_T("AMPM"),_T("%p"),strFormat, nChar,strResult) )
continue;
break;
case 'a':
if ( MatchFormatKey(_T("ampm"),_T("%p"),strFormat, nChar,strResult) )
continue;
case 'y':
if ( MatchFormatKey(_T("yyyy"),_T("%Y"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("yy"),_T("%y"),strFormat, nChar,strResult) )
continue;
break;
case 'M':
if ( MatchFormatKey(_T("MMMM"),_T("%B"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("MMM"),_T("%b"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("MM"),_T("%m"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("M"),_T("%#m"),strFormat, nChar,strResult) )
continue;
break;
case 'd':
if ( MatchFormatKey(_T("dddd"),_T("%A"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("ddd"),_T("%a"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("dd"),_T("%d"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("d"),_T("%#d"),strFormat, nChar,strResult) )
continue;
break;
case 'h':
if ( MatchFormatKey(_T("hh"),_T("%I"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("h"),_T("%#I"),strFormat, nChar,strResult) )
continue;
break;
case 'H':
if ( MatchFormatKey(_T("HH"),_T("%H"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("H"),_T("%#H"),strFormat, nChar,strResult) )
continue;
break;
case 'm':
if ( MatchFormatKey(_T("mm"),_T("%M"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("m"),_T("%#M"),strFormat, nChar,strResult) )
continue;
break;
case 's':
if ( MatchFormatKey(_T("ss"),_T("%S"),strFormat, nChar,strResult) )
continue;
if ( MatchFormatKey(_T("s"),_T("%#S"),strFormat, nChar,strResult) )
continue;
break;
}
strResult+=strFormat[nChar];
nChar++;
}
return strResult;
}
bool MatchFormatKey(const CString & strKey,const CString & strTransKey,const CString & strFormat, int & nPos,CString & strResult)
{
if ( strFormat.Mid(nPos, strKey.GetLength()) != strKey )
return false;
strResult+=strTransKey;
nPos+=strKey.GetLength();
return true;
}
Thanks every-one!
Jerry
modified 18-Jul-12 9:49am.
|
|
|
|
|
Richard pointed out using strftime( ) . it is standard C.
#include <cstdio>
#include <ctime>
int main ()
{
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime (buffer,80,"%Y %m %d ",timeinfo);
puts (buffer);
return 0;
}
|
|
|
|
|