sign up I login
 advanced
refer a friend - earn nickels!!

Ask & Discuss Questions with Community & Experts

Moderation Team
 90 chars left    advanced
Ask iit jee aieee pet cbse icse state board community Community Discussion Question: just check everything is right or not friends ...........
Forum Index -> General Knowledge like the article? email it to a friend.  
Author Message
Vincent (14)

Hot goIITian

Olaaa!! Perrrfect answer. 2  [4 rates]

Vincent's Avatar

total posts: 140    
offline Offline
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 !!!!!!!!

    
spideyunlimited (3916)

Moderator

Olaaa!! Perrrfect answer. 656  [974 rates]

spideyunlimited's Avatar

total posts: 2976    
online Online
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 Olaaa!! Perrrfect answer.   in 2 votes )   [?]
 
You have to be logged on to rate
  
spideyunlimited (3916)

Moderator

Olaaa!! Perrrfect answer. 656  [974 rates]

spideyunlimited's Avatar

total posts: 2976    
online Online
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 Olaaa!! Perrrfect answer.   in 1 votes )   [?]
 
You have to be logged on to rate
  
spideyunlimited (3916)

Moderator

Olaaa!! Perrrfect answer. 656  [974 rates]

spideyunlimited's Avatar

total posts: 2976    
online Online
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 Olaaa!! Perrrfect answer.   in 1 votes )   [?]
 
You have to be logged on to rate
  
spideyunlimited (3916)

Moderator

Olaaa!! Perrrfect answer. 656  [974 rates]

spideyunlimited's Avatar

total posts: 2976    
online Online
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 Olaaa!! Perrrfect answer.   in 0 votes )   [?]
 
You have to be logged on to rate
  
spideyunlimited (3916)

Moderator

Olaaa!! Perrrfect answer. 656  [974 rates]

spideyunlimited's Avatar

total posts: 2976    
online Online
// 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 Olaaa!! Perrrfect answer.   in 1 votes )   [?]
 
You have to be logged on to rate
  
 
Forum Index -> General Knowledge
Go to:   

Top Offers for goIITians
Correspondence Courses
Brilliant Tutorials
Narayana Institute
Aakash Institute
Classroom/Crash Courses
Narayana - Kota , Delhi , Others
Brilliant Tutorials - Class , Crash
Aakash Institute - Medical , Engg
Online Test Series
Brilliant Tutorials
Narayana Institute
Aakash Institute
Mahesh Tutorials
AMITY      Sri Chaitanya