|
Hi,
I am working in text file application.In that application, to opening the file in read only mode like
fstream lpFile;
lpFile.open ( "sample.txt", ios::in );
then i read the file using this file pointer and some times , i move the pointer back to 1 position from current position using seekg ( -1, ios::cur ).
Text file size : 15 KB
Problem:
I couldn't move the file pointer to back using seekg function ( in some case only).I tried to clear the error flag if any before the seekg function. but no result.The problem still continue...
lpFile.clear ( );
lpFile.seekg ( -1, ios::cur );
Sample Text File:
[General]
To read the file information
like name,address,mobile number
[Address1]
Name=vichu
Address=India
Mobile=+919994445550
[Address2]
...
[Addressn]
Name=vishva
Address=chennai
mobile=100
****end of file-----
this is raw text file.
program code:
char chData;
lpFile.get ( chData );
while ( chData != '[' )
{
...
...
}
lpfile.clear ( );
lpFile.seekg ( -1, ios::cur );
Again start the above loop from '[' character.
The seekg function not work sometimes. How can i solve the probelm?
Is there any tool available to find the special character or unknown character in text file?
Thanks & Regards,
-- modified at 2:19 Thursday 15th February, 2007
Vichu
|
|
|
|
|
Just a thought: I think (but haven't verified it), that a file has to be opened in binary mode for seek:s (and tell) to work properly.
Maybe that's your problem?
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
Hi Kakan,
I am using text mode only.The text file contains upto 50 lines.During the process, the seekg function is not working at some times only.Most of the time, the file pointer seek to previous position successfully. I don't known the exact reason for this seek function behavior.
Is any other way to correct this problem?
Vichu
|
|
|
|
|
Yes, what I already said: use binary mode for the file handling. Or rather: try, I still haven't checked that I'm correct (but I'm pretty sure I'm right).
The only thing you will have to put up with, using binary mode, is that you will get CR/LF-sequences as they exists in the file. That is, no conversion to a single \n.
An easy way to overcome this "problem" is to simply ignore all CR characters read from the file.
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
|
|
|
|
|
I will try the binary mode operation and then update you.
Thanks kakan.
Vichu
|
|
|
|
|
Hi kakan,
I fixed that seekg problem. Just I read the text file in binary mode rather than text mode.
Thank you for your response.
Regards,
Vichu
|
|
|
|
|
If your file is really formatted as you have shown, you might want to use GetPrivateProfileString and SetPrivateProfileString instead. They are intended to handle files with the format you show
[SECTION]
Key=Value
Key2=Value2
Hope that helps.
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
|
|
|
|
|
Hi krmed,
You are exactly correct. But in my application, I am using fstream class and its member function to read/write the text file. The seekg function works well in binary mode.
Thank you for your information.
Vichu
|
|
|
|
|
Hi all.
I have some problem, I need class thet manage files as CFile in MFC, but in console.
can anyone help me.
Thanks for any help.
|
|
|
|
|
MFC can be used in Console application also. So use MFC, and then u can use its CFile also.
Come online at:-
jubinc@skype
|
|
|
|
|
david bagaturia wrote: I have some problem, I need class thet manage files as CFile in MFC, but in console.
Look up AfxWinInit in MSDN. There is a sample along with it.
Also when you create a console project you have an option where you can specify whether you want MFC support for your application or not.
|
|
|
|
|
You can use CFile in a console app. The AppWizard for console apps has a checkbox for adding MFC support.
|
|
|
|
|
Tanks all.
It will useful for me, it solve a proble in my other project sow it help me lot, but in this time I am writing service and I need console, I can't use MFC.
|
|
|
|
|
david bagaturia wrote: but in this time I am writing service and I need console, I can't use MFC.
What about fstream ?
|
|
|
|
|
Thanks all.
I have solve problem!!
|
|
|
|
|
Hi,
I am working on RichEdit Control,I am using it in Wizard Dialog.
I want to append & insert Unicode Text at specific position in RichEdit in the format such as (BOLD,ITALIC,ARIAL etc.).
if anyone has idea please tell me.
Thanks,
Swapnil.
|
|
|
|
|
I think you aksed this question on previous messages I suppose you couldn't find answer,If I remember true I think I saw a discuss on the www.codeguru.com(Visual c++ section) about RichEdit(format text,...) see it yesterday
|
|
|
|
|
I'm pretty sure I answered yesterday. Since you didn't believe that answer (apparently) then
maybe you haven't read anything about Rich Edit Controls[^]...
Great job, team. Head back to base for debriefing and cocktails.
|
|
|
|
|
I have a fragment of code as an example, and I'm trying to figure out what it does and how.
void WINAPI CLeft::Abc(LinkPtr pData)<br />
{<br />
g_Spec().Xyz(pData);<br />
}
I know CLeft::CLeft is a global outside of any class. It receives callbacks from a server.
I know Xyz is in a class.
I know the pointer pData is being passed.
I think both Abc and Xyz are defined in the same file.
I'm not sure what g_Spec().Xyz() really is.
I'm trying to figure out how and where one defines g_Spec().Xyz(pData) so it can
1)be referenced by Xyz
2)be a class or within a class
Anything look familiar? I really don't know if there is enough here to provide sufficient information.
I am currently using the following and don't like it. It seems too complex, but that's how I got it to work.
Abc is outside any class.
Xyz is a function in CNewDlg.
void WINAPI Abc(LinkPtr pData)<br />
{<br />
((CNewDlg*)AfxGetApp()->m_pMainWnd)->Xyz(pData); <---I don't like this
}
Thanks
|
|
|
|
|
Oliver123 wrote: I'm not sure what g_Spec().Xyz()really is.
From its name it semms to be a global, and from its use I think it could be a global function, returning the reference to a singelton-instance of a class providing a function called Xyz , which takes a parameter that very well could be a LinkPtr .
Oliver123 wrote: void WINAPI Abc(LinkPtr pData)
{
((CNewDlg*)AfxGetApp()->m_pMainWnd)->Xyz(pData); <---I don't like this
}
Have you considered sending a user defined message?
Failure is not an option - it's built right in.
|
|
|
|
|
i have to list all the hardware devices in the system by c language. is there any function to display all the devices. anyone know this. please help me
Arise Awake Stop Not Till ur Goal is Reached.
|
|
|
|
|
Please don't keep repeating the same question. There is no simple function. You could get all the information you need from the registry.
|
|
|
|
|
Hey, I want to creat a new folder with a new file. Now my code can only creat a new folder but no file.What is the wrong with my code?Please help anyway you can.. Thanks in advance.
m_Luting is the name of the folder , type CString
m_Name is the name of the file ,type CString
My code :
char path[300];
CString stname=".bin";
CString cell;
CreateDirectory(_T(m_Luting), NULL);
strcat(path,m_Luting);
strcat(path,m_Name);
strcat(path,stname);
fstream fMyfile;
fMyfile.open(path,ios:out|ios::binary|ios::trunc);
if(!fMyfile){ exit(-1);}
fMyfile.close();
|
|
|
|
|
zhangguoliming wrote: strcat(path,m_Luting);
This should probably be strcpy() instead.
zhangguoliming wrote: fMyfile.open(path,ios:out|ios::binary|ios::trunc);
What is the value of path at this point?
On a side note, since you are using MFC, have you looked into CFile ?
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
1) don't put m_Luting in a _T() macro.
2) Since you're using MFC, use it all the way...
if (CreateDirectory(m_Luting))
{
CString sPath;
sPath.Format("%s\\%s%s", m_Luting, m_Name, stName);
CFile file;
CFileException e;
if (file.Open(sPath, CFile::modeCreate | CFile::modeWrite, &e)
{
#ifdef _DEBUG
afxDump << "File could not be opened " << e.m_cause << "\n";
#endif
return;
}
else
{
file.Close();
}
}
else
{
AfxMessageBox("Could not create folder.");
}
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|