954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Case Switch with File Processing (Help Needed)

Hey Narue!
I need ur help on a upper-lower case switch problem..
Read the contents of a text file (using ifstream class)..
and create a new file or edit the same one (using ofstream class)
to switch the case of each char (upper to lower & vice versa)..

Thanks a bunch in advance

below is the code i tried but not working:

#include<iostream>
#include<fstream>
#include<cctype>
using namespace std;


int main()
{
ofstream lang("Batch6.txt");
lang<<"Pogram Name Shripal Parekh"<<endl;
lang<<"hELLO wORLD";
cout<<"File Created!\n";
lang.close();

ofstream newfile("SwitchedCases.txt");
ifstream rlang("Batch6.txt");
while(!rlang.eof())
{
char ch=rlang.get();
if(isalpha(ch))
{
if(islower(ch))
{
newfile<<(char)toupper(ch);
}
else
{
newfile<<(char)tolower(ch);
}
}
else
{
cout<<' ';
}
newfile.close();
}
return 0;
}
miraclebos
Newbie Poster
9 posts since Aug 2008
Reputation Points: 10
Solved Threads: 1
 

what is currently happening when you run this?

plz to add code tags to you code

phoenix911
Junior Poster
170 posts since May 2009
Reputation Points: 38
Solved Threads: 21
 

@Phoenix:
It creates the 1st file: "Batch 6.txt" with the specified text..
The case switching works fine.. (I checked by using cout to get the output..)

But the 2nd file: "SwitchedCases.txt" gets create, without the desired output..

miraclebos
Newbie Poster
9 posts since Aug 2008
Reputation Points: 10
Solved Threads: 1
 
Hey Narue!


Why can't I have groupies? I'm sure she'll be thrilled with the shout-out.

Anyway... look at line 36 in relation to your while loop. Walk yourself through a cycle or two of the loop, noting when the file gets closed.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

lol.. sorry didn mean 2 direct the thread at Narue alone.
(it was supposed 2 b reply to a post but i ended up creatin a new thread)..
anyways, u're ryt!! my silly mistake :(
was closing the file inside the loop..
thank u very very much :)

miraclebos
Newbie Poster
9 posts since Aug 2008
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You