Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Using fread for addresses doesn't work for some reason

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
horsedeg
Newbie cheater
Reputation: 0

Joined: 26 Jun 2017
Posts: 24

PostPosted: Sat Sep 09, 2017 12:51 am    Post subject: Using fread for addresses doesn't work for some reason Reply with quote

I'm modifying atom0s's basic scanner to take inputs as well as do "next scans" after the initial scan, and store the addresses in a file. Problem is that fread doesn't convert for some reason at all.

I did this on a small scale and it worked fine. Here's my code on the smaller scale. It works 100% fine. https://pastebin.com/7i5kEY0F

Now here's the code for the scanner (https://pastebin.com/qQV3WG1a), but in this code it just does the initial scan, puts them all into a file ("addr.bin"), and then afterwards pulls them out, puts them into an array with a large size (relative to what I was scanning, which returned around 10k results) to ensure it doesn't pass the end. However, for some reason, when I output what I put back into the array from the file, every single address is "cccccccc" which obviously doesn't match what was put in. I look at the file and it's a bunch of random unicode, which makes sense, so it can't be that.

I checked to make sure there wasn't an error using ferror and there isn't.

Is this an issue with Unicode or something?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 341

Joined: 09 May 2003
Posts: 19983
Location: The netherlands

PostPosted: Sat Sep 09, 2017 2:09 am    Post subject: Reply with quote

in debugmode uninitialized memory is initialized to cccccccc. (int 3, breakpoint)

this means that your read operation did not read a single byte.
check the return value and getlasterror to figure out why.

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Back to top
View user's profile Send private message MSN Messenger
horsedeg
Newbie cheater
Reputation: 0

Joined: 26 Jun 2017
Posts: 24

PostPosted: Sat Sep 09, 2017 2:40 pm    Post subject: Reply with quote

Both the program and GetLastError return 0, so I have no idea. Also when I change "Debug" to "Release" in Visual Studio, instead of cccccccc the addresses show 0.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 137

Joined: 25 Jan 2006
Posts: 7256
Location: 127.0.0.1

PostPosted: Sat Sep 09, 2017 11:31 pm    Post subject: Reply with quote

Some issues with your code:

Code:
void flushArrayToFile(FILE * file, uint32_t arr[]) {
    fwrite(arr, sizeof(uint32_t), sizeof(arr) / 4, file);
}


You cannot use sizeof like this on the arr variable. It will always return 4 for this. You will need to pass the true size of the array to the function as well to properly write the data.

Code:
const int getFileSize(FILE * file) {
   fseek(file, 0, SEEK_END);
   return ftell(file);
}


Keep in mind, fseek sets the current file pointer. If you attempt to read or write to that file after calling this function it will be set to the end of the file. Attempting to read will land up failing since there is nothing to read after the current end of the file. You should restore the file pointer to the beginning of the file before you return in this function, or, you should store the original pointer, then get the size, then restore the original file pointer and return the size depending on how you plan to use the function in the future.

Code:
void showMbiInfo(MEMORY_BASIC_INFORMATION mbi) {
   cout << hex << "Base Address: " << mbi.BaseAddress << endl;
   cout << "Region Size: " << mbi.RegionSize << endl;
}


Not an error or major issue but keep in mind using std::hex like you are here sets that formatting for the stream for any output/input onward. So region size will also output as hex. You should reset the stream back to its previous formatting after editing it if you want to see the size in decimal format.

Code:
   file = fopen("addr.bin", "rb");
   uint32_t buffer[50000];
   fread(buffer, sizeof(uint32_t), getFileSize(file) / 4, file);
   for (int i = 0; i < foundTotal; i++) {
      cout << "Read from file: " << buffer[i] << endl;
   }


Rather than assume 50k entries, you should obtain the size of the file and create the array dynamically. (Or use a vector or other stl container.) If you keep making huge arrays like that on the stack you are going to land up causing your app to just crash.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites