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. using ifstream to read a file.

using ifstream to read a file.

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
4 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.
  • H Offline
    H Offline
    Hachaso
    wrote on last edited by
    #1

    Hi! I'm creating a small program that reads from a text file, using ifstream. I'm wondering how I can tell the stream to start reading the file from the beginning? Is this possible or do I have to close the file and reopen it? This is what I'm doing: ifstream file(filename); if(!file.is_open()) { // Error message. } while(!file.eof()) { file.getline(buf, 200); // after comes more code that is not relevant here. } When I do this getline starts reading line by line. So how can I tell it to start from the beginning? Thanks!

    X S 2 Replies Last reply
    0
    • H Hachaso

      Hi! I'm creating a small program that reads from a text file, using ifstream. I'm wondering how I can tell the stream to start reading the file from the beginning? Is this possible or do I have to close the file and reopen it? This is what I'm doing: ifstream file(filename); if(!file.is_open()) { // Error message. } while(!file.eof()) { file.getline(buf, 200); // after comes more code that is not relevant here. } When I do this getline starts reading line by line. So how can I tell it to start from the beginning? Thanks!

      X Offline
      X Offline
      xiaohe521
      wrote on last edited by
      #2

      but why do you need to read from beginning again I love Programming

      H 1 Reply Last reply
      0
      • X xiaohe521

        but why do you need to read from beginning again I love Programming

        H Offline
        H Offline
        Hachaso
        wrote on last edited by
        #3

        Well I need to get some more data from the file. Is it possible or not?

        1 Reply Last reply
        0
        • H Hachaso

          Hi! I'm creating a small program that reads from a text file, using ifstream. I'm wondering how I can tell the stream to start reading the file from the beginning? Is this possible or do I have to close the file and reopen it? This is what I'm doing: ifstream file(filename); if(!file.is_open()) { // Error message. } while(!file.eof()) { file.getline(buf, 200); // after comes more code that is not relevant here. } When I do this getline starts reading line by line. So how can I tell it to start from the beginning? Thanks!

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          You can use the seekg method for that. Something like

          ifstream input("C:\\Test.txt");

          int txt = 0;
          //Read a number
          input >> txt;
          cout << txt;

          // Go back to beginning
          input.seekg(0, ios::beg);

          // Read the number again.
          input >> txt;
          cout << txt;

          Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          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