Reading from file and converting first letter of words

Hello! I wrote this program which is supposed to read a file character by character,and replace each letter of each word with it's uppercase version.However, it does't work and i can't figure out why.Could anyone please look over it and tell me where is the problem? Also,another aspect that is unclear to me is: if, for example, i use fstream stream_name(file_name,ios::in | ios:: out) ,can i read from/write in a file using only that one stream?
Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream> 
#include <fstream>
#include <malloc.h>
using namespace std;
void upper(char *a,int n);
int main()
{
	
	char *a,c;
	int n=0;
	a=(char *)malloc(200*sizeof(char));
     ifstream read("char.txt",ios::in);
	 for(int i=0;i<(read.eof());i++)
     {   
		 while(read>>c){
		 *(a+i)=c;
		 n++;
		 }
     }
	 read.close();
	 upper(a,n);

   ofstream write("char.txt",ios::out);

   while(!write.eof())
	   for(int i=0;i<n;i++)
	   write<<*(a+i);
	 write.close();
     return 0;
}

void upper(char *a,int n){
	for(int i=0;i<n;i++){
	 if(*(a+i)==' '){
		 *(a+i)=*(a+i+1);
	 if(islower(*(a+i)))
		 *(a+i)=toupper(*(a+i));
	 }
	 }
}
Last edited on
Topic archived. No new replies allowed.