文件看起来是这样的:为什么不在相应的数组中存储字符串?
trying to read this file#*)will it work?
string
和string2
有一些垃圾,每当我试着去阅读他们。我猜我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;
}
为什么你'fopen()'同一个文件两次? – ThiefMaster
此外,你会很好地比较['sscanf()'](http://en.cppreference.com/w/c/io/fscanf)的结果与你试图解析的参数数量* *假设他们正确解析。 **检查你的API结果代码** – WhozCraig
你不需要'fopen'两次,'sscanf'行有错误。第一个参数应该是'buffer'而不是'ptr'。 – jyotesh