CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2003
    Posts
    87

    Counting the number of characters in a file

    Hi

    I want to write a program that opens a file and counts the number of characters in the file. If any one has a solution I would appreciate it.

    Thanks in advance for your help.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    Code:
    #include <iostream>
    #include <fstream>
    
    int main()
    {
      std::ifstream in("p.cpp",std::ios::binary);
      if (!in)
      {
         std::cout << "problem with file open" << std::endl;
         return 0;
      }
    
    
      in.seekg(0,std::ios::end);
      unsigned long length = in.tellg();
    
      std::cout << "number of characters (method 1) = " << length << std::endl;
    
      /////////
    
      in.seekg(0,std::ios::beg);
    
      int count = 0;
      char c;
      while(in.get(c))
          ++count;
    
      std::cout << "number of characters (method 2) = " << count << std::endl;
    
    
      return 0;
    }
    Last edited by Philip Nicoletti; April 23rd, 2003 at 12:15 PM.

  3. #3
    Join Date
    Feb 2003
    Posts
    87
    Thanks for your reply

    I tried your program and it compiled but it wouldn't work - got the result of 0 characters when my .dat file contained around 20 characters.

    Any ideas?

    Thanks in advance for your help.

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725
    I tested on 4 different compilers, and it worked fine.

    Are you using the standard headers <fstream> , or
    the old non-standard headers <fstream.h>

    Under Windows, I think that if you use the non-standard
    headers, and the file does not exists, the error check
    in the code doesn't work. (just to be sure: did you
    change the name in the file open ? and is it in
    the correct folder, etc.)

    After my post, I editted it and added a second method.
    Did you try that ?

  5. #5
    Join Date
    Feb 2003
    Posts
    87

    Could not get the program to work

    Hi

    I am using Windows XP and using Visual C++ 6

    I have edited one bit of your code which is the file name. This is the code:

    #include <iostream>
    #include <fstream>

    int main()
    {
    std::ifstream in("c:hello.dat",std::ios::binary);
    if (!in)
    {
    std::cout << "problem with file open" << std::endl;
    return 0;
    }


    in.seekg(0,std::ios::end);
    unsigned long length = in.tellg();

    std::cout << "number of characters (method 1) = " << length << std::endl;

    /////////

    in.seekg(0,std::ios::beg);

    int count = 0;
    char c;
    while(in.get(c))
    ++count;

    std::cout << "number of characters (method 2) = " << count << std::endl;


    return 0;
    }


    I still get the problem that I get 0 returned when there are 20 characters in my dat file.

    Any ideas?

    Thanks in advance for your help

  6. #6
    Join Date
    Apr 2003
    Location
    Morelia, Mexico
    Posts
    40
    that code should work.

    Just for the sake of it, try this piece of C code

    PHP Code:
    #include <stdio.h>


    int main()
    {
        
    FILE *myfile;
        
    unsigned long sizeofmyfile;
        
    myfile fopen("myfile.dat","rb");
        if(
    myfile==NULL)
        {
            
    printf("could not open file\n");
            return -
    1;
        }

        
    fseek(myfile,0,SEEK_END);
        
    sizeofmyfile=ftell(myfile);
        
    printf("My file has a size of %ld bytes\n",sizeofmyfile);
        return 
    0;


    If 3 methods fail, then i'm pretty sure that you are doing something else wrong.
    Last edited by Luis G; April 24th, 2003 at 01:53 PM.
    int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
    o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured