simple file reading

This is a discussion on simple file reading within the C++ Programming forums, part of the General Programming Boards category; Hello I'd like to determine text file length and read it, if the length doesnt exceed the limit. What is ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    simple file reading

    Hello

    I'd like to determine text file length and read it, if the length doesnt exceed the limit.

    What is the simplest way to do that in C++?

    I have found some examples with eof approach but as far as I remember I've been told this isnt a good way to read files.

    Thanks a lot for help!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    The easiest and safest way, I would think, is to use std::getline and std::string to read line-by-line.
    But why are you interested in the length? Unless you use C buffers, that's not necessary.

    Also, text files are no more special than any other file, so you can only determine its total length, not how many lines there are.
    However, I'm not 100% certain how to get the file size for a file with iostreams...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    To get the filesize in bytes, seekg to the end of the file, and tellg the file position.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Try;
    Code:
           ifstream getText("mytxt.txt",ios::in|ios::binary);
             if (getText.good())
               {
                    getText.seekg( 0, ios::end ) ;
                    int size = getText.tellg() ;
                    getText.seekg( 0, ios::beg ) ;
    
            if(size>SomeNumber){
             // Do something. ..
    
           }
         }
    
     else {
      getText.close();
      return 0;
      }

  5. #5
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    So if I want to determine text file length and use getline, I have to open it in binary mode first, seek end of the file, then close it, open it again in text mode and use getline()?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Meh, just set up your buffer and read until its full.

    pseudocode:

    string block;
    block.resize(some_number);
    while you have room:
    block += readsome(from_here);
    endwhiile
    process(block);


    That way you don't attempt to read too much at once, like you wanted. If some_number is big enough it should be able to handle the expected text file size all at once, as well.

  7. #7
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by citizen View Post
    Meh, just set up your buffer and read until its full.

    pseudocode:

    string block;
    block.resize(some_number);
    while you have room:
    block += readsome(from_here);
    endwhiile
    process(block);


    That way you don't attempt to read too much at once, like you wanted. If some_number is big enough it should be able to handle the expected text file size all at once, as well.
    Theres a reason behind that why I dont want to do it this way. I use some special lua scripts for my application and I dont want them to be half-read and then executed.. I want to make sure about their lenght first and dont read them at all if they're longher than they should be.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Then read line-by-line and determine if that line is the entire lua line (I don't know if the instructions can wrap multiple lines from my limited experience).
    If it's not, then read another line.
    You don't really need to know the size at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Okay, then just throw it away in that case. Streams can let you know if they've been emptied or read completely. They encounter the eof signal if you attempt to do a read and nothing is there.

    more pseudocode:

    while you have room:
    block += readsome(from_here);
    endwhile

    if from_here.eof():
    // eof bit was set - we read everything
    process(block);
    else
    // script was too big for the alloted space
    block.clear();


    Anyway, it's just an idea, but with a well-calculated buffer length it works well. It depends on what is most secure for your program.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I'm not sure if you need to open the file in binary mode to get it's size (in text mode there may be more bytes in the file than there is characters to read, though).
    You can just determine the size as shown, then seek to the beginning and start reading.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Quote Originally Posted by l2u View Post
    So if I want to determine text file length and use getline, I have to open it in binary mode first, seek end of the file, then close it, open it again in text mode and use getline()?
    No, you'd only close the file if you choose not to do anything with it (. .. but close the file after you're done). The file pointer has already returned to the beginning of the file, so you can do whatever you want to do with the file contents (and I use binary out of habit because of the file types I'm generally working with; you can omit binary mode with plain text files).

    . .. I haven't noticed what it was that you wanted to do with the file contents? Fill a buffer? stuff a string? If your intent is to copy the entire file accurately, I'd suggest that you unset whitespaceskip before you begin to read the contents in (ie. getText.unsetf(ios::skipws); /* read spaces */)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  2. Help on simple file functions
    By enjoyincubus in forum C Programming
    Replies: 20
    Last Post: 12-01-2006, 04:41 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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