LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 12-31-2009, 02:23 PM   #1
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 403

Rep: Reputation: 51
ftell always returns 0


I need to get file size in C. So, using this example: http://www.cplusplus.com/reference/c.../cstdio/ftell/,
i try this code:
Code:
/* ftell example : getting size of a file */
#include <stdio.h>

int main ()
{
  FILE * pFile;
  long size;

  pFile = fopen ("/proc/stat","rb");
  if (pFile==NULL) perror ("Error opening file");
  else
  {
    fseek (pFile, 0, SEEK_END);
    size=ftell (pFile);
    fclose (pFile);
    printf ("Size of /proc/stat: %ld bytes.\n",size);
  }
  return 0;
}
And output of this program is this:
Quote:
Size of /proc/stat: 0 bytes.
What am i doing wrong?
 
Old 12-31-2009, 02:33 PM   #2
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 48
What you're doing wrong is trying to find the size of /proc/stat. It isn't a real file, rather it's an interface to the kernel which is made to act like a file... but this doesn't include having a size:

Code:
> ls -l /proc/stat
-r--r--r-- 1 root root 0 2009-12-31 15:15 /proc/stat
See? Nothing wrong with your program
 
Old 12-31-2009, 03:57 PM   #3
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 403

Original Poster
Rep: Reputation: 51
OK, but how can i determine amount of memory to allocate to read this file whole?
 
Old 12-31-2009, 04:47 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The "size" could change at any time; it doesn't exist until you read it. You probably just have to fgets until you can't read any more. If you allocate a page (malloc(sysconf(_SC_PAGESIZE))) that should be more than enough if you just want to read.
Kevin Barry

edit: You should probably read a whole page so that everything is read at once, otherwise you could be copying parts of two different versions of the file.

Last edited by ta0kira; 12-31-2009 at 04:48 PM.
 
  


Reply



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
Konsole returns ~ gbowden Linux - Software 4 11-19-2006 04:55 AM
Carriage Returns Thorkyl Linux - Software 7 06-28-2004 05:42 PM
setup returns after doing nothing?? dfong63 Slackware - Installation 5 06-04-2004 01:40 AM
Bind returns -1 slackwarefan Programming 18 05-31-2004 12:36 AM
mktime() returns -1 nodger Programming 2 01-27-2004 04:38 PM

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

All times are GMT -5. The time now is 08:02 PM.

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
Open Source Consulting | Domain Registration