c++ - 从文件流中检测换行字节

标签 c++ newline text-files fstream

我正在尝试从包含组织名称(无空格)和 float 整数的文本文件中收集信息。我想将这些信息存储在一个数组结构中。

目前我遇到的问题是收集信息。这是文本文件的示例:

CBA 12.3 4.5 7.5 2.9 4.1

TLS 3.9 1 8.6 12.8 4.9

每个组织最多可以有 128 个不同的编号,文本文件中最多可以有 200 个组织。

到目前为止,这是我的结构:

struct callCentre
{
char name[256];
float data[20];
};

我的主要内容:

int main()
{
callCentre aCentre[10];
getdata(aCentre);
calcdata(aCentre);
printdata(aCentre);
return 0;
}

和 getdata 函数:

void getdata(callCentre aCentre[])
{
ifstream ins;
char dataset[20];

cout << "Enter the name of the data file: ";
cin >> dataset;

ins.open(dataset);

if(ins.good())
{
    while(ins.good())
    {
        ins >> aCentre[c].name;
        for(int i = 0; i < MAX; i++)
        {
            ins >> aCentre[c].data[i];
            if(ins == '\n')
                break;
        }
        c++;
    }
}
else
{
    cout << "Data files couldnt be found." << endl;
}
ins.close();
}

我试图在我的 getdata 函数中实现的是:首先将组织名称存储到结构中,然后将每个 float 读入数据数组,直到程序检测到换行字节。但是,到目前为止,我对换行符字节的检查不起作用。

假设变量 cMAX 已经定义。

我应该如何正确处理这个问题?

最佳答案

>> 运算符将空格视为定界符,其中包括换行符,因此它只会吃掉那些而您永远看不到它们。

关于c++ - 从文件流中检测换行字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/640983/

相关文章:

java - 换行符与系统换行符不同

c - scanf ("%[^\n]") 和 scanf ("%s") 之间的区别?

c - 在 C 中,我应该如何读取文本文件并打印所有字符串

c++ - 优化编译器消除错误

c++ - 如何在VC++中使用.lib、.dll和.exp文件?

c++ - LNK2019 (VS 2008) 使用模板函数指针完整实现模板函数

ios - 如何将GTFS文本文件导入iOS应用?

c++ - 使用 boost::lockfree::spsc_queue 时出现编译错误(是否是 boost 中的错误?)

sql - PostgreSQL 中不需要的换行符翻译

c++ - 从 C++ 文本文件中读取坐标