I've been having a lot of problems with fstream lately..
for my class theres this source code that won't compile... i was hoping someone could help me shed some light...
all i get in the console window isCode:#include<iostream> #include<fstream> using namespace std; void adding(fstream & f, int & counter) { char name[51]; long l = f.tellp() / 50; // find out how many records are in the file (Each I record consumes 50 bytes.) cout << "There are " << l << " records in the file." << endl; cout << "Enter a name "; cin.getline(name, 50); f.seekp(counter * 50); // move the pointer to the last position. f.write(name, 50); // write 50 bytes to the file. counter++; // increase the counter. } void displaying(fstream & f) { int r=0; char name[51]; cout << "Which record do you want to read ?"; cin >> r; cin.ignore(200, '\n'); f.seekg(--r * 50); // locate the pointer for the read() function. f.read(name, 50); // read 50 bytes from the file and store it into 'name' cout << "The name is " << name << endl; } void changing(fstream & f) { int r=0; char name[51]; cout << "Which record do you want to change ?"; cin >> r; cin.ignore(200, '\n'); cout << "Enter a name "; cin.getline(name, 50); f.seekp(--r * 50); f.write(name, 50); } int main() { // open a binary file for both input and output. fstream f("Test.bin", ios::in | ios::out | ios::binary); int i=0; int counter=0; if(f) { // the file can open. while(i != 4) { cout << "1 for adding records " << endl; cout << "2 for displaying records " << endl; cout << "3 for changing records " << endl; cout << "4 for quitting the program" << endl; cin >> i; cin.ignore(200, '\n'); if(i==1) adding(f, counter); else if(i==2) displaying(f); else if(i==3) changing(f); } } else cout << "failed" << endl; f.close(); // close the file associated with "f" return 0; }
please let me know why this is happening..Code:failed press any key to continue .....
also as a side note. while learning fstream i have had many failed attempts in opening an fstream with both ' ios::in and ios:ut '.. perhaps there's a relationship between them. or i just dont know my fstream well enough..
thanks



LinkBack URL
About LinkBacks
ut '.. perhaps there's a relationship between them. or i just dont know my fstream well enough..



