Signs in a file without using binary

This is a discussion on Signs in a file without using binary within the C++ Programming forums, part of the General Programming Boards category; What if you feel like knowing how many charecters there are in some file what do you then do. I ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    160

    Exclamation Signs in a file without using binary

    What if you feel like knowing how many charecters there are in some file what do you then do. I know that this doens't work since these functions work with binary and if my file isn't a binary file that's crap.

    File.seekg(0, ios::end);
    Length = File.tellg();
    File.seekg(0, ios::beg);

    So I've aproached the problem this way because I know for a fact that get isn't working with binary.

    Code:
        while (File.good())
        {
          File.get();
    
          Length++;
        }
    But is this the most effecient?
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    All files are binary. Text files is just a special case of a binary file.
    Code:
    int GetFileSize(char* FileName)
    {
       int Result;
       ifstream File(FileName);
    
       if(!File.fail())
       {
          File.seekg(0, ios::end);
          Result = File.tellg();
          File.close();
       }
       return Result;
    }
    ** Edited cause of a non functional function **
    Last edited by Magos; 10-30-2002 at 02:29 PM.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    I know that all files are binary but the result is different when you count binary numbers (0 and 1) you get a higher number then when counting chars...
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  4. #4
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    Not really, unless your size IS in chars. If you get the nuumber of bits in the file, divide by 8 to get the number of chars.

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  5. #5
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    Correct me if I am wrong but,

    If you open a file in text mode, it will cut out some stuff (like \r), and so if you try to check the number of characters in the file,
    you will come up short.

    Of course the real question is, do you want to know how many characters(i.e. *not* including escape chars) are in the file,
    or how big the file actually is (i.e. *including* escape chars) ?
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    The method I gave you counts the actual size of the file, but as Cobra said, if you only want to count the charactersin a file (which you can see in a text editor), you have to use something else. I believe a loop with get() will work.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Which, as you can see above in the start of the thread, is exactly what I did.

    BTW what does '\r' do?
    Last edited by Zahl; 11-02-2002 at 07:14 AM.
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Print file in binary mode
    By vikernes in forum C++ Programming
    Replies: 6
    Last Post: 02-24-2006, 11:43 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21