reading in information from a text file

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: reading in information from a text file

  1. #1
    Join Date
    Jan 2007

    reading in information from a text file


    i am reading in information from a text file, I am using ifstream, but this takes in every line, how to i read one line at a time, where the program will move to the start of new line each time, also i won't know how many lines will be in the file as it will be different each time.

    Thankyou for any help.

  2. #2
    Join Date
    Dec 2007
    to read a complete line from an input steam, use std::getline

    for example, the following program reads lines one by one from the file and prints each line, with a line number, on stdout.
    #include <iostream>
    #include <fstream>
    #include <string>
    int main()
         std::ifstream file( __FILE__ ) ;
         std::string aline ;
         int n = 0 ;
         while( std::getline( file, aline ) )
            std::cout << ++n << ". " << aline << '\n' ;

Similar Threads

  1. Reading a Com Port and Writing to a Text File
    By RPeare in forum VB Classic
    Replies: 1
    Last Post: 08-17-2007, 07:50 PM
  2. question about reading text file
    By aurore in forum Java
    Replies: 6
    Last Post: 08-14-2005, 12:07 PM
  3. Problem: Reading text file with comma
    By Paolo in forum VB Classic
    Replies: 3
    Last Post: 01-08-2001, 09:59 AM
  4. Reading a text file using ADO
    By Ted Young in forum VB Classic
    Replies: 1
    Last Post: 08-16-2000, 09:55 AM
  5. Replies: 1
    Last Post: 05-03-2000, 04:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center