C文件处理程序 [英] C file handling program
本文介绍了C文件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请指导我这个程序,该程序将读取C源文件并验证文件中右括号和左括号的数量是否相等。使用getc()函数读取文件。
我尝试过:
学习文件处理。
Please guide me about this program, that will read a C source file and verify that the number of right and left braces in the file are equal. Use getc() function to read the file.
What I have tried:
studying about file handling.
推荐答案
这看起来像家庭作业。因此,我(和其他人在这里)不会为你编写代码。
但这项任务并不困难。亲自尝试一下:
- 如果需要,请阅读主函数和传递参数
- 声明变量来计算数字打开和关闭括号
- 使用 fopen - C ++打开文件参考 [ ^ ]
- 使用
而
循环使用 getc - C ++参考a> [ ^ ]直到文件结束(getc
返回 EOF - C ++参考a> [ ^ ])
- 检查角色是否是大括号并增加相应的变量如果是这样
- 使用 fclose - C ++参考资料关闭文件 [ ^ ]
- 使用 printf - C ++参考a>打印结果[ ^ ]
This looks like homework. Therefore, I (and other's here) will not write the code for you.
But this task is not difficult. Just try it yourself:
- If necessary read about the main function and passing arguments
- Declare variables to count the number of opening and closing braces
- Open the file using fopen - C++ Reference[^]
- Use a
while
loop that reads the characters with getc - C++ Reference[^] until the end of the file is reached (getc
returns EOF - C++ Reference[^])
- Check if the character is a brace and increment the corresponding variable if so
- Close the file using fclose - C++ Reference[^]
- Print the result using printf - C++ Reference[^]
- 设置一个打开(左)支撑计数器,比如
cnt = 0
。 - 读取
int
,比如c
,fgetc()
。如果c == -1
已达到文件结束,则转到第6步。 - 如果
c == '{'
然后增加cnt
。 - 如果
c =='}'
然后递减cnt
。检查是否(cnt <0)
并在这种情况下报告失败(不平衡大括号)。 - 转到第1步。
- 如果
(cnt == 0)
报告成功,否则报告失败。
- Set a open (left) brace counter, say
cnt=0
. - read a
int
, sayc
, withfgetc()
. Ifc == -1
'end of file' has been reached, hence goto step 6. - if
c ==' {'
then incrementcnt
. - if
c == '}'
then decrementcnt
. Check if(cnt < 0)
and report failure (unbalanced brace) in such a case. - goto step 1.
- if
(cnt == 0)
report success, otherwise report failure.
>
这篇关于C文件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文