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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:40:45+00:00 2026-05-30T17:40:45+00:00

I am trying to access all files in a directory and do some thing

  • 0

I am trying to access all files in a directory and do some thing on the files under this and the subsequent directories. For this operation I used dirent.h with windows and it manages to get all the files and open and close these files. My problem is, when i try to read some something from them and write to another, as shown below, i get the error shown at the end.

Here is the code:

#include <iostream>
#include <cstring>
#include <sys/stat.h>
#include <dirent.h>
FILE *test_file;
char buffer[51];
void listdir(const char *path) 
{
  struct dirent *entry;
  DIR *dp;

  //std::cout << "Dir: " << path << "\n";

  if(dp = opendir(path))
  {
    struct stat buf ;
    FILE *input_file;

    while((entry = readdir(dp)))
    {
        std::string p(path);
        p += "\\";
        p += entry->d_name;
        char fpath[250];
        //strcpy(fpath,path);
        if(!stat(p.c_str(), &buf))
        {
            if(S_ISREG(buf.st_mode))
            {
                std::cout << "    File: " << entry->d_name << "\n";
                sprintf(fpath,"%s\\%s",path,entry->d_name);
                input_file=fopen(fpath,"r+b");
                test_file=fopen("test_test.txt","a+b");
                if(input_file==NULL)
                {
                std::cout<<"\n Could not open\n"<<entry->d_name<<std::endl;
                continue;
                }
                if(test_file==NULL)
                    goto z;
                else 
                {
                    std::cout<<"\n Successfully Opened\n"<<fpath;
                    fread(buffer,50,1,input_file);
                    fprintf(test_file,"\n\n%s\n\n",fpath);
                    fwrite(buffer,50,1,test_file);

                    fclose(input_file);
                     fclose(test_file);
                    // free(buffer);
                }
z:
                if(test_file=NULL)
                fclose(test_file);
            }
            if(S_ISDIR(buf.st_mode) &&  
         // the following is to ensure we do not dive into directories "." and ".."
                      strcmp(entry->d_name, ".")  && strcmp(entry->d_name, "..") )
            {
                listdir(p.c_str());
            }
        }
        else
            std::cout << "ERROR in stat\n";
    }
   // delete buf;
    closedir(dp);
  }
  else
    std::cout << "ERROR in opendir\n";
  fclose(test_file);
}

int main(int argc, char **argv) 
{
  listdir(argv[1]);
  return 0;
}

It manages to open and read the first file but after the first file it will show the following error and open dbgheap.c

HEAP[direntdir.exe]: Invalid address specified to RtlValidateHeap(
002C0000, 002C5718 ) Windows has triggered a breakpoint in
direntdir.exe.

This may be due to a corruption of the heap, which indicates a bug in
direntdir.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while direntdir.exe has
focus.

The output window may have more diagnostic information.

EDIT:
corrected the errors with the buf variables.

Now i get

Debug Assertion Failed!

… Expression:(buffer!=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-05-30T17:40:46+00:00Added an answer on May 30, 2026 at 5:40 pm

    You have two variables named buf:

    char* buf;
    ...
    struct stat *buf = new struct stat;
    

    The latter hides the first and latter is free()d, even though it was created using new, and then reused without being reallocated.
    The latter is also used as an argument to fread(). Rename char* buf and possibly make it local to the function and just use a stack allocated buffer:

    char fread_buffer[51];
    

    EDIT:

    char* buffer never has memory allocated for it before it used in fread() so the call to fread() could be writing anywhere in memory. Change to:

    char buffer[50]; /* as only 50 bytes are read from the file */
    

    and don’t call free() on buffer if it is declared this way.

    Also, to simplify things just declare buf as:

    struct stat buf;
    

    and call stat():

    if(!stat(p.c_str(), &buf))
    

    Making these two changes will remove all dynamic memory management from the code.

    EDIT 2:

    This if is an assignment, not an inequality check:

    if(test_file=NULL)
        fclose(test_file);
    

    should be:

    if(NULL != test_file)
        fclose(test_file);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a subroutine in access 2003 that removes all quote characters
Im trying to access a Sql CE 2005 database on a windows mobile device
I'm trying to get a list of the CSV files in a directory with
I've been trying to get this code to work for hours! All I need
I'm looping through a directory in Java, trying to read each of the files.
I'm trying to set up a box to compile my project on windows. This
I am trying to filter the only.sql files from given Directory path But it
I'm trying to get some information from this tutorial: http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder basically I want to
Im trying to access a web service from a remote computer. I managed to
Im trying to access the Magento customer session in another part of my website.

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.