1;从文件指针fp的指的文件中读取一个字符送入字符变量C中.
答案:3 悬赏:70
解决时间 2021-03-14 07:12
- 提问者网友:离殇似水流年飞逝
- 2021-03-13 21:51
1;从文件指针fp的指的文件中读取一个字符送入字符变量C中.2;从文件指针fp的指的文件中读取一个长度为5的文件串送入str的指的内存中.3;将文件位置指针从当前位置向前移到5个字节
最佳答案
- 二级知识专家网友:不傲怎称霸
- 2021-03-13 22:55
#include<stdio.h>#include<time.h>int main(void){ FILE *fp; char a; fp=fopen("c:\\bocai.txt","r");
if(fp==NULL)
{
printf("error!\n"); return 0;
}
else
while((a=fgetc(fp))!=EOF)
{
printf("读到的字符是%c\n",a); sleep(1000); } return 0;}
你只需要改下文件的路径就可以啦!
if(fp==NULL)
{
printf("error!\n"); return 0;
}
else
while((a=fgetc(fp))!=EOF)
{
printf("读到的字符是%c\n",a); sleep(1000); } return 0;}
你只需要改下文件的路径就可以啦!
全部回答
- 1楼网友:一只傻青衣
- 2021-03-14 00:14
#include <stdio.h>
#include <string.h>
#include <conio.h>int main(void)
{
FILE *fp;
char string[] = "This is a test just for you!";
char C;
int i = 0;
if((fp = fopen("test.txt", "w+")) == NULL){
printf("open file error!");
return 0;
}
fwrite(string, strlen(string), 1, fp);
fseek(fp, 0, SEEK_SET);//将文件指针的游标定位在文件开始处 准备从文件中读取内容
C = fgetc(fp); //读取一个字节
printf("从文件指针fp的指的文件中读取一个字符送入字符变量C中: %c\n",C);
memset(string,0,sizeof(string));
for(i = 0; i < 5; i++){
C = fgetc(fp);
string[i] = C;
}
string[i] = '\0';
printf("从文件指针fp的指的文件中读取一个长度为5的文件串送入str的指的内存中:%s\n",string); //将文件位置指针从当前位置向前移到5个字节
//此处理解为从当前位置向前移动5个字节,如果没有向前偏移的话应该输出 "s a test just for you!"
fseek(fp,-5,SEEK_CUR); do
{
C = fgetc(fp);
putch(C);
}while (C != EOF); fclose(fp);
return 0;
}
- 2楼网友:孤伤未赏
- 2021-03-13 23:54
#include <stdio.h> #include <stdlib.h>
void main() { char c,str[6]; file *fp;
if((fp=fopen("test","rb"))==null){ printf("the file <test> can not be opened.\n"); exit(1); }
c=fgetc(fp); putchar(c);
putchar('\n'); //输出回车,与下次输入区分.
fgets(str,6,fp); puts(str);
if(fseek(fp,5,seek_cur)==-1) {//如果fseek定位出错,返回异常,则输出error. printf("error\n"); } else { c=fgetc(fp); while(c!=eof) { putchar(c); c=fgetc(fp); }
} putchar('\n'); fclose(fp); }
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯