我有一個名爲firstFileStream[50]
的char數組,它使用fstream從infile寫入。如何將char數組轉換爲C++中的字符串?
我想將此char數組轉換爲名爲firstFileAsString
的字符串。如果我寫string firstFileAsString = firstFileStream;
它只寫入數組中的第一個單詞並停在第一個空格或空字符處。如果我寫firstFileAsString(firstFileStream)
我得到相同的輸出。
我該如何寫整個字符數組,以便將字符串中的所有單詞寫入字符串?
下面是代碼的讀取和寫入:
string firstInputFile = "inputText1.txt";
char firstFileStream[50];
ifstream openFileStream;
openFileStream.open(firstInputFile);
if (strlen(firstFileStream) == 0) { // If the array is empty
cout << "First File Stream: " << endl;
while (openFileStream.good()) { // While we haven't reached the end of the file
openFileStream >> firstFileStream;
}
string firstFileAsString = firstFileStream;
}
你怎麼知道有多少個字符複製? – juanchopanza
輸入文件的設置長度包含該字符數,包括空格 – fauliath
@Magis您的意思是你需要複製50個字符,還是一些祕密數字? – juanchopanza