Files become zero bytes after stat()

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

Answers


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


Need Your Help

Should we use async/await for downloading huge chunk of data

c# multithreading asynchronous

As per my understanding async/await will use ThreadPool thread for performing asynchronous operation and we prefer Threadpool thread when operation will be done within shorter span of time, so thre...

php session_start() warning

php session warnings

I'm new to php.. Im trying to get value from session. Below is my code.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.