#include <stdio.h>
#include<stdlib.h>
#include <string.h>
void Fun(char *fname,char st[],int number)
{
FILE *myf;
int i;
if(number==0)
{
myf=fopen(fname,"w");
}
else
{
myf=fopen(fname,"a");
}
if (myf == NULL)
{
printf("cannot open the file.\n");
exit(0);
}
for(i=0;i<strlen(st); i++)
{
fputc(st[i],myf);
}
fclose(myf);
}
int main()
{
int number=0;
char st1[]="new world";
Fun("test",st1,number);
char st2[]="hello";
number=1;
Fun("test",st2,number);
FILE *fp = fopen("test","r");
char ch;
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
}
fclose(fp);
return 0;
}
* // reproduced in the relevant code Description
function prototype: int fputc (int c, FILE stream);
effect: the character c write stream file
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{
FILE *fp;
char ch;
if( argc != 2 )
{
printf("Usage:%s filename\n\a",argv[0]);
exit(1);
}
if( (fp=fopen(argv[1],"wt+")) == NULL )
{
printf("File %s open failed!\n\a",argv[1]);
exit(1);
}
printf("Input a string:");
ch = getchar();
while( ch != '\n')
{
fputc(ch,fp);
ch = getchar();
}
printf("\n");
return 0;
}