By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

How to find out file's text length

P: 57
I want to see the file's text's length (How Many Letters).

How Can I Do That????
Jun 28 '06 #1
Share this Question
Share on Google+
12 Replies


Banfa
Expert Mod 5K+
P: 8,916
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
Jun 28 '06 #2

P: 57
:confused: I don't Understand.

???? :confused: :confused:
Jun 28 '06 #3

Banfa
Expert Mod 5K+
P: 8,916
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.
Jun 28 '06 #4

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

WHy???
Jun 28 '06 #5

100+
P: 293
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.
Jun 28 '06 #6

Banfa
Expert Mod 5K+
P: 8,916
Also postring the actual code you have written would help if you wish us to solve any problems in it.
Jun 28 '06 #7

P: 57
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
Jun 29 '06 #8

P: 57
Here is My Code:

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
Jun 29 '06 #9

Banfa
Expert Mod 5K+
P: 8,916
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
Jun 29 '06 #10

P: 57
:) :) :) Thanks!!!!!!!!!!!!
Jun 29 '06 #11

P: 2
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.  
Aug 20 '07 #12

P: 85
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)
Aug 14 '09 #13

Post your reply

Sign in to post your reply or Sign up for a free account.