[英]C++: possible Xcode (Mac) problem. Can't read in numbers from external file using getline()
我是Mac Xcode(3.2.2)用戶,使用GCC 4.2編譯器,對C ++經驗不足,對編譯器一無所知。
我需要從外部文件中讀取數字,並將其存儲在整數向量中。 我使用流和getline()編寫了代碼。 我可以編譯代碼,並且運行時不會出錯,但是沒有輸出。 不過,它適用於朋友(非Mac和非GCC 4.2編譯器)。
我進行了一些谷歌搜索,發現了幾篇有關Xcode 3.2.1中可能的編譯器(gcc,Apple)問題的帖子,這些問題可能與我的問題有關(例如,此問題:使用getline()的C ++打印:釋放的指針未分配在XCode。但是,我有Xcode 3.2.2,我嘗試了建議的修復程序(與在_GLIBCXX_FULLY_DYNAMIC_STRING中添加_GLIBCXX_FULLY_DYNAMIC_STRING到(1)目標設置窗口或(2)作為預處理器宏之前)有關,但是我的問題仍然沒有解決。
所以我現在不知道我的問題是什么-是否與人們在Xcode 3.2.1中遇到的相同的編譯器問題,但是在3.2.2中需要不同的修復程序。 或者我的代碼中是否還有其他問題。
如果有人可以提供一些建議,那就太好了。 沒有建議太簡單了。 如果還有另一種從外部文件導入數字的方法,我很想知道。 此外,有人知道是否有可能從.dat文件而不是Mac上的.txt文件導入數據嗎?
謝謝。
自從第一次發布以來,我已經包括了wilhelmtell的編輯。
目標:將文本文件中的數字讀入稱為“結果”的整數向量。
1)數據文件test.dat中的數據最初看起來像,
//測試
測試
'1''2''3'
//文本文件中還有一些標簽和注釋,這些標簽和注釋是我不想讀的,但無法從文件中刪除。
#include <algorithm>
#include <cctype>
#include <iterator>
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
// Edited: added struct (wilhelmtell's advice)
struct not_digit_and_not_whitespace {
bool operator()(char c) const {
return ! std::isdigit(c) && ! std::isspace(c);
}
};
int main()
{
system("pwd");
std::string line;
const std::string fileName = "/Users/me/test.dat";
ifstream theStream( fileName.c_str() );
if( ! theStream )
std::cerr << "Error opening file test.dat\n";
std::getline( theStream, line );
// line.erase(remove( line.begin(), line.end(), '\'' ), line.end() ); // Edited (wilhelmtell's advice)
line.erase(remove_if( line.begin(), line.end(),not_digit_and_not_whitespace()), line.end() ); // Edited: added (wilhelmtell's advice)
std::istringstream myStream( line );
std::vector<int> numbers((std::istream_iterator<int>(myStream)), std::istream_iterator<int>());
std::copy(numbers.begin(), numbers.end(),
std::ostream_iterator<int>(std::cout, "\n"));
/* int temp; // Edited: wilhemtell suggested using iterators instead.
while ( myStream >> temp ){
numbers.push_back( temp );
std::cout << temp << std::endl;
}*/
return 0;
}
這可能對您有用。 它在嘗試時使用getline,但使用'\\'字符作為行定界符。 忽略一切,直到第一個'。 假設所有內容,直到下一個'是數字的一部分。 繼續直到邏輯失敗(可能是由於文件結尾)。
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
int main()
{
system("pwd");
std::string line;
const std::string fileName = "test.dat";
std::ifstream theStream( fileName.c_str() );
if( ! theStream )
std::cerr << "Error opening file test.dat\n";
std::vector<int> numbers;
while (true)
{
// get first '
std::getline(theStream, line, '\'');
// read until second '
std::getline(theStream, line, '\'');
std::istringstream myStream( line );
int i;
myStream >> i;
if (myStream.fail())
break;
numbers.push_back(i);
}
std::copy(numbers.begin(), numbers.end(),
std::ostream_iterator<int>(std::cout, "\n"));
}
我的猜測是您的test.dat文件不在工作目錄中。 要驗證您可以測試流,請執行以下操作:
if( ! theStream )
std::cerr << "Error opening file test.dat\n";
在XCode中檢查項目屬性,以查看工作目錄是什么,然后將文件放在此處。
如果文件中包含的數據不能解析為整數,則流的int
解析將失敗。 確保您讀取的行除整數和空格外沒有其他內容:
#include <cctype> // algorithm iterator ...
struct not_digit_and_not_whitespace {
bool operator()(char c) const {
return ! std::isdigit(c) && ! std::isspace(c);
}
};
// ...
line.erase(remove_if( line.begin(), line.end(),
not_digit_and_not_whitespace()),
line.end() );
另外,您可以簡化代碼。 首先,如果包含<iostream>
則不需要<istream>
。 其次,可以使用std::vector
的范圍構造函數獲取整數。
#include <algorithm> // iterator fstream iostream vector string sstream
int main()
{
std::string line;
std::ifstream theStream("test.dat");
std::getline(theStream, line);
line.erase(std::remove(line.begin(), line.end(), '\'' ),
line.end());
std::istringstream myStream(line);
std::vector<int> numbers((std::istream_iterator<int>(myStream)),
std::istream_iterator<int>());
std::copy(numbers.begin(), numbers.end(),
std::ostream_iterator<int>(std::cout, "\n"));
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.