文件的写入与读出的问题
#include<stdio.h>#include<string.h>
#include<stdlib.h>
const int LENGTH=80;
int main()
{
int i;
char mystr[LENGTH]; 为什么编译的时候,这里不对。编译器报“这里需要用常量表达式”
int lstr=0;
int mychar=0;
FILE *pfile=NULL;
char *filename="C:\\myfile.txt";
printf("\nEnter an interesting string of less than 80 characters:\n");
fgets(mystr,LENGTH,stdin);
if(!(pfile=fopen(filename,"w")))
{
printf("Error opening %s for writing.Program terminated.",filename);
exit(1);
}
lstr=strlen(mystr);
for(i=lstr-1;i>=0;i--)
fputc(mystr[i],pfile);
fclose(pfile);
if(!(pfile=fopen(filename,"r")))
{
printf("Error opening %s for reading.Program terminated.",filename);
exit(1);
}
while((mychar=fgetc(pfile))!=EOF)
putchar(mychar);
putchar("\n"); 这里也错了,这是为什么。
fclose(pfile);
remove(filename);
getch();
return 0;
}