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 - 9594 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

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

using namespace std;

class SearchSort
   //   int Record_Size;
	//  string Record_Arr[]; // array to store the data
	//  string list_Read;
		void StoreInArr(string);	   

#include <iostream>
#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())
		  getline(inFile, list_Read);
		} ***/
	  //string *line_Read = &list_Read;

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

int main()
	string line;
	SearchSort xyz;
	ifstream inFile;"list.txt");
	   getline(inFile,line, '\n');
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.

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: 1130
  • View blog
  • Posts: 4,686
  • 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"list.txt");
  while( getline(inFile,line))
//every loop is a new line


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1