0
我已经在C++中编写了一个代码,它必须从txt文件中读取信息,并且当它找到“|”字符必须跳转到新行。它的东西很容易,但我在执行时遇到了问题,并且我一直试图找到问题几个小时,而我没有成功。 :(我连接的代码。从FILE读取并使用strtok函数时出现问题
Thaaaanks提前对您有所帮助。
#include<iostream>
#include<fstream>
#include<string>
#include <stdio.h>
using namespace std;
int main()
{
string ruta_base("C:\\a\\");
char * pch;
ifstream myReadFile;
const string rutaFichero=ruta_base.append("text.txt");
myReadFile.open(rutaFichero.c_str());
char* temp;
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
myReadFile.read(temp,1);
pch = strtok(temp,"|");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, "|");
}
}
}
myReadFile.close();
system("pause");
return 0;
}
你会得到什么输出?请提供一个示例输入文件内容和您得到的输出。 – 2011-01-28 09:54:46
例如,我的“的text.txt”文件将包含“1 |彼得|教授”,输出将是:彼得 教授 (每个不同的元素之后“|”在新线) – pitbull 2011-01-28 10:18:19