#include <stdio.h>main(){char ch1;FILE *fp;fp=fopen("d:\\file","w");if(fp==NULL){printf("\n can't open the file\n");getch();exit(0);}printf("%d\n",fp);ch1=getchar();while(ch1!='$')fputc(ch1,fp);fclose(fp);fp=fopen("d:\\file","r");ch1=fgetc(fp);while(ch1!=EOF){putch(ch1);ch1=fgetc(fp);}fclose(fp);getch();}
3 回答

PIPIONE
TA貢獻1829條經(jīng)驗 獲得超9個贊
在前面頭那里加入#include"string.h"
使用c=getchar()函數(shù) 這個函數(shù)的意思是 從鍵盤輸入一個字符

叮當貓咪
TA貢獻1776條經(jīng)驗 獲得超12個贊
對文件的讀和寫是最常用的文件操作。在C語言中提供了多種文件讀寫的函數(shù):
1.字符讀寫函數(shù) :fgetc和fputc
2.字符串讀寫函數(shù):fgets和fputs
3.數(shù)據(jù)塊讀寫函數(shù):freed和fwrite
4.格式化讀寫函數(shù):fscanf和fprinf

當年話下
TA貢獻1890條經(jīng)驗 獲得超9個贊
一、
ch1=getchar();
//while(ch1!='$')//這里不是個死循環(huán)??去掉再試
fputc(ch1,fp);
fclose(fp);
二、 輸出也要改一下
while(1)
{
ch1=fgetc(fp);
if ( ch1==EOF) break ;
putch(ch1);
}
- 3 回答
- 0 關(guān)注
- 133 瀏覽
添加回答
舉報
0/150
提交
取消