Really big problem

This is a discussion on Really big problem within the C++ Programming forums, part of the General Programming Boards category; Jus explain to me if theres some filesize returning function. I opened a file in binary mode. This is the ...

  1. #1
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Angry Really big problem

    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

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    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;
    }
    The red line probably doesn't do what you want it to. Try doing this for your function instead:
    Code:
    int len;
    file.seekg(0,ios::end); //seek 0 from end of file
    len = file.tellg();
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Thumbs up Thanks a miillion

    I'll Try that.
    And I DID !!!
    It WORKS !!!.
    I was so stupid.
    I mistook the polymorphic form.
    1) seekg(long int);
    2) seekg(long int, flag);
    I mistook the poly morphic form no:1
    to seekg(flag);
    Thanks again!!!!!!!
    Last edited by arjunajay; 06-04-2005 at 06:14 AM. Reason: trial

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. big number problem
    By mad_muppet in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 01:54 AM
  2. Plzzzzz Help Me >>> Big Problem
    By AHMED KHALAF in forum C Programming
    Replies: 2
    Last Post: 12-09-2004, 07:31 AM
  3. [Help] C++ Problem
    By Oxide in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2004, 10:05 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21