Jus explain to me if theres some filesize returning function.
I opened a file in binary mode.
This is the content of the file and was witten to it by the 'read(char*, int)' function.
ARJUNorp L c nsed Materia P123456y of MicrARJUN of MicroÎÿ@ª ¨¬¬123456®×ÿÿ‹ AJAYN of MicroÎÿ@ª ¨¬¬987656®×ÿÿ‹ ARJUN of MicroÎÿ@ª ¨¬¬123456®×ÿÿ‹ AJAYN of MicroÎÿ@ª ¨¬¬987656®×ÿÿ‹ asdfghjklqwertyuiopzxcvbnmasdfghjklqwertyuiopqq
It is actually binary of my 'Person class'.
But when I use 'seekg', 'tellg' functions I get 2bytes as the file size.
My computer shows 272 bytes.
Because of this I cannot read the data I put in (as the size is 2);
Why is this happening? Is it the problem of the 'ios::end' / 'ios::beg' flags?
Heres the simplified code. Som functions (which work / has no relevance) have been omitted.
Code:
#include<C:\DOCUME~1\USER\MYDOCU~1\WEB\CPPPROG\CODEBL~1\c_person.cpp>
void list_record(Person*, int);
void search_record(Person*, int);
void add_record(Person*, int);
void delete_record(Person*, int);
void edit_record(Person*, int);
void refresh_file(Person*, int, fstream &);
long read_from_file(Person*, fstream &); //transfer all records to Person*.
int main(){
clrscr();
fstream myfile;
myfile.open("Numbers.imp", ios::in | ios::binary | ios::app);
if(myfile.fail())cout<<"E R R O R Opening File !\n";
else{
Person buffer[30];
int size = read_from_file(buffer, myfile); //size becomes 2 :(
int n = size/sizeof(Person);
char ch;
cout<<"No: of saved records :"<<n<<endl; //Since size is 2 the records become zero.
myfile.close(); //Let's close it for now...
do{
cout<<"\n<< M E N U >>\n"
<<"1) List ('l')"<<endl
<<"2) Search ('s')"<<endl
<<"3) Add New Entry ('n')"<<endl
<<"4) Delete an entry ('d'){not implemented yet}"<<endl
<<"5) Edit an entry ('e') {n i y}"<<endl
<<"6) Quit ('q')"<<endl
<<"Your Choice :";
cin>>ch;
switch(ch){
case 'l':list_record(buffer, n);
break;
case 's':search_record(buffer, n);
break;
case 'n':add_record(buffer, n);
break;
case 'd':delete_record(buffer, n);
break;
case 'e':edit_record(buffer, n);
break;
case 'q':refresh_file(buffer, n, myfile);
cout<<"Records updated..."
<<"Exiting...\nPress any key...";
getch();
exit(0);
default :cout<<"Invalid Choice !!!\n";
}
}while(1);
}
return 0;
}
long read_from_file(Person* buff, fstream &file){
file.seekg(ios::beg); file.seekp(ios::beg);
long start = file.tellg(), end, size;
file.seekg(ios::end); file.seekp(ios::end);
end = file.tellg();
size = end-start; //'end-start' give 2 instead of 272bytes. :(
cout<<start<<" "<<end<<" "<<size<<endl;
file.read((unsigned char*) &buff, size); //Copies records to buffer.
return size;
}
void list_record(Person* buff, int n){ [b]I put this function in just to be safe (It works).
if(n){
for(int i=0; i<n; i++)
cout<<i+1<<") "<<buff[ i].get_name()<<"\t\t\t"<<buff[ i].get_numb()<<endl;
for(i=0; i<20; i++)cout<<"==";
cout<<endl;
}
}
here's the output.
0 2 2
No: of saved records :0
<<MENU>>
blah blah