| Author |
Message |
![[Post New]](/templates/default/images/icon_minipost_new.gif) 19 Feb 2008 10:57:07 IST
|
|
|
Write a function in C++ to read a text file "NOTES.TXT" , create another text file "NEW.TXT" to contain only the lines that start with letter 'A' .Display the contents of the file "NEW.TXT" .
void countA() { char ch; notes b,c; /*notes is a class previously defined in the program to take the contents of NOTES.TXT */ int count = 0; fstream f; f1.open("NOTES.TXT",ios::in|ios::binary); f2.open("NOTES.TXT",ios::out|ios::binary); if ( !f1 || !f2 ) {cout<<"FILE NOT OPENED!!!!!!!!!!"; exit(0); } f1.read( (char*)&b,sizeof(b) ); while ( !f1.eof() ) { f1.get(ch); if ( f1.eof() ) break; if ( ch=='A' ) f2.write( (char*)&c,sizeof(c) ); else f2.read( (char*)&c,sizeof(c) ); f1.read( (char*)&b,sizeof(b) ); } f1.close(); f2.close(); rename("NEW.TXT","NOTES.TXT"); }
 dosto please help .Rates assured !
|
The one man Dynasty !!!!!!!!
|
|
|
|
![[Post New]](/templates/default/images/icon_minipost_new.gif) 19 Feb 2008 11:08:12 IST
|
|
|
just for safety , before f1.read( (char*)&b,sizeof(b) ); put else {
|
---------------------------------------------------------------
* Gaurav Ragtah ( aka Artemis Fowl )
* Agent 'G' [sniper] - SD-6 (Alliance of Twelve)
* Your friendly neighborhood spideyunlimited |
this reply: 7 points
(with 1 
in 2 votes ) [?]
|
|
You have to be logged on to rate
|
|
|
![[Post New]](/templates/default/images/icon_minipost_new.gif) 19 Feb 2008 11:08:42 IST
|
|
|
In place of
while ( !f1.eof() ) { f1.get(ch); if ( f1.eof() ) break;
use while (f1) { f1.get(ch); //and rest as same. so no need for break statement
|
---------------------------------------------------------------
* Gaurav Ragtah ( aka Artemis Fowl )
* Agent 'G' [sniper] - SD-6 (Alliance of Twelve)
* Your friendly neighborhood spideyunlimited |
this reply: 5 points
(with 1 
in 1 votes ) [?]
|
|
You have to be logged on to rate
|
|
|
![[Post New]](/templates/default/images/icon_minipost_new.gif) 19 Feb 2008 11:11:56 IST
|
|
|
f2.read( (char*)&c,sizeof(c) );
is wrong since you've declared f2 in out mode
|
---------------------------------------------------------------
* Gaurav Ragtah ( aka Artemis Fowl )
* Agent 'G' [sniper] - SD-6 (Alliance of Twelve)
* Your friendly neighborhood spideyunlimited |
this reply: 5 points
(with 1 
in 1 votes ) [?]
|
|
You have to be logged on to rate
|
|
|
![[Post New]](/templates/default/images/icon_minipost_new.gif) 19 Feb 2008 11:14:44 IST
|
|
|
and f2 should open NEW. TXT but you're instead opening the same file with both fstream classs objects.
Also why are you using binary mode with .TXT files? no need
|
---------------------------------------------------------------
* Gaurav Ragtah ( aka Artemis Fowl )
* Agent 'G' [sniper] - SD-6 (Alliance of Twelve)
* Your friendly neighborhood spideyunlimited |
this reply: 0 points
(with 0 
in 0 votes ) [?]
|
|
You have to be logged on to rate
|
|
|
![[Post New]](/templates/default/images/icon_minipost_new.gif) 19 Feb 2008 11:36:12 IST
|
|
|
// DUDE WAIT... it isn't completed.. i'm working on it void countA() { char ch; notes b; /*notes is a class previously defined in the program to take the contents of NOTES.TXT */ int count = 0; ifstream f1; ofstream f2; f1.open("NOTES.TXT",ios::in); f2.open("NEW.TXT",ios::out); if ( !f1 || !f2 ) {cout<<"FILE NOT OPENED!!!!!!!!!!"; exit(0); } else { f1.read( (char*)&b,sizeof(b) ); while (f1) { f1.get(ch); if ( ch=='A' ) f2.write( (char*)&b,sizeof(b) );
f1.read( (char*)&b,sizeof(b) ); } f1.close(); f2.close(); rename("NEW.TXT","NOTES.TXT"); } }
|
---------------------------------------------------------------
* Gaurav Ragtah ( aka Artemis Fowl )
* Agent 'G' [sniper] - SD-6 (Alliance of Twelve)
* Your friendly neighborhood spideyunlimited |
this reply: 5 points
(with 1 
in 1 votes ) [?]
|
|
You have to be logged on to rate
|
|
|
|
|