File attribute modification program not working

This is a discussion on File attribute modification program not working within the C Programming forums, part of the General Programming Boards category; Hy everyone! I'm new here (that's my first post) and I would like you to excuse my English but I'm ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    6

    File attribute modification program not working

    Hy everyone!
    I'm new here (that's my first post) and I would like you to excuse my English but I'm French.

    As a project for my university class, I've to write a dropbox-like program in C.

    I would like to modify attributes like modification/creation dates.
    The function is correctly working as if I type in "ls -al", the timestamp is correct. But when using my program to read these attributes, it returns the "real" modification/creation date.

    Here is the function that shows the timestamp :
    Code:
    time_t t = sb.st_mtime; struct tm tm = *localtime (&t); 
    char s[32]; 
    strftime (s, sizeof s, "%d/%m/%Y %H:%M:%S", &tm); 
    printf ("%-14s %s", lecture->d_name, s);
    And here is the code for modifying the timestamps :
    Code:
    void modifyAttributes(char * file, int mtime, int atime)
    {
        struct utimbuf ubuf;
        ubuf.modtime = mtime;
        ubuf.actime = atime;
        utime(file, &ubuf);
    }
    Can you help me on this issue?

    Thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    After modifying the timestamp, do you then do closedir() and opendir() to get a fresh copy of the file attributes (as stored on the file system)?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    I don't understand. utime is called using only the path of the file. Do I have to do it a different way?
    I mean, my program should be the "modifyAttributes" function as the main function. I don"t use opendir() or closedir() for that part.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > time_t t = sb.st_mtime; struct tm tm = *localtime (&t);
    > char s[32];
    > strftime (s, sizeof s, "%d/%m/%Y %H:%M:%S", &tm);
    > printf ("%-14s %s", lecture->d_name, s);
    Who sets up lecture->d_name?

    This comes from opendir / readdir.

    If you don't closedir/opendir, then my guess is stale data.

    Post a whole program we can run, not a handful of random lines from your own guesswork.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    Yes sorry.

    Here is the whole function. address="../" in my example :
    Code:
    void createLog(char * address){
    	FILE *fp;
    	
    	struct stat sb;
    	
      
        struct dirent *lecture;
    	
    	if(stat(address, &sb) == -1)
    	{
    		perror("there is no such directory");
    		exit(EXIT_FAILURE);
    	}
    	
    	
    	printf("Last status change:       %s", ctime(&sb.st_ctime));
        printf("Last file access:         %s", ctime(&sb.st_atime));
        printf("Last file modification:   %s", ctime(&sb.st_mtime));
        
        fp = fopen("server.log", "w+");
        
        // take the address and open directory
    	DIR *rep = opendir (address); 
    	
    	while ((lecture = readdir (rep))) 
          	{ 
     
             stat (lecture->d_name, &sb); 
    
    
    
    
    		 if(strcmp(lecture->d_name,".")!=0 && strcmp(lecture->d_name,"..")!=0)
    		 {
    					time_t t = sb.st_mtime; 
    					struct tm tm = *localtime (&t); 
    					char s[32]; 
    					strftime (s, sizeof s, "%d/%m/%Y %H:%M:%S", &tm); 
    		 
    					printf ("%-14s %s", lecture->d_name, s); 
    					
    					fprintf(fp, "%s   %s\n", lecture->d_name, s);
    
    
    					printf("\n");
    
    
    		}
    
    
    
    
    
    
          } 
          
          printf("either file was created or edited again...\n");
          
          fclose(fp);
    	
    }
    The function createLog(char...) is used at the beginning of the program and when it receives a signal from the client, it executes the modifyAttributes function.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    > DIR *rep = opendir (address);
    So where is the closedir?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    Arf, didn't see that. I'll try.

    Sorry and thank you

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    I corrected some errors (missing fclose()/closedir()) but no changes. When right clicking->properties on the modified file, the attributes are correct (even with ls -al as said above) but not using the function above.

    Is there another way for showing attributes of a file? Or am I doing something wrong?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,823
    Well it works for me.
    Code:
    $ touch foo.txt ; ./a.out | grep foo.txt
    foo.txt        27/03/2013 21:14:07
    foo.txt        27/03/2012 22:14:07
    My main sets the date back a year.

    Perhaps you're thinking "w+" does something else, because what you don't get is two sets of printout.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Mar 2013
    Posts
    6
    That's strange because I also used "touch" to modify these dates but no change.

    I'll try tomorrow (quite late in France )

    Thank you so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File modification problem
    By led1090 in forum C Programming
    Replies: 14
    Last Post: 09-26-2010, 03:09 AM
  2. Hi Help with reading a file and string modification
    By doubty in forum C Programming
    Replies: 3
    Last Post: 08-06-2009, 03:11 AM
  3. Replies: 3
    Last Post: 11-30-2006, 07:09 AM
  4. direct file modification
    By unreg in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-03-2003, 09:32 AM
  5. Replies: 2
    Last Post: 01-08-2003, 01:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21