关于如何读取文件并在C中打印 [英] about how to read a file and print it in C

查看:125
本文介绍了关于如何读取文件并在C中打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写程序包括所有功能:

1.读取指定文件 sample.txt ,然后在屏幕上打印文件内容。

2.在屏幕上以相反的顺序打印文件的每一行。

3.打印一个表格,显示样本文件中4.alphabet的每个字母的出现次数。

5.打印一个表格,显示样本文件中出现的单字母单词,双字母单词,三个字母单词等的数量。


sample.txt是:

Write a program includes ALL the features:
1.Read the specify file sample.txt and then print the contents of the file on screen.
2.Print each lines of the file in reversed order on screen.
3.Print a table indicating the number of occurrences of each letter of the 4.alphabet in the sample file.
5.Print a table indicating the number of one-letter words, two-letter words, three- letter words, etc., appearing in the sample file.

sample.txt is :

展开 | 选择 | Wrap | 行号

推荐答案




fscanf(cfPtr,"%s",text);


这意味着:读一个字符串来自文件内容并将其放入变量text中。

由于字符串可以通过空格,制表符或返回来终止,fscanf函数在到达I之后的空格字符后会立即停止...


我的建议:

考虑使用函数fgetc,它读取文件内容by charactrer,同时,使用其他函数将此字符打印到标准输出,直到达到EOF。


这将解决第一个问题,希望有所帮助。
Hi

fscanf(cfPtr,"%s",text);

This means: read a "string" from file content and put it into the variable "text".
Since a "string" can be terminated by space, tab or return, the fscanf function would stop right after it reaches the space character after the "I"...

My suggestion:
Think about using the function fgetc, it reads the file content "character by charactrer", meantime, use other functions to print out this character to standard output, until you reach the EOF.

That would solve the first question, hope that helps.


你可以用你的代码来解释吗?

i喜欢用fgetc看你的代码..

谢谢..
can you explain using your code??
i like to see your code using fgetc..
thanks..


我试过用fgetc。


while((ch = fgetc(cfPtr))!= EOF){

printf("%c",ch);

}


它将打印文件的内容。现在尝试反向打印。
I tried with fgetc.

while((ch = fgetc(cfPtr)) != EOF){
printf("%c", ch);
}

It will print the contents of file. Now try to print it in reverse.


这篇关于关于如何读取文件并在C中打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆