"main.exe" Has Crashed Error - c++

I am having a simple error. The project is to create a skeleton of a basic menu. The new command just counts from 1 to an integer entered by the user. Before I continue, I have posted a link to the pastebin that holds my .cpp file:
http://pastebin.com/pAi9EiEi
The rest of the program runs and works. However, the error is simple. It is crashing as soon as I type in any of the commands. After running error checks, I have found the error is not the while but the if statements. The error is on the lines similar to this:
if (stricmp(strstr(newCommand, cmd2), newCommand) == 0)
What this line is supposed to do, is copy what is in cmd2 and put it in newCommand then comparing it without caps sensitivity to:
char newCommand[] = "new";

"What this line is supposed to do, is copy what is in cmd2 and put it in newCommand then comparing it without caps sensitivity to:"
If the code did this, it would be comparing the former contents of cmd2 to itself, wouldn't it?
copy cmd2 -> newCommand
is cmd2 == newCommand?
If the user typed "open", then on the first iteration of your loop it would copy "open" into "newCommand" and you would no-longer have the string "new" anywhere in your program.
strstr searches the first string for an occurrence of the second.
strstr("hello world", "world"); // returns pointer to 'world' in 'hello world'
strstr("biscuit", "new"); // returns NULL to indicate new doesn't occur in 'biscuit'
Surely what you actually want to do is simply:
if (stricmp(newCommand, cmd2) == 0) {
// match
}
Incidentally, if you did want to copy "cmd2" into "newCommand", you would want to use strcpy(destinationStr, sourceStr);
strcpy(newCommand, cmd2);

that line doesn't do what you think it does
http://www.cplusplus.com/reference/cstring/strstr/
it searches for the occurence of cmd2 in newcommand and returns a pointer to it (null if not found) and unless I'm mistaken passing a null in stricmp is undefined behavior
you just want a plain stricmp and you do away with the copy

Related

running multiple execve functions in one c++ file

I have to write a c++ program which "counts the number of lines, words and the number of bytes from a text file", all of which must be in a new line.
I have to use the wc command in my c++ program. I have managed to get the number of lines:
char *envp[] = {NULL};
char *command[] = {"wc", "-l", filename.c_str(), NULL};
execve("/usr/bin/wc", command, envp);
After the above statements, I have one which replaces "-l" with "-w" and so forth. But my program ends immediately after the first execve() statement.
How do I get all my statements to execute even after the execve() statement?
N.B: This will be my first time running system commands using a c++ program.
Thank you in advance.
execve replaces current executable image with a specified one and therefore never returns upon success. If you want to continue executing main program then you will need to fork first. Or use something as dull as system function.

Compiled c++ output file displays random character at end of program?

Not sure if this is an appropriate question, but just recently I've noticed that when I run a C++ program in the terminal when it exits it has a % sign after the last output. For example a hello world program says "hello world%". What is this and how do I get rid of it? I'm on OS X, shell is zsh. Unless I am crazy it has never done this until now.
There are two possibilities that I can think of off hand:
1) You aren't printing a carriage return, so the % prompt appears at the end of the printed text instead of on the next line. (Is the % your standard prompt in the shell?)
2) You are printing past the end of a buffer and getting a random character as a result.
I'd guess #1 based on what you describe, but both could cause the behavior.

MFC C++ Always returning EOF when I have access to file

I am currently stuck in this problem that I do not have any idea to fix. It is regarding a previous question that I have asked here before. But I will reiterate again as I found out the problem but have no idea to fix it.
My program accesses a text file that is updated constantly every millisecond 24/7. It grabs the data line by line and does comparison on each of the line. If any thing is "amiss"(defined by me), then I log that data into a .csv file. This program can be run at timed intervals(user defined).
My problem is that this program works perfectly fine on my computer but yet it doesnt on my clients computer. I have debug the program and these are my findings. Below is my code that I have reduced as much possible to ease the explanation process
int result;
char ReadLogLine[100000] = "";
FILE *readLOG_fp;
CString LogPathName;
LogPathName = Source_Folder + "\\serco.log"; //Source_Folder is found in a .ini file. Value is C:\\phython25\\xyratex\\serco_logs
readLOG_fp = fopen(LogPathName, "r+t");
while ((result = fscanf(readLOG_fp, "%[^\n]\n", ReadLogLine)) != EOF) // Loops through the file till it reaches the end of file
{
Sort_Array(); // Here is a function to sort the different lines that I grabbed from the text file
Comp_State(); // I compare the lines here and store them into an array to be printed out
}
fclose(readLOG_fp);
GenerateCSV(); // This is my function to generate the csv and print everything out
In Sort_Array(), I sort the lines that I grab from the text file as they could be of different nature. For example,
CStringArray LineType_A, LineType_B, LineTypeC, LineTypeD;
if (ReadLogLine == "Example line a")
{
LineType_A.add(ReadLogLine);
}
else if (ReadLogLine == "Example line b")
{
LineType_B.add(ReadLogLine);
}
and so on.
In CompState(), I compare the different values within each LineType array to see if there are any difference. If it is different, then I store them into a seperate array to print. A simple example would be.
CStringArray PrintCSV_Array;
for (int i = 0; i <= LineType_A.GetUpperBound(); i++)
{
if (LineType_A.GetAt(0) == LineType_A.GetAt(1))
{
LineType_A.RemoveAt(0);
}
else
{
LineType_A.RemoveAt(0);
PrintCSV_Array.Add(LineType_A.GetAt(0);
}
}
This way I dont have an infinite amount of data in the array.
Now to the GenerateCSV function, it is just a normal function where I create a .csv file and print whatever I have in the PrintCSV_Array.
Now to the problem. In my client's computer, it seems to not print anything out to the CSV. I debugged the program and found out that it keeps failing here.
while ((result = fscanf(readLOG_fp, "%[^\n]\n", ReadLogLine)) != EOF) // Loops through the file till it reaches the end of file
{
Sort_Array(); // Here is a function to sort the different lines that I grabbed from the text file
Comp_State(); // I compare the lines here and store them into an array to be printed out
}
fclose(readLOG_fp);
It goes into the while loop fine as I did some error checking there in the actual program. The moment it goes into the while loop it breaks out of it suggesting to me it reach EOF for some reason. When that happens, the program has no chance to go into both the Sort_Array and Comp_State functions thus giving me a blank PrintCSV_Array and nothing to print out.
Things that I have checked is that
I definitely have access to the text file.
My thoughts were because the text file is updated every
millisecond, it may have been opened by the other program to write
into it and thus not giving me access OR the text file is always in
an fopen state therefore not saving any data in for me to read. I
tested this out and the program has value added in as I see the KB's
adding up in front of my eyes.
I tried to copy the text file and paste it somewhere else for my
program to read, this way I definitely have full access to it and
once I am done with it, Ill delete it. This gave me nothing to print
aswell.
Am I right to deduce that it is always giving me EOF thus this is
having problems.
while ((result = fscanf(readLOG_fp, "%[^\n]\n", ReadLogLine)) != EOF) // Loops through the file till it reaches the end of file
If yes, How do I fix this?? What other ways can I make it read every line. I have seriously exhausted all my ideas on this problem and need some help in this.
Thanks everyone for your help.
Error is very obvious ... you might have over looked it..
You forgot to open the file.
FILE *readLOG_fp;
CString LogPathName;
LogPathName = Source_Folder + "\\serco.log";
readLOG_fp = fopen(LogPathName.GetBuffer());
if(readLOG_fp==NULL)
cout<<"Error: opening file\n";

How to put a conditional breakpoint to test if a CString variable is empty

So I have this simple code snippet:
CString str;
..................
if ( str.IsEmpty() )
str = spRelease->GetID();
I want to put a conditional breakpoint on the last line to test if str is empty.
I tried this first:
str == ""
But I get this:
Error overloaded operator not found
Then this:
str.isEmpty() == 0
And get this:
Symbol isEMpty() not found
Any idee how this could be done ? Any workaround ?
Thanks.
Why don't you just put a normal breakpoint on the last line? You already know str is empty. If you want to double check whether your string is empty, I would use an ASSERT instead.
If you really have to check your string, you have to check m_pszData in your CString, so your condition looks like this:
str.m_pszData[0] == '\0'
In Visual Studio 6 you have the operation IsEmpty(), note that the first 'I' is uppercase. You also have the Compare() operation. Which version of VS are you using?
One pattern that I've seen for things like this is to add a bit of code like this:
if (some_condition) {
int breakpoint=rand();
}
This generates a warning about breakpoint being initialized but not used so it is easy to remember to take it back out. This also allows you test any condition you want, including invoking functions or anything else, without having to worry about restructions of the debugger. This also avoids the limit on the number of conditional breakpoints you can have that some debuggers have.
The obvious downsides are that you can't add one during a debug session, recompiling, remembering to take them out, etc.

HTTPResponse(msg) Overwrite!

One of my functions returns a 'msg' object... which is merely a string.
I got into 2 for loops in the function.
msg=''
for e in example:
msg+= "some crap"
msg+= "some crap1"
for sl in somelist
msg+= v.somevalue
msg+="-------------"
return httpresponse(msg)
There's an example of the code.
'somelist' contains two values... when the 'msg' returns it only returns the second of the two values! I'm rather confused.
Your code uses sl as the loop variable, then pulls values from v. I'm not sure how they relate. If the final message includes a number of copies of the last value, then probably you forgot to relate sl and v somehow. If it includes only a single copy of the last value, then perhaps the line of code appending to msg is actually outside the loop. This would mean nothing is appended as the loop progresses, then once it exits, the last value is appended.
If your code is exactly like that, it should work just like you want it to. However, as this clearly isn't the actual code, I'd guess you have msg = ... somewhere, when you should have msg += ... At least that's the most likely reason for the behaviour you're seeing.
If you have trouble finding where it goes wrong, put in there some "print msg" statements and test it by running your Django project in development server. You'll see where it goes wrong.