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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:10:26+00:00 2026-06-10T08:10:26+00:00

I am trying to constantly read data into a buffer of type unsigned char*

  • 0

I am trying to constantly read data into a buffer of type unsigned char* from different files. However, I can’t seem to set the buffer to NULL prior to reading in the next file.

Here is only the relevant code:

#include <stdio.h>
#include <fstream>


int
 main (int argc, char** argv) { 

     FILE* dataFile = fopen("C:\\File1.txt", "rb");
     unsigned char *buffer = NULL;
     buffer = (unsigned char*)malloc(1000); 
     fread(buffer,1,1000,dataFile);
     fclose(dataFile);

     dataFile = fopen("C:\\File2.txt", "rb");
     buffer = NULL;
     fread(buffer,1,1000,dataFile);
     fclose(dataFile);

     system("pause");
     return 0; 

}

The error I run into is at the second occurrence of this line: fread(buffer,1,1000,dataFile);

The error I get is:

Debug Assertion Failed!
Expression: (buffer != NULL)

It points me to Line 147 of fread.c which is basically:

/* validation */
_VALIDATE_RETURN((buffer != NULL), EINVAL, 0);
if (stream == NULL || num > (SIZE_MAX / elementSize))
{
    if (bufferSize != SIZE_MAX)
    {
        memset(buffer, _BUFFER_FILL_PATTERN, bufferSize);
    }

    _VALIDATE_RETURN((stream != NULL), EINVAL, 0);
    _VALIDATE_RETURN(num <= (SIZE_MAX / elementSize), EINVAL, 0);
}

I did Google for ways to get the buffer pointer to NULL and tried the various suggestions, but none seem to work. Anyone can clarify what is the right way to set it to NULL?

  • 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-10T08:10:28+00:00Added an answer on June 10, 2026 at 8:10 am

    Your buffer is a pointer.

    When you do this:

    buffer = (unsigned char*)malloc(1000); 
    

    you allocate some space in memory, and assign its starting position to buffer. Remember, buffer holds the address of the beginning of the space, that’s all. When you do this:

    buffer = NULL;
    

    you have thrown away that address.

    EDIT:

    C++ style, without dynamic memory:

    #include <fstream>    
    using std:: string;
    using std:: ifstream;
    
    void readFromFile(string fname)
    {
      char buffer[1000];
      ifstream fin(fname.c_str());
      fin.read(buffer, sizeof(buffer));
      // maybe do things with the data
    }
    
    int main ()
    {
      readFromFile("File1.txt");
      readFromFile("File2.txt");
      return 0;
    }
    

    There’s no need to erase the contents of the buffer. If the cost of allocating and deallocating the buffer with each call is too much, just add static:

      static char buffer[1000];
    

    It will be overwritten each time.

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

Sidebar

Related Questions

So I am trying to read sensor data from an IMU and update angles
I'm trying to accept a formatting constant from a data cell, so I have
I am trying to copy files from a directory that is in constant use
In my WCF service, when trying transfer large data I constantly get an error:
I'm trying to read bytes from the accessory's InputStream without blocking the thread (if
I am trying to read attribute values from themes and styles which were designed
I am trying to make an script that will every second read string from
My application has a logger which constantly logs data in a number of different
So I'm making this android application, that needs to read data from user-provided CSV
I am trying to automate the reading in of files generated from another analysis

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.