为什么字符串不存储在各自的数组中?

时间:2014-01-05 19:07:45

标签: c

文件如下所示:

trying to read this file#*)will it work?   
每当我尝试阅读它们时,

stringstring2都会有一些垃圾;我猜我的sscanf错了:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char buffer[100];
    char string[100];
    char string2[100];

    FILE *ptr = fopen ("testing8.txt", "r");

    if ((ptr = fopen("testing8.txt", "r"))!= NULL )
        printf("file opened successfuly\ncontinuing program..\n");
    else
    {
        printf("unable to open file, terminating program\n");
        return 0;
    }   

    fgets(buffer, 50, ptr);
    //testing to see whether string contains the string or not..

    printf("%s\n",buffer);

    sscanf(ptr,"%[^#*)]#*)%[^?]?", string, string2);
    puts(string);
    puts("\n");

    puts(string2);
    puts("\n");

    fclose(ptr);
    return 0;
}

2 个答案:

答案 0 :(得分:1)

如果您尝试编译您的代码版本,则应该收到警告

$gcc so_test2.c 
so_test2.c: In function ‘main’:
so_test2.c:28: warning: passing argument 1 of ‘sscanf’ from incompatible pointer type
/usr/include/stdio.h:452: note: expected ‘const char * __restrict__’ but argument is of type ‘struct FILE *’
$

所以,我做了以下更改。这对我有用。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
        char buffer[100];
        char string[100];
        char string2[100];

        FILE *ptr;//fopen ("testing8.txt", "r");

        if ((ptr = fopen("testing8.txt", "r"))!= NULL )
                printf("file opened successfuly\ncontinuing program..\n");
        else
        {
                printf("unable to open file, terminating program\n");
                return 0;
        }

        if (fgets(buffer, 50, ptr) == NULL)  //added check for success of fgets()
        {
                printf("fgets error\n");
                exit (-1);
        }
        //testing to see whether string contains the string or not..

        printf("%s\n",buffer);

        sscanf(buffer,"%[^#*)]#*)%[^?]?", string, string2); //change ptr to buffer
        puts(string);
        puts("\n");

        puts(string2);
        puts("\n");

        fclose(ptr);
        return 0;

}

答案 1 :(得分:0)

您的代码应该是这样的(修复ptr调用中buffer代替sscanf()的明显,非编译错误:

if (sscanf(buffer, "%[^#*)]#*)%[^?]?", string, string2) != 2)
    ...report format error...

请注意,这需要在输入缓冲区中找到序列#*)(如在示例行中)。这些行分别可以工作,失败和工作:

OK#*)This is valid?
Not OK#This is invalid?
OK#*)You won't be able to tell that this is invalid!

如果你想要三个标点符号中的任何一个匹配,那么你需要使用:

"%[^#*)]%*1[#*)]%[^?]?"

*%之间的[会抑制作业。

无法判断是否识别出尾随上下文(例如问号),至少是否修改了sscanf()格式字符串和参数列表:

char qmark;
if (sscanf(buffer, "%[^#*)]#*)%[^?]%[?]", string, string2, &qmark) != 3)
    ...report format error...

您还应该检查fgets()是否实际返回了一行:

if (fgets(buffer, sizeof(buffer), ptr) == 0)
    ...report EOF or error...

我经常建议使用长度受限的转换说明符:

char qmark;
if (sscanf(buffer, "%99[^#*)]#*)%99[^?]%[?]", string, string2, &qmark) != 3)
    ...report format error...

但由于源字符串(buffer)长度相同,因此您无法在此处遇到缓冲区溢出。请注意,如果缓冲区的大小为100,则必须在格式字符串中指定99;如有必要,它会在100 th 字符上写入空值。