how do you open a file from a user specified location using fstrea.h, i've been trying
file.open("location specified")
because i was told thats how to do it but obviously its not working! any help would be greatly appreciated!
MinnoW
how do you open a file from a user specified location using fstrea.h, i've been trying
file.open("location specified")
because i was told thats how to do it but obviously its not working! any help would be greatly appreciated!
MinnoW
try this........
ifstream fin("c:/autoexec.bat");
--- you're declaring an object of an ifstream to read from a file..... and initialize it to open the file above....
....
there's couple ways you can open a file.....don't limit yourself to only one....
good luck..
Regards,
matheo917
thank you very much!!!
ok, i declared it:
ifstream file("location");
location is found by:
cout<<"Where is the file located(ie; c:\\frunlog.txt):";
getline(cin,location);
(Keep in mind i'm a beginning programmer)
how do i open the file now? or be able to use it? do i have to do something like file.open("what do i enter here") since it would normally be the name.
what i'm doing is opening the text and assinging the entire thing to an apstring to search through and what not. right now i am not able to open any file unless i declare the ifstream regularly and just put the text i want to open in the same directory as the prog and specify the name. I hope i didn't lose anyone. any help would be great!
oki doki....
like i previously stated....
ifstream fin("c:/autoexec.bat");
declares an object (stream) and ALREADY
got cut off.......
any ..........it already opens a file....
now the trick is in reading the contents of a file.......
hint: remember there's couple of different ways of "opening" and "closing" files.......
ifstream fin("c:/autoexec.bat");
this snippet ..... declares a string and generates a loop that will execute until it encounters an END OF FILE .... and "getline" will read the contents into "fin" until it encounters an END OF LINE '\n'.......Code:string str1; while (!fin.eof()) { getline(fin, str1, '\n'); cout << fin << endl; } fin.close();
hope this helped..
Regards,
matheo917
you need to use a null terminated char array as the argument for the open() method of streams. You can't use instances of a string class.
//ok
ifstream fin("myFile.txt");
//ok
ofstream fout(C:\\myDocuments\\BCB\\homework\\day44.txt");
//ok
char filename = "myFile.txt";
ifstream fin(filename);
//not ok
apstring filename1 = "myFile.txt";
ofstream fout(filename1);
//if c_str() returns the char array upon which apstrings are built
//ok
ofstream fout(filename1.c_str());
If you are using dos or windows you have to use "\\" on unix or other similar os's (linux) you use / to select a directory. Hope this helps
"only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein
YEEE HAAAA, i got to work, thanks to all of you very much!
What i ended up doing was:
ifstream file(location.c_str());
then:
while(!file.eof())
{
getline(file, text);
main = main + "\n" + text;
}
since it kept saying "getline does not take 3 parameters" when i had getline(file, text, '/n')
thanks again!
it does take 3 parameters.....
hmmmm???.......
what compiler are you using...??
Microsoft Visual C++ 6.0(theres probably more info you need but i'm not sure what it would be)
i've never used Micorsoft V. C++ , but i am pretty sure it should take 3 parameters, the last parameter is just a delimeter.......
hmm... i am using Borland's C++ Builder....and everything works fine..
hmmmm
......
anyway.... I am glad you got it to work
Regards,
matheo917