alternative for getline

Hi,

Need an alternative to getline to read the text file and copy each line into a vector..

right now i am using this code but the obj file which i am using in my project is not allowing me to use getline, so it throws lnk2005 error which says getline is already defined in xxx.obj file.

1
2
3
4
5
  while(inpFile.good())
	{
	    getline(inpFile,temp);
		tempVector.push_back(temp);
	}


Please help me out to get an alternative for getline..

Thank you.
You could try using >> and append maybe then clear it after each line
Hi giblit,

my input file has got few fields with | as delimeter.

Say for example

1
2
num|name|age|address|hobby
1|szpt9m|28|xyz|abc


so as per your suggestion i used
1
2
inpFile >> temp;
tempVector.push_back(temp);


but when sending it to vector took the whole file instead of each line.

Please let me know if I have done something wrong.. I am a beginner c++

Thank you.
Try using the full name: std::getline(). You'll learn about namespaces and std later on.

It is strange that there is somehow another definition of getline somewhere. Did you try defining one?
Hi,

I didnt define anything.. But I am using few libs and objs in my project which required for my tool to work.. So in one of the obj file it is defined i guess.. thats the reason i want an alternative for this.
Which compiler do you use?

Try compiling this program:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main(int argc, char *argv[]){
   std::string input("");
   
   std::getline(std::cin,input);
   std::cout << "You entered: " << input << std::endl;
   return 0;
}
Last edited on
This program will work fine if i do not include that obj file in my program. But it will throw lnk2005 error if i use that obj file!
Last edited on
you don't need to use getline.

you can use string functions like find() and substr() for that purpose:

http://www.cplusplus.com/reference/string/string/find
http://www.cplusplus.com/reference/string/string/substr
I need to get each line of that text file into a vector. is that possible if i use find and substr
well, this

inpFile >> temp;

reads all characters until a white space is found.

See
http://www.cplusplus.com/reference/cctype/isspace/
what a white space is


if that above statement reads all characters until the end of the file then there's no white space (and no end of line).

Without getline() you need to find() the separators and extract the substr() with the functions shown above.
Topic archived. No new replies allowed.