#include <stdio.h>
#include <conio.h>
int main()
{
FILE *fp;
int i,j,k,ch;
char buf[1];
char s;
clrscr();
printf("Enter the choice 1: Reading 2: Writing :");
scanf("%d",&ch);
fp=fopen("D:\sample.txt","a+");
if(fp==NULL)
{
printf("Error : Not able to Open the file....\n");
}
else
{
do
{
switch(ch)
{
case 1:
{
printf("Reading a file....\n");
fseek(fp,0, SEEK_SET);
while( !feof(fp) )
{
fread(buf, 1, 1, fp);
printf("%c", buf[0]);
}
break;
}
case 2:
{
printf("Press ESC to terminate writing data in the file\n");
printf("Enter the data : ");
fseek(fp,2, SEEK_SET);
do
{
s=getch();
fwrite(&s, 1, 1, fp); /* write struct s to file */
printf("%c",s);
}
while( (s != 27) );
break;
}
default:
{
printf("Entered a wrong option...\n");
break;
}
}
printf("\nEnter the choice 1: Reading 2: Writing 3: Leave :");
scanf("%d",&ch);
if(ch==3)
{
break;
}
}
while( (ch==1) || (ch==2) );
}
printf("\nPress any key to leave the process...\n");
fclose(fp);
getch();
return 0;
}
Actually the code is working fine No problem But while entering some data in the file i pressed enter then the control is taken to starting position of the file ....It is rewritting the data from the beginning in the same line....
if i pressed enter key then the data entering should be proceed from the necxt link i.e how the microsft word works.....