Reading from text file one line at a time

Read one line from text file and then move to the other

Page 1 of 1

1 Replies - 5056 Views - Last Post: 12 November 2009 - 07:06 PM Rate Topic: -----

#1 jingoria  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 96
  • Joined: 22-October 07

Reading from text file one line at a time

Post icon  Posted 12 November 2009 - 07:00 PM

Hi,
I am trying to implement a program that reads from a text file, a line at a time. I am able to read the first line using the getline function but now how would I go to the next line. Here is my code so far



#ifndef H_SearchSort
#define H_SearchSort

#include<iostream>
#include<string>
using namespace std;

class SearchSort
{
   //   int Record_Size;
	//  string Record_Arr[]; // array to store the data
	//  string list_Read;
		 
 public:
		void StoreInArr(string);	   
};
#endif



#include <iostream>
#include<fstream>
#include "SearchSort.h"

using namespace std;

void SearchSort::StoreInArr(string line_Read)
{
	 // int Array_Size = 0;
	//string Array[Array_Size];
	 int i = 1;
	 
  /***  ifstream inFile("list.txt");
	 
	 if (inFile.good())
	 {
		while(!inFile.eof())
		{
		  getline(inFile, list_Read);
		} ***/
	  //string *line_Read = &list_Read;
	  cout<<line_Read<<endl;
	
	  return;
} 
	  

#include "SearchSort.h"
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	string line;
	SearchSort xyz;
	
	ifstream inFile;
	inFile.open("list.txt");
	   getline(inFile,line, '\n');
	   xyz.StoreInArr(line);	
	
system ("PAUSE");
return 0;

}



I thought of making a string pointer that points to the variable 'line' which has the first sentence and then increment the pointer to point to next address but I realized that variable 'line' is just storing the read in sentence and not really pointing to the sentence in the text file.

Please someone help me.
Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading from text file one line at a time

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Reading from text file one line at a time

Posted 12 November 2009 - 07:06 PM

by using a loop
	inFile.open("list.txt");
  while( getline(inFile,line))
{
cout<<line<<endl;
//every loop is a new line


}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1