Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Read 1000 Lines from text file in a single shot ........

Read 1000 Lines from text file in a single shot ........

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++iostutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spalanivel
    wrote on last edited by
    #1

    Hi, Below is the line of code(C++) for Reading the Textfile line by line. CString = strFilePath; // text path file path ifstream Textfile; Textfile.open(strFilePath,ios::in); std::string value; while(!Textfile.eof()) { getline(Textfile,value); // Read line by line... } Textfile.close(); Now the question is I am having 40MB text file size. I need to read 1000 Lines in a single shot (And upto read 1000 times to reach the whole 40 MB size) not line by line. How to do this? (ie from 1 to 1000 and 1001 to 2000.... reach max lines) reg, Subbu

    S D 2 Replies Last reply
    0
    • S spalanivel

      Hi, Below is the line of code(C++) for Reading the Textfile line by line. CString = strFilePath; // text path file path ifstream Textfile; Textfile.open(strFilePath,ios::in); std::string value; while(!Textfile.eof()) { getline(Textfile,value); // Read line by line... } Textfile.close(); Now the question is I am having 40MB text file size. I need to read 1000 Lines in a single shot (And upto read 1000 times to reach the whole 40 MB size) not line by line. How to do this? (ie from 1 to 1000 and 1001 to 2000.... reach max lines) reg, Subbu

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      To read a line from a file, you effectively need to read the file character by character - that's how you find the line termninator character. Sounds to me like you'd be better off memory mapping the whole file and processing it in memory

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

      1 Reply Last reply
      0
      • S spalanivel

        Hi, Below is the line of code(C++) for Reading the Textfile line by line. CString = strFilePath; // text path file path ifstream Textfile; Textfile.open(strFilePath,ios::in); std::string value; while(!Textfile.eof()) { getline(Textfile,value); // Read line by line... } Textfile.close(); Now the question is I am having 40MB text file size. I need to read 1000 Lines in a single shot (And upto read 1000 times to reach the whole 40 MB size) not line by line. How to do this? (ie from 1 to 1000 and 1001 to 2000.... reach max lines) reg, Subbu

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        spalanivel wrote:

        How to do this?

        You can't. You can, however, reduce file I/O by reading the entire file once into a buffer and then process that buffer instead. See Stuart's suggestion.

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups