需要了解下面C程序中while循环条件的工作原理 [英] Need to understand the working of the while loop condition in the below C program

查看:87
本文介绍了需要了解下面C程序中while循环条件的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/* The below program receives strings from keyboard and writes them to file */
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main( )
{
FILE *p ;
char string[80] ;
p = fopen ( "check.txt", "w" ) ;
if ( p == NULL )
{
puts ( "Unable to open file" ) ;
exit(1) ;
}
printf ( "\nEnter text :\n" ) ;
while ( strlen ( gets ( string ) ) > 0 )
{
fputs ( string, p ) ;
fputs ( "\n", p ) ;
}
fclose ( p ) ;
getch();
}





在上述程序中,我无法理解while条件的工作>> while(strlen(gets(string))> 0)<<





请帮助我理解。如果需要更清晰的话,请告诉我。



In the above program I am not able to understand the working of the while condition >> while ( strlen ( gets ( string ) ) > 0 ) <<


Please help me understand it.Please let me know if more clarity in the question is needed.

推荐答案

只需谷歌中的gets()函数。这是一个链接, http://www.tutorialspoint.com/c_standard_library/c_function_gets.htm [ ^ ]



实际上,代码顶部的注释和while循环之前的printf语句正好解释了发生了什么。
Just google the gets() function in c. Here is a link, http://www.tutorialspoint.com/c_standard_library/c_function_gets.htm[^]

In fact, the comment at the top of the code and the printf statement right before the while loop explain exactly what is happening.


while循环可以读取如下: br />


The while loop can be read as the following :

int mLen = 0;
char* strInput = gets ( string ); // console blocks here waiting for input
mLen = strlen(strInput);

while ( mLen > 0 )
{
   fputs ( string, p ) ;
   fputs ( "\n", p ) ;
   strInput = gets ( string ); // console blocks here waiting for input
   mLen = strlen(strInput);
}


这篇关于需要了解下面C程序中while循环条件的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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