Originally Posted by
7stud
You don't need to know the number of lines, you just need to keep reading lines until there are no more lines to read. The basic setup for reading from a file is to create a while loop and then use the read statement as the while conditional:
Code:
while(getline(inputFile, myStr) );
{
//do something with myStr
}
Unfortunately that doesn't quite work the way I need it to. I know how to do that, But I need each line to become assigned to a string. I was thinking something maybe like :
Code:
string item1, item2, something;
int x;
void Stock::GetNames(char *file, int lines)
{
ifstream productlist(file);
if(productlist.is_open())
{
while((getline(productlist, something)) && (x<lines))
{
x++;
switch(x)
case 1:
item1 = something;
break;
case 2:
item2 = something;
break;
// I don't want to have to add more string variables
//than I absolutely have to.
default:
cout<<"Error!\n";
break;
}
}
else
{
cout<<"Invalid File\n";
}
return item1, item2;
}
The problem with this code is that if i were to ever add more "items" to the .txt i would have to recompile the .exe with another item# variable. Is there a way of avoiding a situation like that?