Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:41:55+00:00 2026-06-02T09:41:55+00:00

I am reusing some old code(originally developed on c, not c++) with some functions

  • 0

I am reusing some old code(originally developed on c, not c++) with some functions to open/read/manipulate text-files. The path to the text-files is passed to the functions as a string (char*) then opened using: FileToUse = fopen(filename, "rb"); then multiple calls to fread() and fseek() are used. This code is known to work for external text-files, but now I would like to include the textfiles as resources in my project (MFC C++ in visual studio).

I found some examples on the web on how to use resources rusulting in this code:

HINSTANCE hInst = AfxGetResourceHandle();
HRSRC hResource = FindResource(hInst, MAKEINTRESOURCE(IDR_TEXTFILE1), "TEXTFILE");

if (hResource){
  HGLOBAL hLoadedResource = LoadResource(hInst, hResource);
  if (hLoadedResource){
    const char* pResource = LockResource(hLoadedResource);
    if (pResource){
      DWORD dwResourceSize = SizeofResource(hInst, hResource);
      if (0 != dwResourceSize){                            // if(FileToUse){
        memcpy(&Buffer, pResource, (15 * 2));              //   fread($Buffer, 15, 2, FileToUse);
        pTemp = pResource + 200;                           //   fseek(FileToUse, 200, SEEK_SET);
        pTemp = pTemp + 100;                               //   fseek(FileToUse, 100, SEEK_CUR);
        pTemp = pResource + (dwResourceSize - 1) - 40;     //   fseek(FileToUse, -40, SEEK_END);
      }
    }
  }
}

I replaced the fread call by memcpy() as shown, but I’m missing the return value of fread (actual read items) and in the original code the filepointer was moved by fseek, I wonder whether my approach using a temporary pointer is correct.

My ultimate goal is to simulate the fread and fseek calls for resources with similar function prototypes:

size_t resread( void* buffer, size_t size, size_t count, char* resource );

int resseek( char* resource, long offset, int origin );

Any suggestions are much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:41:56+00:00Added an answer on June 2, 2026 at 9:41 am

    Thanks for your help, based on the Agent_L’s suggestion this is what I came up with:

    Text-resource type:

    struct _resource {
        const char * content;      // File content
        size_t size;               // File size
        size_t ptrloc;             // 'Pointer' location
    };
    typedef struct _resource RES_TXT;
    

    resread based on fread:

    size_t resread( void* buffer, size_t size, size_t count, RES_TXT * resource)
    {
        size_t actualCount = ( resource->size - resource->ptrloc ) / size;
        actualCount = min( count, actualCount );
        if ( actualCount <= 0 ) return 0;
        memcpy(buffer, (resource->_ptr + resource->ptrloc), (actualCount * size) );
        resource->ptrloc += (actualCount * size);
        return actualCount;
    }
    

    and to complete resseek based on fseek:

    int resseek( RES_TXT * resource, long offset, int origin ) {
        size_t nextloc;
        switch ( origin ) {
            case SEEK_SET: nextloc = 0;
                break;
            case SEEK_CUR: nextloc = resource->ptrloc;
                break;
            case SEEK_END: nextloc = resource->size;
                break;
            default: return -1;
        }
        nextloc += offset;
        if ( nextloc >= 0  && nextloc < resource->size )
            resource->ptrloc = nextloc;
        else
            return -1;
        return 0;
    }
    

    Any call to fseek and fread can now be replaced to use a resource instead of an external file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read that reusing UIWebViews is kind of a bad practice. Some code I
In the interest of reusing some existing code that was defined as an instance
Here's a wierd one. I'm reusing a code base that unfortunately must not be
I am reusing code used in a Windows forms application. I found some help
For a J2EE bean I am reusing code that was developed for a java
I am reusing some code that build UIButtons dynamically (without interface builder). I am
I am doing some C programming for school and I have found myself reusing
When reusing code (for example, a Util library you created), do you add its
I've tried two different methods of reusing code. I have a solution full of
I'm trying to avoid code like this when reusing the same ViewUserControl in ASP.NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.