C学习-fgets()篇1

学习fgets()函数时发现了一个问题,先贴代码

#include<stdio.h>
#include<string.h>
#include<ctype.h>
void convert(char buffer[])  //将字符转换大写
{
    int i;
    for(i=0;buffer[i]!='\0';i++)
    {
        buffer[i]=toupper(buffer[i]);
    }
}
int findstring() /* Finding occurrence of one string in another */
{
    int position=0;
    char *find=NULL;
    char buffer[20];  //要查找的字符串
    char source[20];  //buffer字符串要在此字符串查找
    printf("Please input a string you want to find(Less than 20): "); 
    if(fgets(buffer,sizeof(buffer),stdin)==NULL)     //调用fets()输入
    {
        printf("Error reading input.");
        return 1;
    }
    printf("Please input a string sought(Less than 20): ");
    if(fgets(source,sizeof(source),stdin)==NULL)
    {
        printf("Error reading input.");
        return 1;
    }
    //buffer[strlen(buffer)-1]='\0';     //关键的地方
    //source[strlen(source)-1]='\0';
    convert(buffer);   //将两个字符串都转换为大写
    convert(source);
    if((find=strstr(source,buffer))!=NULL)
    {
        position=find-source+1;  //计算匹配的字符串第一个字符出现的字符
        printf("\nThe fist char %c match: %d\n",*find,position);
    }
    else
    {
        printf("\nCan't find the string.\n");
        return 1;
    }
    return 0;
}
int main()
{
    findstring();
    return 0;
}

运行结果

20131110152406140

输入匹配字符串" I love C"

输入要查找的字符串"I love C and C++"

发现程序不匹配,然后将这两行代码注释去掉

buffer[strlen(buffer)-1]='\0';     
source[strlen(source)-1]='\0';
再运行程序看看

xx

这次匹配成功,第一个字符匹配,关键地方在那两行.

再看个实验,先贴代码

#include<stdio.h>
#include<string.h>
int main()
{
    char buffer[20];
    printf("请输入小于长度20的字符串: ");
    fgets(buffer,sizeof(buffer),stdin);
    printf("输入的字符长度为: %d \n",strlen(buffer));
    return 0;
}
x1

 

明明输入长度为5的字符串,但是用strlen()计算时长度为6,这是什么原因呢?

先看fgets()使用说明,摘录于MSDN(01年版的)

 

声明:char *fgets( char *string, int n, FILE *stream );

Return Value

Each of these functions returns string. NULL is returned to indicate an error or an end-of-file condition. Use feof or ferror to determine whether an error occurred.

Remarks

The fgets function reads a string from the input stream argument and stores it in string. fgets reads characters from the current stream position to and including the first newline character, to the end of the stream, or until the number of characters read is equal to n – 1, whichever comes first. The result stored in string is appended with a null character. The newline character, if read, is included in the string.

fgets()从流中获取一定字节的字符储存在字符串string中,关于流我这里是stdin,也就是标准输入流,就是从键盘获取数据,文档说明fgets()从键盘获取n-1字节的字符,参数指定字节为n,但是只能输入n-1字节,原因是最后一个字节要给字符串附加一个'\0'字符,你可以测试一下 ,代码还是刚才那个

 

#include<stdio.h>
#include<string.h>
int main()
{
    char buffer[20];
    printf("请输入小于长度20的字符串: ");
    fgets(buffer,sizeof(buffer),stdin);
    printf("输入的字符长度为: %d \n",strlen(buffer));
    return 0;
}

输入超过20个字符的字符串,看看结果

x3

这是在你可以输入满n-1个字符的情况下,如果你没能输入满n-1个字符呢?

这个就出了点变化,则fgets()从流中获取字符到换行符(或eof)为止,例如输入:  abcd回车 则string中字符就有五个 a,b,c,d,'\n' 然后才是'\0' ,这就是前面明明输入5个字符,但是strlen()却计算出6个字符的原因。

回到刚开始那个例子,如果按照刚才所说,fgets()在获取少于n-1个字符的情况下

字符数组buffer储存内容 "I love C\n\0"

字符数组source储存内容 "I love C and C++\n\0"

用匹配函数strstr()自然匹配不成功啦,因为buffer多了一个换行符'\n',因此为了匹配成功,应该将buffer中换行符去掉,也就是说的那两行代码

buffer[strlen(buffer)-1]='\0';     
source[strlen(source)-1]='\0';

posted on 2014-04-28 21:44  C Pointer  阅读(559)  评论(0编辑  收藏  举报

导航