Hello
I am writing a file program like that user has to input a string and that string
was written to a file. From that file, that string was read to the another file changing lower case letters to upper case letters and upper case letters to lower case letters.
In my ogram outputs one more character in the second file.
I have been looking for the error,but I can't find. What is the error?

#include<stdio.h>
#include<string.h>

int main()
{
FILE *ptr,*ptr1;

char org[30];
char upper[30];
char lower[30];
char change[30];
int i=0,count=0;

if((ptr=fopen("original.txt","w"))!=NULL)
{
printf("\nEnter a string");
gets(org);
fputs(org,ptr);


}
 fclose(ptr);

if((ptr=fopen("original.txt","r"))!=NULL)
{
if((ptr1=fopen("changecase.txt","w"))!=NULL)
{



 while(!feof(ptr))
 {
  org[i]=getc(ptr); printf("%c",org[i]);
  i++;
  count++;

 }
 org[i]='\0';



 for(i=0;i<count;i++)
 {
  upper[i]=toupper(org[i]);
  lower[i]=tolower(org[i]);
 }

 upper[i]='\0';
 lower[i]='\0';



 for(i=0;i<count;i++)
 {
  if(org[i]==upper[i])
     change[i]=lower[i];
     else change[i]=upper[i];
 }


change[i]='\0';

fputs(change,ptr1);

}
}
return 0;
}

This puts one extra character at the end.

while(!feof(ptr))
 {
  org[i]=getc(ptr); printf("%c",org[i]);
  i++;
  count++;
 }
 org[i]='\0';

Instread, try this:

int c, i = 0;
while ((c = getc(ptr)) != EOF)
    org[i++] = c;
count = i;

Also, try using the isupper() and/or islower() functions for the following part.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.