Kodewerx https://www.kodewerx.org/forum/ |
|
I fail at C++ https://www.kodewerx.org/forum/viewtopic.php?f=5&t=4278 |
Page 1 of 1 |
Author: | Hextator [ Mon Sep 17, 2007 10:49 am ] |
Post subject: | I fail at C++ |
Code: [Removed (outdated)] I fixed a lot of the problems I was having between the time of posting this and the time of this edit. However, I'm not reaching that last cout. I'm wondering if I mishandled failed reads of the input file...it should have exited or printed "Done." and it did neither. Edit: Well, here's what happens now. Enter name of file to search through: fail.dmp Enter bit mask as hex: 01000000 Enter hex pointer to search for: 01000000 Searching through file... Mask = 16777216 PointerToGet = 16777216 FileName = fail.dmp Then it freezes at the while loop instead of executing it. What the hell? Edit: Apparently FileSize is "-1". Any idea why that happened? If "NULL" means it expected a 0 value, then getting -1 would explain why both the exception handler and the while loop weren't executed. But if the while loop can't be executed, why doesn't it just cout "Done." and end the program like it's supposed to? Edit: Now the file size is 4. But the while still isn't executing! What the hell is going on? |
Author: | dlong [ Tue Sep 18, 2007 5:12 am ] |
Post subject: | Re: I fail at C++ |
$5 says Parasyte makes a post on the evils of C++. PS: ftell() returns -1 if there's an error in the filestream. Try using perror() after the ftell()'s? In fact, check everything for failure. fopen(), ftell(), fread(), etc. It's good practice. |
Author: | Hextator [ Tue Sep 18, 2007 9:21 am ] |
Post subject: | |
dlong wrote: PS: ftell() returns -1 if there's an error in the filestream. It was fucking up because I was using "set equal to" (=) instead of "if equal to" (==) in my if statements (in a previous version, anyway). I fixed that, then ditched the ftell by using if (!feof(ReadCursor)) instead (compliments to SubDrag for pointing that one out). The while statement still isn't being executed though...I watched all the variables, and they look right. It's even opening the correct file (when I was still using the ftell, it was returning 4, which is indeed the file size). So the file must be opening correctly. Maybe this beta version of Dev C++ doesn't compile while statements right? That sounds very unlikely. Still, it just STOPS when it gets there. It doesn't continue on if the conditions aren't meant or execute if they are. It just craps out. I wanted to try using a label/goto combo (I hear that's an ugly way of doing anything in C++) just to see if using an if instead of a while would make it work, but I don't know how to do that. Here's what I have now, by the way: Code: [Removed (outdated)] Edit: I tried it with a label (figures how to use labels would be so obvious) and an if, and it's actually executing now! ![]() But it doesn't ever reach the end of the file. SubDrag said fread increments the read pointer. I think I read on http://www.cplusplus.com that it does, too. But if it's incrementing, why isn't the end of file flag being set? It just keeps going and going... I think I should cout an ftell and see wtf is going on. Edit: It's outputting now. I just need to format the output width for the first two output variables to always be 8 digits and find a way to make the program terminate. It is indeed incrementing the variable...I think what it's doing is returning to the beginning of the file if it reads past the end. That seems ridiculous. It should still set the eof flag if it does that. @_@ |
Author: | dlong [ Tue Sep 18, 2007 10:45 am ] |
Post subject: | Re: I fail at C++ |
Have you verified that fread() is returning a value identical to count (in this case, 4)? |
Author: | Hextator [ Tue Sep 18, 2007 1:21 pm ] |
Post subject: | |
fread returns the value at the address of the file being read, which, in my test file, is 0x1000000 in little endian. I have it out putting now. It prints 0 1000000 0 if the input is "fail.dmp 1000000 1000000" where fail.dmp is a 4 byte file with the value 0x1000000 in it in little endian. The problem is, it starts printing 4 1000000 0 infinitely more afterward. The loop isn't terminating! Ugh. Code: #include <stdio.h> #include <iostream> using namespace std; char FileName[255]; unsigned long Mask; unsigned long PointerToGet; unsigned long PseudoPointer; unsigned long TestVal; unsigned long LoadedVal; unsigned long ReadCursorVal = 0; unsigned long FileSize; signed long Offset; int main () { cout << "Enter name of file to search through: "; cin >> FileName; cout << "Enter bit mask as hex: "; cin>>hex>> Mask; cout << "Enter hex pointer to search for: "; cin>>hex>> PointerToGet; PseudoPointer = PointerToGet & Mask; FILE * WriteCursor; WriteCursor = fopen ("Search Results.txt", "w"); if (WriteCursor == NULL) { cout << "Writing error\n"; system ("pause"); exit (1); } FILE * ReadCursor; ReadCursor = fopen (FileName, "rb"); if (ReadCursor == NULL) { cout << "Reading error\n"; system ("pause"); exit (2); } fseek (ReadCursor, 0, SEEK_END); FileSize = ftell (ReadCursor); fseek (ReadCursor, 0, SEEK_SET); cout << "Searching through file...\n"; cout << "Mask = "<<Mask; cout << "\nPointerToGet = "<<PointerToGet; cout << "\nFileName = "<<FileName; Fail: if (ReadCursorVal < FileSize); { ReadCursorVal = ftell (ReadCursor); fread (&LoadedVal, 1, 4, ReadCursor); TestVal = LoadedVal & Mask; if (TestVal == PseudoPointer) { Offset = PointerToGet - LoadedVal; fprintf (WriteCursor, "%X ", ReadCursorVal); fprintf (WriteCursor, "%X ", LoadedVal); fprintf (WriteCursor, "%d\n", Offset); } goto Fail; } fclose (ReadCursor); fclose (WriteCursor); cout << "Done."; } Latest ^ |
Author: | HyperHacker [ Tue Sep 18, 2007 1:28 pm ] |
Post subject: | Re: |
Eww @ cin/cout. Anyway... Zeld wrote: if (WriteCursor == NULL) { cout << "Writing error"; exit (1); //Why is this on one line when you have braces? >_> } Quote: while (!feof(ReadCursor)); //Infinite loop! Watch those semicolons. (Code tags omitted because they filter bbcode too. <_<)[edit] I see a goto in there. WTF? Gotos are evil! |
Author: | Hextator [ Tue Sep 18, 2007 2:02 pm ] |
Post subject: | |
I no longer fail (as hardly) at C++ ![]() The program works great now. |
Author: | dlong [ Tue Sep 18, 2007 5:43 pm ] |
Post subject: | Re: I fail at C++ |
Eww. I'm not used to looking for those mistakes ('=' vs. '==', missing braces, bad semi-colon) because I don't make them. You have bad habits apparently. |
Author: | Hextator [ Tue Sep 18, 2007 7:33 pm ] |
Post subject: | |
Aren't habits supposed to be...habitual? It was my first program. I haven't done any of this stuff enough for it to be considered a habit. |
Page 1 of 1 | All times are UTC - 8 hours [ DST ] |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |