Sponsored Content
Top Forums Programming [c] How to calculate size of the file from size of the buffer? Post 302656147 by jim mcnamara on Thursday 14th of June 2012 09:55:10 AM
Old 06-14-2012
It is not guaranteed to work. Use stat() to get a file size.
Code:
#include <sys/stat.h>
size_t filesize(int fd)
{
     struct stat st;
     if(fstat(fd, &st) == -1 )
     {
          perror("cannot stat file");
          exit(1);
      }
     return st.st_size;
}

void myfoo(void)
{
    size_t bytes;
    //.......... your code here
     bytes=filesize(fileno(fic_sortie));
    //.......  more code here
}

try something like that.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

how to calculate the file size?

HI, Can somebody please tell me how many bytes make a KB & MB. ThANKS. Rooh.:( (3 Replies)
Discussion started by: rooh
3 Replies

2. Programming

Using fread if the buffer size is not known

Hi... I am trying to read a binary data that have different types of messages of different lengths. I am using fread() but this functions needs the size and count to read the buffer from the file. I think this may cause that the buffer overlaps other messages. Is there an alternative to read... (1 Reply)
Discussion started by: jlrodz
1 Replies

3. Shell Programming and Scripting

How to calculate file's size in directory and subdirectory

Hi, I have written one script to calculate total space of all file in one directory, ignoring subdirectory, it works fine. Now, I've been trying to calculate all files which includes files in any subdirectories. I use recursive function to do this, but it can work only if there is only one... (4 Replies)
Discussion started by: KLL
4 Replies

4. AIX

Pipe Buffer Size

Hi:- One of our users is getting an error: "There is no process to read data written to a pipe.” I am trying to find out what the pipe buffer size is currently set to. How do I go about this? Thanks, (0 Replies)
Discussion started by: janet
0 Replies

5. Red Hat

buffer cache size

hi everyone, can any one help change the buffer cache size in redhat and suse?? this error i got when i installed oracle 10g and it went well and when i try to mount the database using startup cmd it says too many buffer cache parameters (error code : ora-1034) thnq in advance (0 Replies)
Discussion started by: gsr_kashyap
0 Replies

6. UNIX for Dummies Questions & Answers

calculate directory size by year of file

I need to calcualte the size of a directory by the year the files in that directory were created . For example the script will sum up, by year, the number of blocks for that directory and its' subdirectories for files created / accessed in that year. I need a report that would look like... (11 Replies)
Discussion started by: igidttam
11 Replies

7. UNIX for Dummies Questions & Answers

How to check the buffer size of a file?

I have a c program and I want to know what command to use to display the current buffer size of the file using Terminal in Unix? (0 Replies)
Discussion started by: Izzy123
0 Replies

8. Programming

Maximum buffer size for read()

Hi friends, Hope everybody is fine. First have a look at my code, then we will talk about it. $ cat copy.c #include <stdio.h> #define PERMS 0644 /* RW for owner, R for group, others */ #define BUFSIZE 1 char *progname; int main(int argc,char * argv) { int f1, f2, n; ... (4 Replies)
Discussion started by: gabam
4 Replies

9. UNIX for Dummies Questions & Answers

Decrease buffer size

Hi, I am using the below command to get the output in a file called "Logs.txt" tail -f filename | egrep -i "cpu | hung " >> Logs.txt The problem is the Logs.txt file gets updated only after the buffer is 8Kb, but i want to update the file immediately and not wait for the buffer to get 8kb. Is... (8 Replies)
Discussion started by: @bhi
8 Replies

10. Shell Programming and Scripting

Find a particular directory in multiple file systems and calculate total size

Hello : I need some help in writing a ksh script which will find a particular directory in all the file systems in a server and finally report the total size of the direcotry in all the file systems. Some thing like this.. find /u*/app/oracle -type d -name "product" -prune and then... (1 Reply)
Discussion started by: Sam1974
1 Replies
explain_fstat(3)					     Library Functions Manual						  explain_fstat(3)

NAME
explain_fstat - explain fstat(2) errors SYNOPSIS
#include <libexplain/fstat.h> const char *explain_fstat(int fildes, struct stat *buf); const char *explain_errno_fstat(int errnum, int fildes, struct stat *buf); void explain_message_fstat(char *message, int message_size, int fildes, struct stat *buf); void explain_message_errno_fstat(char *message, int message_size, int errnum, int fildes, struct stat *buf); DESCRIPTION
These functions may be used to obtain explanations for errors returned by the fstat(2) system call. explain_fstat const char *explain_fstat(int fildes, struct stat *buf); The explain_fstat function is used to obtain an explanation of an error returned by the fstat(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. This function is intended to be used in a fashion similar to the following example: if (fstat(fildes, buf) < 0) { fprintf(stderr, "%s ", explain_fstat(fildes, buf)); exit(EXIT_FAILURE); } fildes The original fildes, exactly as passed to the fstat(2) system call. buf The original buf, exactly as passed to the fstat(2) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. explain_errno_fstat const char *explain_errno_fstat(int errnum, int fildes, struct stat *buf); The explain_errno_fstat function is used to obtain an explanation of an error returned by the fstat(2) system call. The least the message will contain is the value of strerror(errnum), but usually it will do much better, and indicate the underlying cause in more detail. This function is intended to be used in a fashion similar to the following example: if (fstat(fildes, buf) < 0) { int err = errno; fprintf(stderr, "%s ", explain_errno_fstat(err, fildes, buf)); exit(EXIT_FAILURE); } errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. fildes The original fildes, exactly as passed to the fstat(2) system call. buf The original buf, exactly as passed to the fstat(2) system call. Returns: The message explaining the error. This message buffer is shared by all libexplain functions which do not supply a buffer in their argument list. This will be overwritten by the next call to any libexplain function which shares this buffer, including other threads. Note: This function is not thread safe, because it shares a return buffer across all threads, and many other functions in this library. explain_message_fstat void explain_message_fstat(char *message, int message_size, int fildes, struct stat *buf); The explain_message_fstat function may be used to obtain an explanation of an error returned by the fstat(2) system call. The least the message will contain is the value of strerror(errno), but usually it will do much better, and indicate the underlying cause in more detail. The errno global variable will be used to obtain the error value to be decoded. This function is intended to be used in a fashion similar to the following example: if (fstat(fildes, buf) < 0) { char message[3000]; explain_message_fstat(message, sizeof(message), fildes, buf); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. fildes The original fildes, exactly as passed to the fstat(2) system call. buf The original buf, exactly as passed to the fstat(2) system call. explain_message_errno_fstat void explain_message_errno_fstat(char *message, int message_size, int errnum, int fildes, struct stat *buf); The explain_message_errno_fstat function may be used to obtain an explanation of an error returned by the fstat(2) system call. The least the message will contain is the value of strerror(errnum), but usually it will do much better, and indicate the underlying cause in more detail. This function is intended to be used in a fashion similar to the following example: if (fstat(fildes, buf) < 0) { int err = errno; char message[3000]; explain_message_errno_fstat(message, sizeof(message), err, fildes, buf); fprintf(stderr, "%s ", message); exit(EXIT_FAILURE); } message The location in which to store the returned message. If a suitable message return buffer is supplied, this function is thread safe. message_size The size in bytes of the location in which to store the returned message. errnum The error value to be decoded, usually obtained from the errno global variable just before this function is called. This is neces- sary if you need to call any code between the system call to be explained and this function, because many libc functions will alter the value of errno. fildes The original fildes, exactly as passed to the fstat(2) system call. buf The original buf, exactly as passed to the fstat(2) system call. SEE ALSO
fstat(2) get file status explain_fstat_or_die(3) get file status and report errors COPYRIGHT
libexplain version 0.52 Copyright (C) 2008 Peter Miller explain_fstat(3)
All times are GMT -4. The time now is 05:22 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy