Hi there
I have a question about pointers, i have a functions that takes a path to a txt file, extracts whats in it into an array and returns the array.
As i understand it you can't actually pass the array, you have to return the pointer to the array and then dereference the pointer to get to the contents?
I've listed my code below;
If i output line[6] in the omps function, all is well and it gives me line 6 of line array
When i output just line in omps it give me a memory address, i think this is normal?
When i output FOO in main it gives me the same memory address, also normal i think!?
How do i get whats in the memory address, that is the contects of the array. Basically i want to return an array from the OMPS function with the contents of what in the text file then to read it back in in main.
Any help is much appriciated, thanks
PS: complete newb at this so might be barking up the wrong tree!
string *OMPS (char const* path)
{
//set string array length
string line[1000];
//loop counter
int n = 0;
//set text file to read
ifstream myfile(path);
if (myfile.is_open())
{
while ( myfile.good())
{
//Go through line of the text file and find instances of message headers
getline (myfile,line[n]);
//cout << line[n] << endl;
//move on to next line
n++;
}
myfile.close();
}
else cout << "Unable to open file";
//cout << "output IS "<<line[6]<< endl; // gives line 6 of string array
cout << "output IS "<<line<< endl; //gives memory address
return line;
}
int main()
{ string *FOO;
FOO = OMPS("test.txt");
cout<<"\n Output is "<< FOO<< endl;; //gives memory address
system("pause");
return 0;
}