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
    Posts
    3

    reading in information from a text file

    Hi,

    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
    Posts
    401
    to read a complete line from an input steam, use std::getline http://www.cplusplus.com/reference/string/getline/

    for example, the following program reads lines one by one from the file and prints each line, with a line number, on stdout.
    Code:
    #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, 06:50 PM
  2. question about reading text file
    By aurore in forum Java
    Replies: 6
    Last Post: 08-14-2005, 11:07 AM
  3. Problem: Reading text file with comma
    By Paolo in forum VB Classic
    Replies: 3
    Last Post: 01-08-2001, 08:59 AM
  4. Reading a text file using ADO
    By Ted Young in forum VB Classic
    Replies: 1
    Last Post: 08-16-2000, 08:55 AM
  5. Replies: 1
    Last Post: 05-03-2000, 03: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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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