Kodewerx

Our culture has advanced beyond all that you could possibly comprehend with one hundred percent of your brain.
It is currently Thu Mar 28, 2024 3:37 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: I fail at C++
PostPosted: Mon Sep 17, 2007 10:49 am 
Offline
Komrade
Komrade

Joined: Tue Mar 27, 2007 10:18 am
Posts: 1328
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?

_________________
Image


Last edited by Hextator on Tue Sep 18, 2007 1:26 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: I fail at C++
PostPosted: Tue Sep 18, 2007 5:12 am 
$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.


Top
  
Reply with quote  
 Post subject:
PostPosted: Tue Sep 18, 2007 9:21 am 
Offline
Komrade
Komrade

Joined: Tue Mar 27, 2007 10:18 am
Posts: 1328
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! :D

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. @_@

_________________
Image


Last edited by Hextator on Tue Sep 18, 2007 1:24 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: I fail at C++
PostPosted: Tue Sep 18, 2007 10:45 am 
Have you verified that fread() is returning a value identical to count (in this case, 4)?


Top
  
Reply with quote  
 Post subject:
PostPosted: Tue Sep 18, 2007 1:21 pm 
Offline
Komrade
Komrade

Joined: Tue Mar 27, 2007 10:18 am
Posts: 1328
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 ^

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re:
PostPosted: Tue Sep 18, 2007 1:28 pm 
Offline
Komrade
Komrade
User avatar

Joined: Tue Mar 27, 2007 6:23 pm
Posts: 1354
Location: Mario Raceway, 1509.831, 217.198, -564.429
Title: Mario Kart 64 Hacker
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!

_________________
Image 143
HyperNova Software is now live (but may take a few tries to load) currently down; check out my PSP/DS/Game Boy/Windows/Linux homebrew, ROM hacks, and Gameshark codes!


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Sep 18, 2007 2:02 pm 
Offline
Komrade
Komrade

Joined: Tue Mar 27, 2007 10:18 am
Posts: 1328
I no longer fail (as hardly) at C++ :D

The program works great now.

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: I fail at C++
PostPosted: Tue Sep 18, 2007 5:43 pm 
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.


Top
  
Reply with quote  
 Post subject:
PostPosted: Tue Sep 18, 2007 7:33 pm 
Offline
Komrade
Komrade

Joined: Tue Mar 27, 2007 10:18 am
Posts: 1328
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.

_________________
Image


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: AhrefsBot [Bot] and 139 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group