c语言i––是什么_C语言中的文件处理–第2部分

c语言i––是什么

Read: File Handling in C – Part 1

In the last tutorial I told you about the basic file input output functions. If you are reading this tutorial by skipping the last tutorial then I would strongly recommend you to read that tutorial first. So today I will tell you one program that will count all the characters, spaces, blanks and tabs inside one text file.

File Handling in C

C Program to count characters, spaces, tabs and lines in a text file

Lets straight away write the program first. 



Output

File Handling in C - Part 2

Note: I have opened the demo.txt in my computer. So the output will vary from file to file.

Explanation
1. First of all I have declared one FILE structure pointer np and initialized four integers which are lines, tabs, blanks and characters.

2. After that I have opened the file demo.txt using fopen() function. I have used the same function in my last tutorial too.

3. After that I have started one while loop. Inside it I am fetching the characters of that file using fgetc() function one by one. This while loop will continue till end of file. The end of file is encountered when value of ch become EOF.

5. Now I have used if blocks to count the number of characters, blanks, lines and tabs.

6. At the end of the loop I have closed the file using fclose() function.

7. The I have printed the values inside the integer variables which I have used to count the characters, spaces, blanks and tabs.

Note: This is one of the most frequently used practical application of file input output functions. By introducing some variables inside the program I have used it to count the characters inside the file.

Till now we have seen how to read from file and then display it on console. In our next tutorials we will learn about how to write something into a file.

翻译自: https://www.thecrazyprogrammer.com/2015/02/file-handling-in-c-part-2.html

c语言i––是什么

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值