我正在尝试编写一个函数,它读取txt文件中的各行并将它们存储在字符串数组中。除了读取空白行时,该函数可以正常工作。例如:C++流不读取空行
功能
ifstream flinput("somefile.txt")
string line;
while(getline(flinput, line)) {
//Add line to array
所以问题是,如果文本文件看起来像这样。
Line1 Some Text blar blar blar
\n
Line3 Some Text blar blar blar
\n
Line5 Some Text blar blar blar
该数组最终看起来像这样。
array[0] = "Line1 Some Text blar blar blar"
array[1] = "Line3 Some Text blar blar blar"
array[2] = "Line5 Some Text blar blar blar"
当它看起来像这样。
array[0] = "Line1 Some Text blar blar blar"
array[1] = ""
array[2] = "Line3 Some Text blar blar blar"
array[3] = ""
array[4] = "Line5 Some Text blar blar blar"
我在做什么错?
由于
更简单的方法可以是将到阵列之前检查一个空字符串。如果字符串为空,则向数组追加一个“\ n”字符串。 – 2010-01-05 18:01:30