|
Dear Sir, concerning your little search engine that finds A's in text files ..
Well, it works perfectly now, some minor alterations were made.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class FileSearcher
{
public:
string fileName,line;
char filename[256];
unsigned long fileSize, begin,end;
int Adder,count;
string *A;
public:
FileSearcher(char *file )
{
Adder=0;
count=0;
runMe(file);
}
~FileSearcher()
{
delete []A;
}
public:
void runMe(char *file)
{
cout<<"Enter the file name and complete path:> ";
cin>>filename;
fileName.assign( filename );
ifstream infile(filename);
infile.seekg (0, ios::end);
fileSize = infile.tellg();
infile.close();
cout << "size is: " << (fileSize) << " bytes.\n";
A=new string[fileSize+1];
infile.open(fileName.c_str());
Adder=0;
count=0;
while(!infile.eof())
{
getline(infile,line);
if(line[count]=='A'|| line[count]=='a')
{
cout<<"FOUND an A here ("<<line[count]<<") Line= "<<Adder<<": \n\n"<<line.c_str()<<"\n";
A[Adder]=strlen(line.c_str());
count++;
}
count=0;
Adder++;
}
infile.close();
cout<<"\n\n";
}
};
int main()
{
FileSearcher *myFileSearcher = new FileSearcher("c:\\t.txt");
return 0;
}
..
modified on Saturday, May 22, 2010 6:58 AM
|
|
|
|
|
It is perfect, for some strange version of perfection that I'm not sure that most experienced C++ programmers would recognise.
Faux pas the first: #pragma warning( disable : 4101 ) . Why are you disabling this warning? What defects does it hide in your code?
Faux pas the second: Using arrays when, in this example you just need std::string
Faux pas the third: Redundancy - reading stuff into a character array then assigning to a string when you could just read straight into the string
Faux pas the fourth: Closing the file, why not just rewind it to the beginning. Or stick getting the filesize in it's own function.
Faux pas the fifth: What are Adder and count for? Where I come from an Adder is a snake and the count is on Sesame Street. They're initialised, incremented and assigned and, er, pointless.
Faux pas the sixth: What's the point of an array of empty string, one per character in the input file?
Faux pas the seventh: Declaring variables miles away from using them
Faux pas the eighth: You can't see this one as you disabled the warning, bummer!
|
|
|
|
|
You are correct on all points.
Forgive my ignorance and lazyness , i stand corrected.
im used to passing the project down the development cycle
and letting someone better like for instance a senior software engineer do the cleaning up of code.
modified on Friday, May 21, 2010 6:01 AM
|
|
|
|
|
I'd like to step through the Microsoft code for dawing a CTabCtrl. In winctrl2.cpp, CTabCtrl::DrawItem consists only of ASSERT (FALSE); When I put a breakpoint here, it is not hit. If I put a break at CWnd::OnDrawItem it is hit, but stepping through doesn't land in the drawing code, and I don't know where to put a break to see the code that does the computations for the drawing.
Can you tell me how to set a breakpoint so I can step through this code? Thanks.
modified on Thursday, May 20, 2010 6:05 PM
|
|
|
|
|
Hi,
I am trying to add a combo box to provide a list of choice to the user. I add it to a dialogue box and entered Listbox items, but when I try to run to see the list, it is not there. I guess I need more work to make it work for me.
Please direct me
Thanks
|
|
|
|
|
mrby123 wrote: ...and entered Listbox items...
How?
"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
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
By right click the combo box image -> property->Data tab
to do Enter ListBox Items
Thanks
|
|
|
|
|
You need to size the control by clicking on its down-arrow and draging the control to the desired height.
"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
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
I got the combo box working. I can select an item and retrieve the value.
However, the combo box is empty by default if the user does not select an item.
How to set an default value if the user does not or forget to select a value?
Thanks
|
|
|
|
|
Use SetWindowText() or SetCurSel() .
"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
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Thanks. That works.
In the simple application I have two dialogue box. One is for input and the other for display results.
I use Edit box to display results of calculated numbers (float or int). Then each edit box have a tittle
above it by Static Text.
Ideally I can change the static text according to the input since this static text should show the unit of the
displayed numbers. The unit depends on the input selection of the units.
How to change the static text on fly by the application according to the input selection from the input combo box.
Thanks a lot
|
|
|
|
|
mrby123 wrote: How to change the static text on fly by the application...
Use SetWindowText() .
"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
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
m_pv_unit is declared in the header file of Class A: CString m_pv_unit; which is assigned value from Combo Box.
Class B is the for the output display. Class B includes the header file of Class A. However, I tried to use the variable: m_pv_unit to change the static text (the tittle of the output edit box), I got error message from the compile:
'm_pv_unit' : undeclared identifier
How to make: m_pv_unit to be see in Class B ? Or how to pass the variable: m_pv_unit and its value in Class A to methods in Class B. Class B (its methods) is called by Class A.
The method of Class B displays the results processed by Class A.
Thanks
modified on Friday, May 21, 2010 1:02 AM
|
|
|
|
|
mrby123 wrote: m_pv_unit is declared in the header file of Class A: CString m_pv_unit; which is assigned value from Combo Box.
I suggest using CComboBox object instead.
"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
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Have had an app running for years and now suddenly it is unable to delete files. It is processing the data in the files. The only step it seems unable to complete now is the file deletion.
I have checked the directory permissions and even recreated the directory. Has there been any recent Windows update that would make a call to CFile::Remove fail? Any other ideas what might be going on?
Thanx,
>>>-----> MikeO
modified on Tuesday, May 25, 2010 1:48 PM
|
|
|
|
|
Is it throwing an exception? If so which one?
|
|
|
|
|
No exception is being thrown. The program reads the contents of the file into memory, deletes the file, and then processes the data. If an exception were being thrown the data would not be processed. I can clearly see in the database though that it is.
The program has been running correctly about five years now. I am hoping somebody has an idea what I can check without having to modify the code.
Thanx,
>>>-----> MikeO
|
|
|
|
|
So you've got the call in a TRY/CATCH block?
|
|
|
|
|
The call is in a try/catch block. An exception should also write a message to the program log. The last entry in the log is "Program started".
Thanx,
>>>-----> MikeO
|
|
|
|
|
Mike Osbahr wrote: ...deletes the file...
So then what's the problem?
"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
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
I was explaining how I know that no exception is being thrown. If CFile::Remove were throwing an exception the data would not be processed.
The data is being processed but the files are not being removed. This causes the file processing loop to take longer each time and eventually the program fails.
I'm looking for clues why CFile::Remove might not be working and not throwing an exception. The program has been working for five years. This is why I asked about Windows updates.
Thanx,
>>>-----> MikeO
|
|
|
|
|
Mike Osbahr wrote: If CFile::Remove were throwing an exception the data would not be processed.
This implies that you are removing the file before processing it, correct?
Mike Osbahr wrote: I'm looking for clues why CFile::Remove might not be working...
See here.
"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
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Perhaps you should read my posts. The data in the file is read into memory before I attempt to delete the file. I am trying to get help diagnosing why a long running program would fail without modifying the code.
Since the read/delete/process occur in that order within the try/catch and the data is being processed, I know that CFile::Remove is not throwing an exception. Is there an error condition of DeleteFile() for which CFile::Remove would not throw an exception?
Thanx,
>>>-----> MikeO
|
|
|
|
|
Mike Osbahr wrote: Any other ideas what might be going on?
Since CFile::Remove() is just a thin wrapper around DeleteFile() , why not call DeleteFile() directly? If it fails, call GetLastError() to find out why? This is what Remove() is doing when it throws an exception.
"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
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Here is an hypothesis. It may seem far fetched, it has been proven correct many times already.
A file gets created. By your app or another one, not really relevant.
Pretty soon (very relevant) your app wants to read that file, no problem; wants to modify or delete that file, big problem.
What happens is this: something is also interested in your fresh file, opens it with read intentions, but doing so also locks it for writing and deleting.
Who does this? Any piece of code that wants to help you: an anti-virus package; an automatic indexer; Google Desktop; you name it.
Remedy 1: wait 1 minute. Literally. Let the helpers do their work, then remove the file.
Remedy 2: use a special folder, and put it on the exceptions list of the helper, if you can identify it.
Why the sudden change? did you add such helper? did one of them become slower? did the load of your system increase recently, making everything run a bit slower?
FYI: Windows Explorer is well aware of this phenomenon; if you give it a delete command, which fails, it will not tell you immediately; instead it will retry 5 times at 1 second intervals, and only if that still fails, it will tell you so. (And you can find a patch to modify that "5" in the registry, Google will provide the details).
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|