所以我在 CodeLite 的作業區和 Test 檔案夾中有一個 Test 檔案夾:
主檔案
測驗.txt
問題是每當我嘗試從 test.txt 讀取時,編譯器都會洗掉檔案內容并在我的 test.txt 檔案中寫入“Debug/main.cpp.o”。例如,如果我的 txt 檔案包含以下文本:
Abcd ef
我在 main.cpp 中的代碼:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string data;
ifstream infile;
infile.open("text.txt");
cout << "Reading from the file" << endl;
infile >> data;
return 0;
}
當我運行我的代碼時,輸??出應該是:
Reading from file
Abcd
ef
但相反,輸出是:
Reading from file
現在我的 test.txt 包含:
除錯/main.cpp.o
我還插入了我的檔案夾包含的內容:

我不知道它為什么這樣做。任何人都可以幫忙嗎?
uj5u.com熱心網友回復:
發生這種情況是因為您沒有使用/列印資料變數。使用此代碼
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(){
string data;
ifstream infile;
infile.open("test.txt");
cout<<"Reading from file"<<endl;
while (getline(infile,data))
{
cout<<data<<endl;
}
return 0;
}
uj5u.com熱心網友回復:
Codelite生成$(project).txt($(project)是Test你的情況)的所有物件檔案名編輯(如回應檔案(以命令列的長度的旁路極限時,有太多的檔案))。
將專案放在另一個目錄中或重命名檔案或專案以避免與該檔案沖突。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/409047.html
標籤:
上一篇:全域變數的“未決議的外部符號”
