PROBLEM WITH FILE HANDLING

HELLO!
I AM TRYING TO PICK A STRING FROM ONE FILE AND CONVERT IT INTO LOWERCASE LETTERS IN ANOTHER FILE.BUT IT IS OVERWRITING MY PREVIOUS DATA AND PASTING ONLY LAST LETTER OF STRING
PLZ CORRECT IT
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin;
ofstream fout;
void to_upper(char str[])
{
int i =0;
while (str[i]!='\0')
{
if((int(str[i])<123) && (int(str[i])>96))
{
ofstream fout;
fout.open("result1.txt");
fout.put(char(int((str[i])-32)));

}
else
{
ofstream fout;
fout.open("result1.txt");


fout.put(char(int(str[i])));

}
i++;
}

}

void test_to_upper(char str[])
{
ifstream fin;
fin.open("test1.txt");
fin.getline(str,1000000);

to_upper(str);


}
int main()
{
char str [1000] ;

test_to_upper(str);



return 0;



}
1
2
3
4
5
6
7
8
9
10
11
    while (str[i]!='\0')
    {
         if((int(str[i])<123) && (int(str[i])>96))
         {
              ofstream fout;
              fout.open("result1.txt");
              fout.put(char(int((str[i])-32)));

         }
<other code removed>
   }


The problem is here. You're opening the file within a loop, so it's actually reopening the file every time it makes a pass through the loop.

Move the file related statements outside of the loop.
thanx alot buddy
Topic archived. No new replies allowed.