|
1. I’m trying to create an MFC library, the constructor of which reads from a file and updates an array.
CSentenceAr.h
#pragma once
#include <iostream>
class CSentenceAr
{
public:
CSentenceAr(LPCTSTR strFileToRead);
virtual ~CSentenceAr(void);
void sortSentences(bool bAscending);
int HowManyWords(const CString *pszWord);
private:
CStringArray m_strSentences;
};
#include "StdAfx.h"
#include ".\sentencear.h"
#define MAX_SENTENCES_PER_FILE 100
#define MAX_SENTENCE_LENGTH 1000
CSentenceAr.cpp
CSentenceAr::CSentenceAr(LPCTSTR strFileToRead)
{
CFile fileOp;
fileOp.Open(strFileToRead, CFile::modeCreate|CFile::modeRead);
char strLine[MAX_SENTENCE_LENGTH];
fileOp.Read(strLine,MAX_SENTENCE_LENGTH);
m_strSentences.Add(strLine);
}
CSentenceAr::~CSentenceAr(void)
{
}
I would like to read it line by line. Will the .Read method read the file to the end? How do I include the library file (preferably as a .dll, so I can add it as a reference) within another project to test it?
2. I tried to add an fstream object within this dll, but the compiler was returning the following error:
SentenceAr.cpp(12): error C2065: 'fstream' : undeclared identifier
Code:
#include "StdAfx.h"
#include ".\sentencear.h"
#include <fstream>
#define MAX_SENTENCES_PER_FILE 100
#define MAX_SENTENCE_LENGTH 1000
CSentenceAr::CSentenceAr(LPCTSTR strFileToRead)
{
CFile fileOp;
fstream mydamnFile;
fileOp.Open(strFileToRead, CFile::modeCreate|CFile::modeRead);
char strLine[MAX_SENTENCE_LENGTH];
fileOp.Read(strLine,MAX_SENTENCE_LENGTH);
m_strSentences.Add(strLine);
}
CSentenceAr::~CSentenceAr(void)
{
}
Jon
|
|
|
|
|
Well, I think your design could be improved for a start - why are you trying to read the entire file in the class constructor ?? not good - at best you should use it to initialize class member variables (like the filename) and such, and have a seperate method/function to perform the file read - but this is your issue, not mine
the guts of what you want is below, btw - notice 'ReadString'
>>>
CStdioFile XrefFile;
CString inString = "";
try {
XrefFile.Open(strInputFile, CFile::modeRead | CFile::typeText, NULL);
}
catch (CFileException e) {
//MessageBox("The file " + filename + " could not be opened for reading", "File Open Failed", MB_ICONHAND|MB_ICONSTOP|MB_ICONERROR );
return -1;
}
while (XrefFile.ReadString(inString)) {
// Do Something with inString
}
XrefFile.Close();
|
|
|
|
|
NOTE: CFile::Open DOES NOT throw any exception, so in your example try/catch is useless.
Ovidiu Cucu
Microsoft MVP - Visual C++
|
|
|
|
|
|
You are welcome. Anytime
Ovidiu Cucu
Microsoft MVP - Visual C++
|
|
|
|
|
Here is a simple function which reads a text file line by line and fills a CStringArray:
void CSentenceAr::ReadTextFile(LPCTSTR pszFile)
{
m_strSentences.RemoveAll();
TRY
{
CStdioFile file(pszFile, CFile::modeRead);
CString strLine;
while(file.ReadString(strLine))
{
m_strSentences.Add(strLine);
}
}
CATCH(CFileException, e)
{
e->ReportError();
}
END_CATCH
}
NOTE: Once you can solve the problem using only MFC, it has no sense to mix STL (fstream) stuff.
However, when you do it, don't forget 'using namespace std', or write 'std::fstream'
Ovidiu Cucu
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks for the solution
Jon
|
|
|
|
|
Hi,
I need someones help to Convert C++ strings to a C-style string (char*) in VC++ 6.0.
What I need to do is something as follows.
There is a string as,
string sh = “hello”;
I need to take the string in sh to
const char* tempd1 = .....
Thus I need someones help to do this.
Also if you can send me a way to convert a array of char(char st[]) to const char* .
Thanks
Dhananjaya
|
|
|
|
|
string s = "Hello";
const char *pCSTR = s.c_str();
Steve
|
|
|
|
|
Dhananjayak02 wrote: I need to take the string in sh to
std::string sh = "hello";
const char* tempd1 = sh.c_str();
or if u want the string to copy to another pointer
int n = sh.size();
char* c = new char[n + 1];
strcpy( c, sh.c_str());
Dhananjayak02 wrote: if you can send me a way to convert a array of char(char st[]) to const char*
char ch[20];
const char* pch = ch;
nave
|
|
|
|
|
Dhananjayak02 wrote: convert a array of char(char st[]) to const char* .
Before you try to *convert* types , you try to understand what are they.
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
I dont know why the hell the script for voting 5 is disabled only for me??
|
|
|
|
|
|
Dhananjayak02 wrote: a array of char(char st[]) to const char* .
they are almost same thing!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
|
QuickDeveloper wrote: is there any function in VC to get the current screen resolution
Either take the window rect of desktop window or use
GetSystemMetrics() funtion with the following parameters
SM_CXSCREEN - for width
SM_CYSCREEN - for height
QuickDeveloper wrote: change the bitmap accordingly??
StretchBlt() can help
nave
|
|
|
|
|
You can use for get width and height and also see this code maybe it is some helpful to you
int x=GetSystemMetrics(SM_CXSCREEN)for x
in y=GetSystemMetrics(SM_CYSCREEN)for y
<br />
Graphics graphics(GetDC()->m_hDC);<br />
CRect rect;<br />
ImageAttributes imAttrib;<br />
Image *img;<br />
GetWindowRect(rect);<br />
ScreenToClient(rect);<br />
img = Image::FromFile(A2W("D:\\0.bmp")) ;<br />
graphics.DrawImage(img,Rect(0,0,rect.right,rect.bottom),<br />
0,0,img ->GetWidth(),img ->GetHeight(),<br />
UnitPixel,&imAttrib);<br />
|
|
|
|
|
QuickDeveloper wrote: 2)is there any function in VC to get the current screen resolution and change the bitmap accordingly??
Otherwise the only alternative is to make the application work only under specific resolution
SystemParametersInfo and StrechBlt api will help
QuickDeveloper wrote: Every morning I go through Forbes list of 40 richest people in the world. If my name is not in there, I go to work..!!!"
good quote!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
hi
how can i hide a process?
|
|
|
|
|
farshad.f wrote: how can i hide a process?
hide from where?
nave
|
|
|
|
|
If it was easy to hide a process every virus/trojan would be doing it already.
Steve
|
|
|
|
|
1. By closing it.
2. By renaming it.
<marquee scrollamount="1" scrolldelay="1" direction="up" height="10" step="1">--[ ]--
[My Current Status]
I dont know why the hell the script for voting 5 is disabled only for me??
|
|
|
|
|
VuNic wrote: 2. By renaming it.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
farshad.f wrote: how can i hide a process?
yes you can ShowWindow(SW_SHOW)... be more specfic while asking question?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
If anybody have used NetMeetin 3 SDK componant please reply. I need help on this.
Thank you.
KIRAN PINJARLA
|
|
|
|
|
kiran.pinjarla wrote: If anybody have used NetMeetin 3 SDK componant please reply. I need help on this
exactly what is your problem!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|