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

  • Home
  • SEARCH
  • 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 6681717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:37:18+00:00 2026-05-26T04:37:18+00:00

bool CReadWrite::write(unsigned long long offset, void* pvsrc, unsigned long long nbytes) { int m_WriteResult;

  • 0
bool CReadWrite::write(unsigned long long offset, void* pvsrc, unsigned long long nbytes)
{   int m_WriteResult;

    pFile = fopen("E:\\myfile.bin","wb");

    if (!pFile){   
        puts("Can't open file"); 
        return false;
    }

    fseek(pFile,SIZE_OF_FILE-1,SEEK_SET);
    fwrite(pvsrc,1,1,pFile);
    fseek(pFile,offset,SEEK_SET);

printf("fseek(pFile,SIZE_OF_FILE-1,SEEK_SET); returned -- ", fseek(pFile,SIZE_OF_FILE-1,SEEK_SET));

printf( "fwrite(pvsrc,1,1,pFile); returned -- %d", fwrite(pvsrc,1,1,pFile)); 


    printf("Current file pointer position is : %d\n",ftell(pFile)); 
    m_WriteResult = fwrite (pvsrc, 1, nbytes, pFile);

    if (m_WriteResult == nbytes){   
        puts("Wrote to file");
        printf("The number of bytes written to the file is : %d\n\n",m_WriteResult);
        fclose(pFile);
        return true;
    }
    else{   
        puts("Unable to write to File."); 
    fclose(pFile);
    return false;
    }   
}

main.cpp:

char* pWriteBuffer;
char* pReadBuffer;  
int nbytes = 85;
int nbytes2= 10;

pWriteBuffer  = new char [nbytes];
pReadBuffer = new char [nbytes];

CReadWrite test;


for(Bufferndx = 0; Bufferndx<nbytes; Bufferndx++){
     pWriteBuffer[Bufferndx] = Bufferndx; 
}
test.write(20,pWriteBuffer,50);

for(Bufferndx = 10; Bufferndx;Bufferndx--){
    pWriteBuffer[Bufferndx] = Bufferndx;
}
test.write(30,pWriteBuffer,nbytes2);

test.read(5,pReadBuffer,85);

delete[] pWriteBuffer;
delete[] pReadBuffer;

This is a part of my program that writes to a given buffer. The write function is given an offset, a source and how many bytes to write.

It first checks to see if the file was even able to open, fseeks to my desired file size -1, then writes a byte. It then fseeks in the file to the offset given. If it successfully wrote nbytes, it prints out some stuff, then closes the file. Else it prints out that it was unable to write and closes the file.

In my main, I’m just testing that my code actually wrote what I wanted to and I’ve got two for loops that have two writes after. I’m also doing a test.read after this but I am not sure why I’m not able to see the correct results I think I’m supposed to get in the debug mode when compiling.

Just FYI, The read class function is almost identical to the write but of course fread.

My question is: Why am I not getting the results when I step through using the debug mode and why am I not able to see the buffer be filled correctly. It’s “almost” like my writes/read is not really happening.

EDIT: What I’m “trying to do” is fill a buffer with 85 bytes (my biggest buffer).

  • My test.write(20,pWriteBuffer,50) will start from offset 20 and write 50 bytes, aka. offset 20 -70.
  • My second write test.write(30,pWriteBuffer,nbytes2) will start at offset 30 and write 10 bytes, aka. offset 20-30.
  • And 3rd, I read the whole thing and I should be able to tell where all the writes occur.

Or that is the plan, but I do not see that when I debug. Also, Maybe someone can shed some light on this but I’m reviewing over it and it looks like I’m…

fseek(pFile,SIZE_OF_FILE-1,SEEK_SET);
fwrite(pvsrc,1,1,pFile);

each time I write which is wrong… I shouldn’t be seeking out and making the file bigger each time I write. Ugh

printf( “fwrite(pvsrc,1,1,pFile); returned — %d”, fwrite(pvsrc,1,1,pFile)); //prints out 1

printf(“fseek(pFile,SIZE_OF_FILE-1,SEEK_SET); returned — “, fseek(pFile,SIZE_OF_FILE-1,SEEK_SET)); //prints out 0

  • 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-05-26T04:37:19+00:00Added an answer on May 26, 2026 at 4:37 am

    Here are the problems I see:

    1. You fopen the file with “wb”, which would truncate the file if existing or create a new one if not existing. So how do you expect to seek to 20th byte?
    2. Where is SIZE_OF_FILE set or more importantly is it set correctly?

    Suggestions: Never forget to check the return codes of all functions you’re calling. Print them out. Like you did in this: if (!pFile){ /*...*/}

    E.g.

    fseek(pFile,SIZE_OF_FILE-1,SEEK_SET);
    fwrite(pvsrc,1,1,pFile);
    fseek(pFile,offset,SEEK_SET);
    // should be
    std::cout << "fseek(pFile,SIZE_OF_FILE-1,SEEK_SET); returned -- " << fseek(pFile,SIZE_OF_FILE-1,SEEK_SET) << std::endl;
    printf( "fwrite(pvsrc,1,1,pFile); returned -- %d", fwrite(pvsrc,1,1,pFile));
    int retCode;
    if((retCode = fseek(pFile,offset,SEEK_SET)) != 0)
        std::cout << "fseek(pFile,offset,SEEK_SET) returned non-zero code -- " << retCode << std::endl;
    

    Don’t get confused, I’ve just shown 3 different ways of printing out debug info for 3 different calls. YOu can pick any one way (preferably 3rd one) and use everywhere.

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

Sidebar

Related Questions

bool CReadWrite::write(unsigned long long offset, void* pvsrc, unsigned long long nbytes) { int WriteResult;
bool is_something_ok(int param,SomeStruct* p) { bool is_ok = false; // check if is_ok if(is_ok)
bool IsTypeAGenericList(Type listType) { typeof(IList<>).IsAssignableFrom(listType.GetGenericTypeDefinition()) } returns false when given typeof(List<int>) . I assume
bool pred(int k, int l, int num1, int num2) { return (num1 < num2);
// Example bool is true bool t = true; // Convert bool to int
bool fn() { if(something bad happen) return false; .. } void gn() { assert(something
bool stop = false; int f1 = 1; int f2 = 2; int f3
bool binsearch(string phrase, vector<string> words, int from, int to, int &test) { while (tf
bool Win64bit = (sizeof(int*) == 8) ? 1 : 0; I need this so
bool isValid = false; string username = someadmin; If( !String.IsNullOrEmpty(username) && !( username.IndexOf(admin) !=

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.