Thread: reading from a text file a certain amount

  1. #1
    Registered User
    Join Date
    Jan 2005

    reading from a text file a certain amount


    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


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    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:
    std::ifstream file("myfile.ext");
    file.seekg(10000, std::ios::beg);;

    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, 03:17 PM
  5. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM