Hi I'm Indrajeet. I was writing a program in VC++ 2010 Express for file manipulation, and after finally getting all the syntax errors out,my code looks lie this:
/*********************************************************************
This is a program for file manipulation in VC++
Created by Indrajeet Roy
*********************************************************************/
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
//Protoype of the functions
void read(char *fn);
void write(char *fn);
void append(char *fn);
void main()
{
char fn[20];
int ch;
printf("\t\tEnter the name of the file\n\t\t");
scanf("%s",&fn);
//Print the Menu:
printf("\n1)Write\n2)Read\n3)Add to a prev file\n");
printf("\nEnter '4' to exit");
scanf("%d",&ch);
//Actual Processing:
do
{
switch (ch)
{
case 1:write(fn);
break;
case 2:read(fn);
break;
case 3:append(fn);
break;
default: printf("Please try again");
}
}while(ch!=4);
}
void read(char *fn)
{
FILE *fw;
char ch;
printf("\n");
fw = fopen(fn,"r");
do
{
ch=fgetc(fw);
printf("%c",ch);
} while(ch!=EOF);
fclose(fw);
}
void write(char *fn)
{
FILE *fw;
char ch;
fw=fopen(fn,"w");
if(fw==NULL)
abort();
printf("\nEnter the text.Press * to exit\n");
while((ch=getche())!='*')
fputc(ch,fw);
fclose(fw);
}
void append(char *fn)
{
FILE *fw;
char ch;
fw=fopen(fn,"a+");
if(fw==NULL)
abort();
printf("\nEnter the text.Press * to exit\n");
while((ch=getche())!='*')
fputc(ch,fw);
fclose(fw);
}
The problems are
1)When the code runs,the write() does not pass control back after I press the *.
2)In the read(),it displays an unending loop of printing "3" for some reason!!
Please help.
Thanks
Indrajeet Roy