2017-08-09 101 views
0

我从文本文件中读取字符串时遇到问题。在文件中,有数据格式为:如何忽略fscanf中的引号

东西=“测试”

我想读引号之间的字符串。所以在我的节目,我做的:?

fscanf(fil,"language=\"%s[^\"]",data); 

fscanf(fil,"language=\"%s\"",data); 

,但我总是在可变数据测试”我怎么能忽略第二个引号除了把空间之前在文件正是我想要在文本文件格式。

我将是帮助表示感谢。

+3

's'不属于那里的第一个片段。 – WhozCraig

+0

数据是字符数组数据[20],所以我认为s是很好的格式。 –

+0

'fscanf'无法备份,因此当它以格式读取'%s'时,它会读取一个完整的字符串,这是所有字符,直到空白为止。 – stark

回答

0

如果你不想太深入思考,你总是可以在满弦比读格式化字符串拿出你想要的子字符串。

实施例:

通过str下面的代码搜索该第一"和最后"并提出子串在strNew

#include <string> 
#include <iostream> 

using namespace std; //used for ease but not the best to use in actual code. 

int main() 
{ 
    string str = "variable=\"name\""; 
    cout << str << endl; 
    int first = str.find("\""); 
    int last = str.find_last_of("\""); 
    string strNew = str.substr (first + 1, last - first -1); 
    cout << strNew << endl; 
    return 0; 
} 
+0

好吧,那就是我所需要的,现在它的工作原理就是我想要的。 –