C文件处理程序 [英] C file handling program

查看:93
本文介绍了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.

推荐答案

这看起来像家庭作业。因此,我(和其他人在这里)不会为你编写代码。



但这项任务并不困难。亲自尝试一下:
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[^]


  1. 设置一个打开(左)支撑计数器,比如 cnt = 0
  2. 读取 int ,比如 c fgetc()。如果 c == -1 已达到文件结束,则转到第6步。
  3. 如果 c == '{'然后增加 cnt
  4. 如果 c =='}'然后递减 cnt 。检查是否(cnt <0)并在这种情况下报告失败(不平衡大括号)。
  5. 转到第1步。
  6. 如果(cnt == 0)报告成功,否则报告失败。
  1. Set a open (left) brace counter, say cnt=0.
  2. read a int, say c, with fgetc(). If c == -1 'end of file' has been reached, hence goto step 6.
  3. if c ==' {' then increment cnt.
  4. if c == '}' then decrement cnt. Check if (cnt < 0) and report failure (unbalanced brace) in such a case.
  5. goto step 1.
  6. if (cnt == 0) report success, otherwise report failure.

    这篇关于C文件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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