LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-05-2005, 11:56 AM   #1
skie_knite007
Member
 
Registered: Dec 2004
Location: India
Distribution: Fedora Core 4
Posts: 145

Rep: Reputation: 15
Not able to obtain file size


I'm not able to btain the size of a file.

When I ran it,, segmaentation fault is occuring



The program is like


#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
main()
{
int fd;
fd=open("test.dat",0);
struct stat *buf;
stat("test.dat",buf);
int n;
n=buf->st_size;
printf("%d",n);
}

The compilation using gcc is not giving any error message..

Wat is the cause of segmentation fault????

How can I get the information abt file in a program,,,if this is not the right way?
 
Old 08-05-2005, 12:07 PM   #2
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Hi,

You are passing an address to a NULL (empty, non-existant) buffer in your call to stat. The exception is occuring when stat tries to fill in your buffer and encounters a bogus pointer. Do the following:

Code:
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
main()
{
    int fd;
    fd=open("test.dat",0);
    struct stat buf;  //NOTICE no *
    stat("test.dat", &buf);  // NOTICE usage of &
    int n;
    n=buf.st_size;  // NOTICE usage of . instead of ->
  printf("%d\n",n);  // NOTICE usage of \n for easier readability
}
BTW: Why are you "open"ing the file? It is not needed for what you are doing.
 
Old 08-05-2005, 12:14 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
Quote:
Originally posted by rstewart
BTW: Why are you "open"ing the file? It is not needed for what you are doing.
I agree with this. But if you really need to open the file for some other purposes, it's not a problem. But then use fstat() instead, passing the file descriptor instead of the string containing the file name. Like this:
Code:
fstat(fd, &buf);
 
Old 08-05-2005, 12:20 PM   #4
skie_knite007
Member
 
Registered: Dec 2004
Location: India
Distribution: Fedora Core 4
Posts: 145

Original Poster
Rep: Reputation: 15
thanx a lot............

It did really work.........

Actally I needed to open the file for some other purposes.
 
Old 08-07-2005, 06:13 AM   #5
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally posted by rstewart
[B]You are passing an address to a NULL (empty, non-existant) buffer in your call to stat. The exception is occuring when stat tries to fill in your buffer and encounters a bogus pointer.
You are NOT passing an address to a NULL (empty, non-existent) buffer - you're passing a random (undefined) value, which may, or may not, be an address. Pedantic point, but accuracy matters.
 
Old 08-08-2005, 01:10 PM   #6
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Quote:
You are NOT passing an address to a NULL (empty, non-existent) buffer - you're passing a random (undefined) value, which may, or may not, be an address. Pedantic point, but accuracy matters.
Your point about the data being random is well taken for the current Linux stack implementation, however actually, pre-initialization is OS dependent if you really want to be pedantic. I have seen some systems generate random values, some systems generate NULL (0) values - it depends upon on how the OS handles variable pre-initialization (either stack or bss - I have seen them handled differently in differing OSes). The point I was trying to make was that the pointer was uninitialized, and as such, any usage of that variable by the stat function would most likely generate a seg fault due to it pointing to a virtual address being outside of the program's current scope.

Not trying to start any sort of flame war here, just trying to help a novice.

BTW: To the OP, what we are both trying to say is that you can't trust the data in any variable that you the programmer yourself have not initialized either at compile time, or run time.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I know the size of a file using C skie_knite007 Programming 3 04-04-2005 11:49 AM
when creating a *.iso file, how to make the file size smaller? minm Linux - Newbie 8 12-26-2004 10:58 PM
file system size larger than fysical size:superblock or partition table corrupt klizon Linux - General 0 06-18-2004 05:18 PM
get file size in C++ danxl Programming 3 11-18-2003 10:24 PM
how to obtain missing file? okok Linux - Newbie 2 02-23-2002 04:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration