Connecting Tech Pros Worldwide Forums | Help | Site Map

How to find out file's text length

Member
 
Join Date: Jun 2006
Posts: 57
#1: Jun 28 '06
I want to see the file's text's length (How Many Letters).

How Can I Do That????



Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,166
#2: Jun 28 '06

re: How to find out file's text length


If You are using MSVC

GetFileSize

Alternitively

Open the file - fopen
goto the end of the file - fseek
get the file position - ftell
close the file again - fclose
Member
 
Join Date: Jun 2006
Posts: 57
#3: Jun 28 '06

re: How to find out file's text length


:confused: I don't Understand.

???? :confused: :confused:
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,166
#4: Jun 28 '06

re: How to find out file's text length


Those are functions you can look them up in the links I provide in one of your other posts.

If you are using WIN32, i.e. developing on the Windows platform for the Windows platform you can call

GetFileSize( ... )

if you are not on the Windows platform or need to write portable code then you can use the sequence of function calls

fopen( ... )
fseek( ... )
ftell( ... )
fclose( ... )

I suggest you go and look up the help of these functions.
Member
 
Join Date: Jun 2006
Posts: 57
#5: Jun 28 '06

re: How to find out file's text length


In My Document Its Lenght is 36 I Think but in there it writes 6

WHy???
D_C D_C is offline
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 294
#6: Jun 29 '06

re: How to find out file's text length


Is it referring to the number of lines, or the number of characters? Could you post the document, since it doesn't seem too big. I am assuming it's just a test file.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,166
#7: Jun 29 '06

re: How to find out file's text length


Also postring the actual code you have written would help if you wish us to solve any problems in it.
Member
 
Join Date: Jun 2006
Posts: 57
#8: Jun 29 '06

re: How to find out file's text length


Quote:

Originally Posted by D_C

Is it referring to the number of lines, or the number of characters? Could you post the document, since it doesn't seem too big. I am assuming it's just a test file.

YES - It is a Test File:

There I write 5 times "<Kasya>" word in tags
Member
 
Join Date: Jun 2006
Posts: 57
#9: Jun 29 '06

re: How to find out file's text length


Here is My Code:

Quote:
using namespace std;
int main ()
{
char buffer[80];
FILE * pFile;
pFile = fopen ("size.txt","r+");

fseek (pFile,9,SEEK_SET);
cout << ftell(pFile);

fclose (pFile);
return 0;
}
kasya.byethost10.com/size.txt
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,166
#10: Jun 29 '06

re: How to find out file's text length


Use [code&#93 and [/code] to encapsulate code

Expand|Select|Wrap|Line Numbers
  1. fseek (pFile,9,SEEK_SET);
  2.  
Your error is on this line, SEEK_SET sets the origin of the seek (reposition) to the start of the file so with your code the file pointer will always be positioned 9 bytes from the start and ftell should always return 9.

The other options are SEEK_CUR sets the origin as the current position of the file pointer, and SEEK_END set the origin as the end of the file so to position the file pointer at the end of the file so that ftell will give you the file size you should be using

Expand|Select|Wrap|Line Numbers
  1. fseek (pFile,0,SEEK_END);
  2.  
Also you are opening the file in "r+" mode, but you never write it, however opening the file in text mode can confuse things as end of line processing will happen. I subbest you use "rb" mode instead which is read only binary.

Cheers
Ben
Member
 
Join Date: Jun 2006
Posts: 57
#11: Jun 29 '06

re: How to find out file's text length


:) :) :) Thanks!!!!!!!!!!!!
Newbie
 
Join Date: Aug 2007
Posts: 2
#12: Aug 20 '07

re: How to find out file's text length


Why not stick to the old faithful ? :

Expand|Select|Wrap|Line Numbers
  1. long FileSize(char* filename)
  2. {
  3.   struct stat stbuf;
  4.   stat(filename, &stbuf);
  5.   return stbuf.st_size;
  6. }
That's the method described in "The C Programming Language" is much simpler than any other technique, plus you get access to all this as well:

Expand|Select|Wrap|Line Numbers
  1. struct stat   /* inode information returned by stat */
  2.    {
  3.        dev_t     st_dev;      /* device of inode */
  4.        ino_t     st_ino;      /* inode number */
  5.        short     st_mode;     /* mode bits */
  6.        short     st_nlink;    /* number of links to file */
  7.        short     st_uid;      /* owners user id */
  8.        short     st_gid;      /* owners group id */
  9.        dev_t     st_rdev;     /* for special files */
  10.        off_t     st_size;     /* file size in characters */
  11.        time_t    st_atime;    /* time last accessed */
  12.        time_t    st_mtime;    /* time last modified */
  13.        time_t    st_ctime;    /* time originally created */
  14.    };
  15.  
Member
 
Join Date: Aug 2009
Posts: 64
#13: Aug 14 '09

re: How to find out file's text length


This is exactly what i wanted now because I'm making a prime finder another thing I'd like is a loop to check all possible odd factors if I can do that i can find all primes under 18 quintillion I think and store them in files ( yes I have nearly 200 GB of space left)
Reply