Detect newline byte from filestream

£可爱£侵袭症+ 提交于 2019-12-06 05:52:44
char byte = ins.peek();

Or

if(ins.peek() == '\n') break;

(Edit): You'll want to also check for an eof after your peek(), because some files may not have a ending newline.

I'd like to point out that you might want to consider using a vector<callCentre> instead of a static array. If your input file length exceeds the capacity of the array, you'll walk all over the stack.

The >> operator treats whitespace as a delimiter, and that includes newlines, so it just eats those and you never see them.

You need to read lines and then chop the lines up. The following bit of hackery illustrates the basic idea:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() {
    string line;
    while( getline( cin, line ) ) {
        istringstream is( line );
        string cs;
        is >> cs;
        double vals[10];
        int i = 0;
        while( is >> vals[i] ) {
            i++;
        }

        cout << "CS: " << cs;
        for ( int j = 0; j < i; j++ ) {
            cout << " " << vals[j];
        }
        cout << endl;
    }
}

I would read the file, one line after another and parse each line individually for the values:

std::string line;
while (std::getline(ins, line)) {
  std::istringstream sline(line);
  sline >> aCentre[c].name;
  int i = 0;
  while (sline >> aCentre[c].data[i])
    i++;
  c++;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!