开发者

about file io functions IN C

i am dealing with the file io functions,can any one explain me the use & working of the statement 2,in the following code,here i want to enter string,&then want to write it on a disk.....

#include<stdio.h>
#include<sttring.h>
int main()
{
    FILE *fp;
    char s[80];
    fp=fopen("noname00.cpp","w");
    while(strlen(gets(s))>0) /*parenthesis now at correct place*/ /*开发者_开发百科purpose of this statement */
    {
        fputs(s,fp);
        fputs("\n",fp);
    }
    fclose(fp);
    return 0;
}

Simple but to the point help would be appreciated.


Which one exactly is statement 2? char s[80];? It simply declares and defines a character array of size 80.

If you mean statement 2 is while(strlen(gets(s)>0)), it doesn't look correct to me. strlen() accepts a const char * as its argument but you are specifying it an integer instead: gets(s) > 0.

Also, never use gets() because http://c-faq.com/stdio/getsvsfgets.html

Also, get a copy of http://en.wikipedia.org/wiki/The_C_Programming_Language and study.


The purpose of the statement is to test for just a "return" to break out of the loop. Otherwise the use can continue to enter multiple lines and add more than one line to the file. If you really only want to allow a single line, the while {} can be removed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜