can't open file
need help opening files in a function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
using namespace std;
void displayPets();
int main()
{
char choice;
do
{
displayPets;
//addPets;
cout<<"do you want to continue (Y/N)"<<endl;
cin>>choice;
choice = toupper(choice);
}while(choice != 'N');
return 0;
}
void displayPets()
{
string name;
string breed;
int age;
ifstream fin;
fin.open("C:MyPets.txt");
if (fin.is_open())
{
//read from file
while (isalnum(fin.peek()) )
{
getline(fin, name, '#');
getline(fin, breed, '#');
fin >> age;
fin.ignore(1,'/n');
cout<< name<<"\t"<<breed<<"\t"<<age<<"\n";
}
}
else
{
cout<<"failed to open"<<endl;
}
fin.close();
}
|
Try "C:/MyPets.txt"
with a '/'
after the ':'
Topic archived. No new replies allowed.