[英]Extract Whitespace in addition to text from input file, C++?
我必須編寫一個將從文件中讀取輸入的函數。 設置文件:整個文件中一個字符,空格,單詞,空格,例如:
A space 1 space 2 space...
等等
我需要提取一個字符后的空格,而不要提取單詞后的空格。
我該怎么做呢? 我是否應該這樣做,以便函數編寫空白而不是提取空白?
另外,我正在將此信息導入到2維char數組中。 我會在嘗試將整數寫入char數組時遇到問題嗎?
像這樣的東西?
#include <iostream>
#include <fstream>
int main() {
char myChar;
char theWS;
std::string word;
std::ifstream in("example.txt");
while(in >> myChar >> std::noskipws >> theWS >> word >> std::skipws) {
std::cout << myChar << theWS << word << '\n';
}
}
現在,您應該已經了解了分詞器的概念。 這就是您需要的結構。
您可以將整數寫入字符數組。 由於C和C ++始終將ascii字符表示為小數字,因此處理它們很容易。 對應於特定字符的數字值的一些示例:“ 0” => 48,“ 1” => 49,...,“ A” => 65,“ B” => 66等。
請查看http://www.asciitable.com/ ,以獲取完整的ascii字符集及其相應的值。
這還使您可以對字符(例如'A' + 1 => 'B'
進行數學運算,以及在數字和字符之間進行轉換(char) 65 => 'A'
好吧,如果您可以確定可以嘗試的格式:
#include <fstream>
using namespace std;
void file_read_func()
{
string input;
string ret;
ifstream file;
file.open("full file path");
while (!file.eof())
{
file >> input;
if (!file.eof())
{
ret += input;
file >> input;
ret += input + " ";
}
}
return ret;
}
文件流用空格和換行分隔,因此
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.