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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:13:11+00:00 2026-05-27T05:13:11+00:00

I have encountered a strange issue while listing files in a subtree which are

  • 0

I have encountered a strange issue while listing files in a subtree which are only directories or regular files. The files are listed but the size of the files after the following program traverses them becomes zero. How do I prevent that? Is it because of the stat()?

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)
{
  int fd;
  struct list_rec t;
  struct stat buff;

  if(stat(fpath,&buff) == -1)
 printf("Error reading inode");

  /*the file will be listed if it is directory or if it is a regular file and can be   opened for writing*/
  if(S_ISDIR(buff.st_mode)==0)
  {
 //printf("\nIT IS NOT A DIRECTORY %s",fpath);
if(S_ISREG(buff.st_mode)!=0)
{
    //printf("IT IS A REGULAR FILE %s",fpath);
    fd = open  (fpath,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
    if(fd != -1)
    {
        close(fd);
        no_of_files = no_of_files+1;
        t.seqno = no_of_files;
        t.path_length = strlen(fpath)+1;
        t.file_path = malloc(sizeof(char)*t.path_length);
        strcpy(t.file_path,fpath);

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

        free(t.file_path);
    }
    else
        printf("\n is regular but could not open %s",fpath);
}
else
    printf("\nNot a regular file %s",fpath);    
 }
 else
 {
    no_of_files = no_of_files+1;
t.seqno = no_of_files;
t.path_length = strlen(fpath)+1;
t.file_path = malloc(sizeof(char)*t.path_length);
strcpy(t.file_path,fpath);

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

free(t.file_path);
  }
 return 0;
 }

 int main(int argc, char *argv[])
 {
   listoffiles = fopen("/home/juggler/files.txt","w+b");
   no_of_files = 0;
   ftw(argv[1],list_the_files,20);
   fwrite(&no_of_files,sizeof(int),1,listoffiles);//Writing the total number of    records at the end of the file
  }

Thanks. Help will be much appreciated

  • 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-27T05:13:12+00:00Added an answer on May 27, 2026 at 5:13 am

    You’re opening files with O_WRONLY and O_TRUNC, which will truncate the file to 0 length. You probably just want O_RDONLY.

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

Sidebar

Related Questions

I have encountered a strange behavior while executing an sql query on the Oracle
I have encountered a very strange issue. Whenever I use the .NET membership provider
I have been trying to output XML with PHP but encountered a strange(!) error
I have not programmed C++ for a while and encountered a strange behavior when
I have encountered strange issue with lists in F#. I am using XamlXmlReader to
I encountered a strange issue while coding a .dll file in C with gcc
I'm having a strange issue that I've never encountered before. I have a partial
I have encountered a strange issue. When I update one of my websites to
I have encountered strange issue. From Activity onStart() I request Bluetooth activation and 120s
I am having a strange issue that I have never encountered in the past

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.