Hi, i can't seem to figure this out, how can i open my text file when i call it in a function? The input parameter is a fstream but i'm not sure what to put in there. Thanks
Well that's what my question is, how do i open it in a function? if i put it in main it worked but i need it in a function and i haven't figured out how to do that.
Actually Im also trying to learn the fstream.h file(how to use it....
I will give a working example of a function that accepts a filename as argument....
In the mean while try to see my post titled "How to give a user specified name to a file" or something like that....you will get some help,but Im still a noob (I mean a newbie :P ).
Just wait for 5 mins!!!
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
usingnamespace std;
void allLines(char x[50]);
int main ()
{
char name[50];
for(int i=0;name[i]!='\0';i++)
cin>>name;
allLines(name);
return 0;
}
void allLines (char x[50])//Accept a name-of-file char as argument
{
char a[50];//Idk if accepting a string as a parameter in any function of fstream works
char c='y';
ifstream myfile;//pass the char to open a file
myfile.open(x,ios::in|ios::binary);/**This is the open function,which is more efficient than the one you tried
* First the name of the file is passed as argument
* Then ios is a header which is the base of fstream and iostream
* ios::in means taking in the input
* ios::binary means saving the input in binary(.dat)mode(text is default)**/
while (c=='y'||c=='Y')
{
cout<<"Enter the thing"<<endl;
cin>>a;
myfile>>a;
cout<<"Do you wanna add more?"<<endl;
cin>>c;
if(c=='n'||c=='N')
break;
}
myfile.close();
}