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 3351048
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:51:41+00:00 2026-05-18T01:51:41+00:00

I’ve been trying to debug my code for the past few hours and I

  • 0

I’ve been trying to debug my code for the past few hours and I couldn’t figure out the problem. I eventually set my filestream to throw exceptions on failbit and I found that my filestream was setting the failbit for some reason. I have absolutely no reason why the failbit is being set, because all I’m doing is writing 2048-byte chunks of data to the stream until suddenly it fails (at the same spot each time).

I would like to show you my code to see if anyone can see a problem and possibly see what might cause a std::ios::failure to be thrown:

bool abstractBlock::encryptBlockRC4(char* key)
{//Thic encryption method can be chunked :)
 getStream().seekg(0,std::ios::end);
 int sLen = int(getStream().tellg())-this->headerSize;
 seekg(0);//Seek to beginning of Data
 seekp(0);
 char* encryptionChunkBuffer = new char[2048]; //2KB chunk buffer
 for (int chunkIterator =0; chunkIterator<sLen; chunkIterator+=2048)
 {
  if (chunkIterator+2048<=sLen)
  {
   getStream().read(encryptionChunkBuffer,2048);
   char* encryptedData = EnDeCrypt(encryptionChunkBuffer,2048,key);
   getStream().write(encryptedData,2048);
   free(encryptedData);
  }else{
   int restLen = sLen-chunkIterator;
   getStream().read(encryptionChunkBuffer,restLen);
   char* encryptedData = EnDeCrypt(encryptionChunkBuffer,restLen,key);
   getStream().write(encryptedData,restLen);
   delete  encryptedData;
  }
 }
 delete [] encryptionChunkBuffer;
 dataFlags |= DATA_ENCRYPTED_RC4; // Set the "encryted (rc4)" bit
 seekp(0); //Seek tp beginning of Data
 seekg(0); //Seek tp beginning of Data
 return true;
}

The above code is essentially encrypting a file using 2048 chunks. It basically reads 2048 bytes, encrypts it and then writes it back to the stream (overwrites the “unencrypted” data that was previously there).
getStream() is simply returning the fstream handle to the file thats being operated on.

The error always occurs when chunkIterator==86116352 on the line getStream().write(encryptedData,2048);

I know my code may be hard to decode, but maybe you can tell me some possible things that might trigger a failbit? Currently, I think that the problem lies within the fact that I an reading/writing to a stream and it may be causing problems, but as I mentioned any ideas that can cause a failbit would maybe help me investigate the problem more.

  • 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-18T01:51:42+00:00Added an answer on May 18, 2026 at 1:51 am

    You must seekp between changing from reads to writes (and vice versa, using seekg). It appears your implementation allows you to avoid this some of the time. (You’re probably running into implicit flushes or other buffer manipulation which hide the problem sometimes.)

    C++03, §27.5.1p1, Stream buffer requirements

    The controlled sequences can impose limitations on how the program can read characters from a
    sequence, write characters to a sequence, put characters back into an input sequence, or alter the stream
    position.

    This just generally states these aspects are controlled by the specific stream buffer.

    C++03, §27.8.1.1p2, Class template basic_filebuf

    The restrictions on reading and writing a sequence controlled by an object of class
    basic_filebuf<charT,traits> are the same as for reading and writing with the Standard C library
    FILEs.

    fstream, ifstream, and ofstream use filebufs.

    C99, §7.19.5.3p6, The fopen function

    When a file is opened with update mode (‘+’ as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end-of-file.

    You may need to look up these calls to translate to iostreams terminology, but it is fairly straight-forward.


    You sometimes free the result of EnDeCrypt and sometimes delete it (but with single-object delete and not the array form); this most likely doesn’t contribute to the problem you see, but it’s either an error on your part or, less likely, on the part of the designer of EnDeCrypt.


    You’re using:

    char* encryptionChunkBuffer = new char[2048]; //2KB chunk buffer
    //...
    getStream().read(encryptionChunkBuffer,2048);
    //...
    delete[] encryptionChunkBuffer;
    

    But it would be better and easier to use:

    vector<char> encryptionChunkBuffer (2048); //2KB chunk buffer
    //...
    getStream().read(&encryptionChunkBuffer[0], encryptionChunkBuffer.size());
    //...
    // no delete
    

    If you don’t want to type encryptionChunkBuffer.size() twice, then use a local constant for it.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.