I need help stat!!!

This is a discussion on I need help stat!!! within the C Programming forums, part of the General Programming Boards category; I am using stat() to find the size of a file but for some reason I am returned a constant ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    103

    I need help stat!!!

    I am using stat() to find the size of a file but for some reason I am returned a constant file size. Here's what i have for code

    Code:
    struct stat statdata;
    	static struct stat largestFiles[3];
    	
    	int i = 0;
    	int x =0;
    	dentry = readdir(dpntr);
    	while(dentry != 0){
    		
    		i=0;
    
    		if(strcmp((dentry->d_name),"..") > 0){
    			if(!stat(filepathname, &statdata)){
    				
    			        printf("FilePathName: 	%s\n", filepathname);
    				printf("FileName:	%s\n",dentry->d_name);
    				printf("Size of statdata %d\n\n\n ", statdata.st_size);
    and here's what the print out is

    FilePathName: /home/this/me/prog
    FileName: Asg
    Size of statdata 512


    FilePathName: /home/this/me/prog
    FileName: Asg2
    Size of statdata 512


    FilePathName: /home/this/me/prog
    FileName: Asg3
    Size of statdata 512
    I can't figure out where the 512 is coming from but each file is approx. 10-15kb. Can someone spot the not?

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    957
    You're scalling stat() with filepathname each time, which is always /home/this/me/prog. Presumably you want to stat dentry->d_name instead (making sure you're in the correct directory, or building a path by combining filepathname and dentry->d_name).

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    Quote Originally Posted by cas View Post
    You're scalling stat() with filepathname each time, which is always /home/this/me/prog. Presumably you want to stat dentry->d_name instead (making sure you're in the correct directory, or building a path by combining filepathname and dentry->d_name).
    That made a lot of since. Thanks for that insight. I was just wondering though with new fix I still get 512 printed out on some of the files. Is there a reason for this or did I not completely fix the problem.

    Code:
    struct stat statdata;
    	char filepath[256];
    	char *file;
    	static struct stat largestFiles[3];
    	
    	int i = 0;
    	int x =0;
    	dentry = readdir(dpntr);
    	while(dentry != 0){
    		
    		i=0;
    
    		if(strcmp((dentry->d_name),"..") > 0){
    
    			file = dentry->d_name;
    			printf("File name is: %s\n", file);
    			sprintf(filepath, "%s/%s",filepathname,file);
    			printf("FullPathName: %s \n", filepath);
    
    			if(!stat(filepath, &statdata)){
    
    				
    				int size = statdata.st_size;
    				printf("Size of %s is %d\n\n\n ", file, size);


    printout is


    File name is: sqlnet.log
    FullPathName: /home/it02/me/prog/sqlnet.log
    Size of sqlnet.log is 4925


    File name is: country.sql.txt
    FullPathName: /home/it02/me/prog/country.sql.txt
    Size of country.sql.txt is 23888


    File name is: untitled folder
    FullPathName: /home/it02/me/prog/untitled folder
    Size of untitled folder is 512

    that last file didn't seem to have anything in it but it returned 512 again

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    that last file didn't seem to have anything in it but it returned 512 again
    Judging by the name, I'd say the last file is actually a folder.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You should check these against the sizes listed in your file browser (for windows, the file browser is Explorer, I think you can get file sizes by using the "details" type listing). Notice a folder does have a size, they are usually all multiples of a block size (which 512 is).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    Yeah i think you guys are right about size of 512 being folders. I however thought that even if a folder is passed in wouldn't stat still be able to find the size of the folder?

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    The st_size field of struct stat applies to regular files only, not directories, sockets, or r/b device files.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-06-2007, 04:17 AM
  2. Random Number Range Problem.
    By xamlit in forum C Programming
    Replies: 11
    Last Post: 01-26-2006, 11:55 AM
  3. Capturing file stat information
    By Sue Paterniti in forum C Programming
    Replies: 3
    Last Post: 04-15-2002, 05:47 AM
  4. Shortening main
    By pdstatha in forum C Programming
    Replies: 1
    Last Post: 04-03-2002, 03:56 PM
  5. that damn stat()
    By @licomb in forum Linux Programming
    Replies: 2
    Last Post: 08-22-2001, 04:24 PM

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