Thread: how can i read data from file... plz help me

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    7

    how can i read data from file... plz help me

    in my project in first time to read the data from the file i wrote

    out.seekg( (ID-1) * sizeof ( node ));
    out.read(reinterpret_cost < char * > ( &e ), sizeof (node ));
    e.print();


    the output of data is correct
    but when i want to read it in the second time the out put
    is zero
    i wrote

    out.clear();
    out. seekg(0);
    out.read(reinterpret_cost < char * > ( &e ), sizeof (node ));
    e.print();

    what can i do ...
    plz help me

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    out.read(reinterpret_cost < char * > ( &e ), sizeof (node ));
    Typo?

    What does an "e" object look like (in terms of data members)? How many "records" are you looking into when you do the first read/print (what is ID at that point) and how many total records are there in the file? Is the file attached to the out stream object binary/text and can you verify from looking at the file itself whether there is data at the start of the file? What specifically is the output being produced in both instances?

    What compiler are you using and what (if any) warnings are present during compilation/linking?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    7
    sorry , my type here is wrong
    node is my class so the e is the name to call the class
    int main ()
    {
    node e ;
    ...
    ...
    ..
    }
    Code:
    int main () 
    {
    	 node w ;
    	total q;
    nade e;
    char op;
    int id ,c,m;
    long int pass;
    	 
    ifstream out;
    out.open("out.dat",ios::binary);
    	  if (!out )
    	  {
    		  cout << "  file could not be opened ... " << endl;
    		  exit(1);
    	  }
    	  do {
    		  n--;
    		  cout << "enter the id : \a";
    		  cin >> id;
    		 cout << " entar your password:\a";
    		 cin >> pass;
    
    		 out.seekg((id) * sizeof(node),ios::beg);
    		  out.read(reinterpret_cast <char * > ( &(w)) , sizeof (node ));
    
    
    w.print();
    
    		  if ( !(w.getpass()==pass))
    		  {
    cout << " your password is wrong ";
    		  }
    	 else
    	 {
    
    
     ifstream out ;
    	  out.open("out.r",ios::binary);
    	  if (!out )
    	  {
    		  cout << "  file could not be opened ... " << endl;wait(1);
    		 exit(1);
    	  }
    
    for ( int i =0 ;i<3;i++)
    {
     out.seekg((i+1) * sizeof(nade),ios::beg);
    		  out.read(reinterpret_cast <char * > ( &(e)) , sizeof (nade ));
    		  	e.print();
    }			 w.print();
           
    			
    						  cout << endl;
    			  system("CLS")	;
    
    cout << " do you want to continue  ( y \n )" << endl;
    cin >> op; }while (op!='n'&&op !='N');
    
    
    
    out.clear();
    out.seekg(0);
    
    for ( int i =0 ;i<3;i++)
    {
     out.seekg((i+1) * sizeof(nade),ios::beg);
    		  out.read(reinterpret_cast <char * > ( &(e)) , sizeof (nade ));
    		  	e.print();
    }
    	  return 0;
    }
    in the first time the data is ok
    but in the next thre are no output
    Last edited by fnfn; 05-22-2007 at 06:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read data from file - C program ?
    By Pawan Sangal in forum C Programming
    Replies: 2
    Last Post: 08-22-2008, 04:28 AM
  2. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 05:49 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. read data from file
    By matth in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2005, 09:37 AM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM