Do not use on any
shared computer
July 25, 2008 02:34pm pdt
null
[x]
Attachment Details

File Size Function / Reading txt File Problem

Tags: c, file, size
Dear All,

I'm trying to find out file size for my input text files.Here are steps that I followed.

Inputs-
1. A text file at location c:\temp\input_files.txt , this file has list of files for which I've to find file size.
    List includes -   c:\temp\test1.txt
                           c:\temp\test2.txt
                           c:\temp\test3.txt
Logic Followed -

1. I open file c:\temp\input_files.txt
2. Read first line i.e file for which I want file size.
3. Store this file in an array
4. Pass this file to function which returns File Size.
5. Output file size.
 
Problem -

My Program always returns file size only for last line in input_files.txt, for rest of them it returns 0, but if I pass in file name having "C:\temp\test1.txt"
format, instead of reading it from input file, it returns correct value.

thanks & regards
Shirish


Code -

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <malloc.h>

#define INPUT_FIL "C:\\temp\\input_files.txt"

long get_filesize(char *FileName);


void main()
{


      int j=0;
      char inp_file[256];
      char my_file[50][256];
      FILE *fp;
      fp = fopen(INPUT_FIL,"r");

      while(fgets(inp_file,256,fp) != NULL)
                              {
                                          
                                        strcpy(my_file[j],inp_file);
                                          j++;
                                          
                                                                                    
                              }

             long file_size1 = get_filesize(my_file[0]);
             long file_size2 = get_filesize(my_file[1]);
      long file_size3 = get_filesize(my_file[2]);
      printf("%d",file_size1);
             printf("%d",file_size2);
             printf("%d",file_size3);


}



long get_filesize(char *FileName)
{
   
      
    struct stat file;
      
     if(!stat(FileName,&file))
     {
         return file.st_size;
     }

   
      return 0;
}
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: s_more
Solution Provided By: brettmjohnson
Participating Experts: 4
Solution Grade: B
Views: 430
Translate:
Loading Advertisement...
 
[+][-]Accepted Solution by brettmjohnson

Rank: Guru

Accepted Solution by brettmjohnson:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by Ajar
Expert Comment by Ajar:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by Sys_Prog
Expert Comment by Sys_Prog:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by brettmjohnson

Rank: Guru

Expert Comment by brettmjohnson:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by cero
Expert Comment by cero:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by cero
Expert Comment by cero:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Author Comment by s_more
Author Comment by s_more:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34