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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:25:53+00:00 2026-05-25T11:25:53+00:00

I have a program that walks the file system hierarchy listing the file path

  • 0

I have a program that walks the file system hierarchy listing the file path names on a binary file as follows:

struct list_rec{
    int seqno;
    int path_length;
    char *file_path;
};
FILE *listoffiles;
int no_of_files;/*couter to keep track of files visited*/
static int list_the_files(const char *fpath,const struct stat *sb, int tflag, struct   FTW *ftwbuf)
{
struct list_rec t;
 no_of_files = no_of_files+1;
 t.seqno = no_of_files;
 t.path_length = strlen(fpath);
 t.file_path = malloc((sizeof(char)*t.path_length)+1);
 strcpy(t.file_path,fpath);

 fwrite(&t.seqno,sizeof(int),1,listoffiles);
 fwrite(&t.path_length,sizeof(int),1,listoffiles);
 fwrite(t.file_path,t.path_length,1,listoffiles);

 free(t.file_path);

 return 0;
}

int main(int argc, char *argv[])
{
 listoffiles = fopen("/home/juggler/Examples/Traces/files.txt","r+b");
 no_of_files = 0;
 ftw(argv[1],list_the_files,20);
}

and then read the file to check from a different program in the following way:

struct list_rec{
    int seqno;
    int path_length;
    char *file_path;
};
FILE *listoffiles;
int main()
{
    struct list_rec t;

    listoffiles = fopen("/home/juggler/Examples/Traces/files.txt","rb");
    if(!listoffiles)
    {
            printf("Unable to open file");
            return 1;
    }



    while(fread(&t.seqno,sizeof(int),1,listoffiles)!=0)
    {
    printf("\n %d  ",t.seqno);/*Log Record Number*/
    fread(&t.path_length,sizeof(int),1,listoffiles);
            printf(" %d",t.path_length);
    t.file_path = malloc(sizeof(char)*t.path_length);
            fread(t.file_path,t.path_length,1,listoffiles);
            printf(" %s",t.file_path);

    fflush(stdout);
    }
}

My problem is the output on running the second program shows some unwanted characters appended to some of the filepaths as follows:check record 51611 and 51617

51611   92 /media/New Volume/ March2010/June_2009/new latex_12 may 2009/document6_new.pdf��
51612   99 /media/New Volume/ March2010/June_2009/new latex_12 may 2009/proof with graph.tex.sav
51613   115 /media/New Volume/ March2010/June_2009/new latex_12 may 2009/Operations beyond read and write.tex.bak
51614   107 /media/New Volume/ March2010/June_2009/new latex_12 may 2009/singe version implementation.blg
51615   93 /media/New Volume/ March2010/June_2009/new latex_12 may 2009/formalization1.pdf
51616   92 /media/New Volume/ March2010/June_2009/new latex_12 may 2009/document6_new.texÉ… 
51617   95 /media/New Volume/ March2010/June_2009/new latex_12 may 2009/cascading undone.tex
  • 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-25T11:25:53+00:00Added an answer on May 25, 2026 at 11:25 am

    You’re not null-terminating the file paths when you read them back in.

    You should be allocating one more char’s worth than the string length, and setting that last char to 0 (fread won’t do that for you).

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

Sidebar

Related Questions

I have an autoupdater C# program. It will download a rar file that holds
I have program that has a variable that should never change. However, somehow, it
I have program that runs fast enough. I want to see the number of
I have a program that spits out both standard error and standard out, and
I have a program that creates a Windows user account using the NetUserAdd() API
I have a program that uses the mt19937 random number generator from boost::random. I
I have a program that spits out an Excel workbook in Excel 2003 XML
I have a program that monitors debug messages and I have tried using a
I have a program that will calculate the minimal area taken by fitting rectangles
I have a program that runs osql.exe from microsoft sql server tools directory and

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.