reading from a text file a certain amount

This is a discussion on reading from a text file a certain amount within the C++ Programming forums, part of the General Programming Boards category; hi, i was wondering, if you were reading information from a text file,could you control the amount read in? For ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    18

    reading from a text file a certain amount

    hi,

    i was wondering, if you were reading information from a text file,could you control the amount read in?

    For example, if you had 3MB of data (just sequences of letters) in a text file, could the program read like 10,000 characters inside from that 3MB file.


    kind regards

    Johnny

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    A for loop reading 10000 characters ought to do it.

    Or use one of the read() functions, and point at a buffer of 10K chars, and ask it to read 10K
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    If you mean read the first 10000 characters, just read 10000 characters (as explained above).
    If you mean to start reading after the 10000th character, use seekg() to go to that spot:
    Code:
    std::ifstream file("myfile.ext");
    
    file.seekg(10000, std::ios::beg);
    
    file.read(...);
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 02:17 PM
  5. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM

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