0

为什么这段代码打印字符,没有第一个字符?它说ocalhost而不是localhost。感谢帮助。

#include <winsock2.h>
#include <mysql/mysql.h>
#include <iostream>
#include <windows.h>
#include <fstream>
using namespace std;

int main ()  {    
int b = 0;
char * pch;
int stringLength = 0;
char textRead[50];
ifstream infile("config.ini", ios::in | ios::binary);            
if(!infile) {
            cout << "ERROR: config.ini not found!\n";
            system("pause");
            exit(0);
}

infile >> textRead;
stringLength = strlen(textRead);
pch=strchr(textRead,'"');
while(pch != NULL) {
          infile.seekg(pch-textRead-1);
          infile >> textRead;
          pch = strchr(pch+1,'"');
}
cout << textRead;
infile.close();
4

2 回答 2

0

我在猜测 的内容config.ini,因为您没有提供它,但看起来ifstream阅读得很好。cout << textRead << endl;在你infile >> textRead;检查之后放一个。这就是我正在使用的config.ini

localhost = "foo"

不过,您与朋友的逻辑seekg似乎被打破了。 seekg不打算用于支持解析(在您的情况下,跳过引号);它的目的是在需要时跳过大块文件,这样您就不会浪费时间阅读它。老实说,我不确定您在做什么,因为pch-textRead-1如果第一个字符是引号,则可能是 -1。

另一件事是它infile >> textRead;不读取一行,它读取一个单词,并丢弃前导空格。

为了记录,我省略了

#include <winsock2.h>
#include <mysql/mysql.h>
#include <windows.h>

因为不需要。

于 2010-11-18T19:27:51.490 回答
0

在您的 while 循环中,您调用:

  infile >> textRead;
  pch = strchr(pch+1,'"');

当您尝试strchr在第二行中运行时,它仍然引用您在textRead不是最近提取的单词中的前一个字符串。

不幸的是,我无法推断出您实际上想要做什么,因此我无法提供有关如何解决它的建议。

于 2010-11-18T19:27:32.487 回答