Need help for a function that reads a txt file and stores each line it reads into a variable.
What would be better to use for the variables: pointers or references? In my program I've declared the variables in main & .h file and passing them to the function.
There is 9 lines that the .txt file then it gives new data to be filled into the variables.
Heres what I've come up with so far but the output goes "Unable to read" line.
NOTE: Using namespace standard, declare the function in a .h file and have it the file correctly included in main.cpp and other cpp file.
Code
void readFile(string var1, string var2) //I know var3-9 is passed. I have them there for an example purpose
{
ifstream datafile ("file.txt");
if (datafile.is_open()) //Opens the txt file
{
while(datafile.good())
{
getline(datafile, var1);
getline(datafile, var2);
getline(datafile, var3); //var3-9 is just for display, i know it isn't passed.
getline(datafile, var4);
getline(datafile, var5);
getline(datafile, var6);
getline(datafile, var7);
getline(datafile, var8);
getline(datafile, var9);
cout << var1 << endl; // Just a test to see if it worked
cout << var2 << endl; // ^
}
datafile.close(); //Closes the txt file after reading in the txt
}
else cout <<"\nUnable to open the data file!\n"; //Reports if unable to read the file to the user
}
Please and Thank you.