2014-01-05 43 views
0

文件看起来是这样的:为什么不在相应的数组中存储字符串?

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; 
} 
+3

为什么你'fopen()'同一个文件两次? – ThiefMaster

+1

此外,你会很好地比较['sscanf()'](http://en.cppreference.com/w/c/io/fscanf)的结果与你试图解析的参数数量* *假设他们正确解析。 **检查你的API结果代码** – WhozCraig

+1

你不需要'fopen'两次,'sscanf'行有错误。第一个参数应该是'buffer'而不是'ptr'。 – jyotesh

回答

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; 

} 
+0

你已经修复了显而易见的错误 - 防止它编译的错误,以及双重打开。你并没有真正解决测试'fgets()'或'sscanf()',这两者都很重要。 –

+0

@JonathanLeffler谢谢指出。更新了'fgets()'的检查 –

0

您的代码应该读什么样(固定公然,非编译错误,在调用了ptr而不是buffersscanf()):

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 字符。

相关问题