Find Indexof in MFC C++ - c++

How can I find the indexof in GetWindowsText?
I just want to get extensions from box->fileExt->GetWindowsText(save);
For example my input is .exe .txt .bmp
So I want to get them separately. For example something like that:
.exe
.txt
.bmp
Currently my code is this:
for (int i = 0; i < files; i++)
{
box->testBox1.AddString(save);
fileExtensions.Add(save)`enter code here`;
CString check;
box->fileExt.GetWindowText(check);
CString store = check;
check.Find(' ') == save;
break;
continue;
if (fileExtensions[fileCounter] == store)
{
box->textBox2.AddString(fileExtensions[fileCounter]);
fileCounter++;
}
}//end for
It does not work.

You need to split string based on separator character(s). You can use Tokenize method of CString to do that:
CString sExtensions(_T(".exe .txt .bmp"));
CString sExt;
int nCurPos = 0;
CString sSeparators(_T(" ;"));
CStringArray Extensions;
sExt = sExtensions.Tokenize(sSeparators, nCurPos);
while (!sExt.IsEmpty())
{
Extensions.Add(sExt);
sExt = sExtensions.Tokenize(sSeparators, nCurPos);
}

Related

C++/MFC. Insert Image in CSV file Using MFC

Hello I'm studying MFC and I wanna know how to insert some images un csv file.
The file structure is as follows:The result folder contains 1.jpg, 2.jpg files.
In csv file, at the top "Index, Name, Age, Picture" must be included and "Index, Name, Age" are in the List Control.
I've finished entering the information in the csv file using the code below. However, I can't figure out how to insert the images in csv file.
`
CString _FilePath = theApp.m_ResultDir + _T("Result.csv"); //m_ResultDir : result folder Location
std::ofstream File(_FilePath,'w');
File << "Index, Name, Age, Picture\n";
CHeaderCtrl* pHeader = (CHeaderCtrl*)m_ListControl.GetHeaderCtrl();
int nRow = m_ListControl.GetItemCount();
int nCol = pHeader->GetItemCount();
CString text;
for (int i = 0; i < nRow; i++)
{
text = "";
for (int j = 0; j < nCol; j++)
{
text = text+ m_ListControl.GetItemText(i, j) + _T(", ");
}
File << text + "\n";
}
File.close();
`
It would be easy problem, but I'd appreciate it if you understand because it's my first time doing this.
This are what I tried.
First, I tried using TypeLib and select excel.exe and i contained some header files. However, I wanna make it csv file not xlsx file.
Second, using result folder location, I tried to add images. but failed.
`
CString image;
image.Format(theApp.m_ResultDir+_T("%d.jpg"), i+1);
text += image;
`
First argument to CString::Format is a format specification, followed by the arguments to be formatted. So something like that would work:
image.Format(_T("%s%d.jpg"), theApp.m_ResultDir, i+1);

C Builder (C++) AnsiString Length method

I am used to program in c#, but now i had to help my roommate with a c++ project.
This is the "not working code" :
void HighlightKeyWords::Highlight(TRichEdit eMemo,TRichEdit RichEdit1)
{
ifstream file("KeyWords.txt");
AnsiString temp;
int maxWordLength=0;
if(file.is_open())
{
while(file>>temp)
{ if(temp.Length()> maxWordLength)
{
maxWordLength=temp.Trim().Length();
}
keyWords.push_back(temp);
}
file.close();
}
else
{
ShowMessage("Unable to open file. ");
}
for(unsigned i=0;i<KeyWords.size();i++)
{
richEdit1->Text=KeyWords[i];
}
eMemo->Text=MaxWordLength;
}
I get a list of keywords from the file. In MaxWordLength i want to know to maximum length of a word ( words are separated by new line in the text file ). When I do the temp.Length, i get 695 ( the number of all characters in the file ). Why am I not getting the actual length of the word i am adding to the vector?
Thank you!
LE: I also did the MaxWordLength logic in the for below, the for where i put the items in the RichEdit.
Use file.getline() instead of the >> operator, which won't produce the desired output in your case, but gives you the full file content as result. So AnsiString().Length() is not your problem. Just modify part of your code to get it working as intended:
char buffer[255];
if(file.is_open()){
while(file.getline(buffer, sizeof(buffer))){
temp = AnsiString(buffer).Trim();
if(temp.Length()> maxWordLength) maxWordLength=temp.Length();
keyWords.push_back(temp);
}
file.close();
}

Extract words from txt file preceded by a seperator or blank space

try
{
CStdioFile file(_T("D:\\thedirectory\\1.txt"), CFile::modeRead);
CString str,mainstr = _T("");
while(file.ReadString(str))
{
mainstr += str;
mainstr += _T("\r\n");
}
CWnd *editwindow = this->GetDlgItem(IDC_EDIT2);
editwindow->SetWindowText(mainstr);
}
catch(CException* e)
{
MessageBox(_T("no such file"));
e->Delete();
}
I have managed to read a .txt file, and then update an edit control box with the contents. works great, but now i want to extract only the 2nd, 3rd, 4th, 5th word separately from the txt file. Any ideas?
Something like:
int i = 0;
while(file.ReadString(str))
{
i++;
if (i == 1) {
mainstr += str;
mainstr += _T("\r\n");
}
}
May be a good place to start experimenting. You can play with the value of i when intializing, placement of the initilizing variable, etc.
You could use a find method from CString to find a "word separating" character, then use a substring method to extract the word.
Search StackOverflow for "CString parse" or "CString regular expression".

How to separate a executing path to file path and parameter?

There are such lines as
C:\Program Files\Realtek\Audio\HDA\RAVCpl64.exe -s
in the registry key
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
Now I want to separate the absolute path and parameters from the line.
If the line is
C:\Space Dir\Dot.Dir\Sample Name.Dot.exe param path."C:\Space Dir\Dot.Dir\Sample Name.Dot.exe"
Which separator should I use to deal with this line? Is there any Windows API function to solve this problem?
The function you want is in the standard C library that you can use in Windows.
char theDrive[5],thePath[MAX_PATH],theFilename[MAX_PATH],theExtension[MAX_PATH];
_splitpath(theSCTDataFilename,theDrive,thePath,theFilename,theExtension);
You can also use a more general tokenizing function like this which takes any string, a char and a CStringArray..
void tokenizeString(CString theString, TCHAR theToken, CStringArray *theParameters)
{
CString temp = "";
int i = 0;
for(i = 0; i < theString.GetLength(); i++ )
{
if (theString.GetAt(i) != theToken)
{
temp += theString.GetAt(i);
}
else
{
theParameters->Add(temp);
temp = "";
}
if(i == theString.GetLength()-1)
theParameters->Add(temp);
}
}
CStringArray thePathParts;
tokenizeString("your\complex\path\of\strings\separated\by\slashes",'/',&thePathParts);
This will give you an array of CString (CStringArray object) that contains each section of the input string. You can use this function to parse the major chunks then the minor ones as long as you know the seperator charactor you want to split the string on.

Help improve this INI parsing code

This is something simple I came up with for this question. I'm not entirely happy with it and I saw it as a chance to help improve my use of STL and streams based programming.
std::wifstream file(L"\\Windows\\myini.ini");
if (file)
{
bool section=false;
while (!file.eof())
{
std::wstring line;
std::getline(file, line);
if (line.empty()) continue;
switch (line[0])
{
// new header
case L'[':
{
std::wstring header;
size_t pos=line.find(L']');
if (pos!=std::wstring::npos)
{
header=line.substr(1, pos);
if (header==L"Section")
section=true;
else
section=false;
}
}
break;
// comments
case ';':
case ' ':
case '#':
break;
// var=value
default:
{
if (!section) continue;
// what if the name = value does not have white space?
// what if the value is enclosed in quotes?
std::wstring name, dummy, value;
lineStm >> name >> dummy;
ws(lineStm);
WCHAR _value[256];
lineStm.getline(_value, ELEMENTS(_value));
value=_value;
}
}
}
}
How would you improve this? Please do not recommend alternative libraries - I just want a simple method for parsing out some config strings from an INI file.
// what if the name = value does not have white space?
// what if the value is enclosed in quotes?
I would use boost::regex to match for every different type of element, something like:
boost::smatch matches;
boost::regex name_value("(\S+)\s*=\s*(\S+)");
if(boost::regex_match(line, matches, name_value))
{
name = matches[1];
value = matches[2];
}
the regular expressions might need some tweaking.
I would also replace de stream.getline with std::getline, getting rid of the static char array.
This:
for (size_t i=1; i<line.length(); i++)
{
if (line[i]!=L']')
header.push_back(line[i]);
else
break;
}
should be simplified by a call to wstrchr, wcschr, WSTRCHR, or something else, depending on what platform you are on.
// how to get a line into a string in one go?
Use the (nonmember) getline function from the standard string header.