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