有人能告诉我我用getline格式化(cpp)时做错了什么吗 尝试用C++ GETLIN函数格式化。输出将所有内容放在第一个记录编号的名字处,而不是它应该放在的位置

有人能告诉我我用getline格式化(cpp)时做错了什么吗 尝试用C++ GETLIN函数格式化。输出将所有内容放在第一个记录编号的名字处,而不是它应该放在的位置,c++,format,getline,C++,Format,Getline,代码: 请自己运行代码以了解发生了什么,并将txt文件与代码放在同一文件夹中 希望有人能帮助我。 < P>在ISRESRAM中有更容易的单词分离方法,即C++ SnScript流工具: #include <fstream> #include <iostream> #include <sstream> //<-- string stream library using namespace std; //<-- should not be used

代码:

请自己运行代码以了解发生了什么,并将txt文件与代码放在同一文件夹中


希望有人能帮助我。

< P>在ISRESRAM中有更容易的单词分离方法,即C++ SnScript流工具:

#include <fstream>
#include <iostream>
#include <sstream>  //<-- string stream library

using namespace std; //<-- should not be used, use scope std::

int main() {

    const int RANGE = 12;
    string tab[RANGE];
    string temp;       //<--to store each field temporarily
    int i = 0, j = 0;
    ifstream reader("records.txt");
    if (!reader) {
        cout << "Error opening input file" << endl;
        return -1;
    }
    while (getline(reader, temp)) { //<-- read one full line
        stringstream ss(temp); // <-- input to a string stream
        while(ss >> tab[i]){   // <-- passing strings to the string array one by one
            i++;
        }
    }
    reader.close();
    i = 0;
    while (i < RANGE) {
        cout << endl << "Record Number: " << ++j << endl;
        cout << "Forename: " << tab[i++] << endl;
        cout << "Surname: " << tab[i++] << endl;
        cout << "Department: " << tab[i++] << endl;
        cout << "Telephone: " << tab[i++] << endl;
    }
    return 0;
}
#包括
#包括
#包括//请参见

另外,如果文件中的字符串少于12个,那么最后的
while
循环应该只输出实际读入数组的字符串,而不是整个数组。但是,除非您能保证您的文件不会超过12个字符串,否则您应该使用
std::vector
而不是固定数组

另外,我不会在单个循环中交替使用
getline()
分隔符,而是使用外部循环仅读取整行,然后分别从每行读取制表符分隔的值。然后将值存储在struct的数组/向量中,而不是单独存储

尝试类似以下内容:

#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构人
{
字符串名;
串姓;
弦乐部;
字符串电话号码;
};
int main()
{
ifstream阅读器(“records.txt”);
如果(!读卡器)
{

我想,你的问题的根源已经解释清楚了

由于该错误,您正在阅读
选项卡[12]
选项卡[13]
选项卡[13]
选项卡[14]
。当然,这会导致未定义的行为

将循环更改为:

// Read the contents of the file line by line
std::string line;
while (getline( reader, line))
{
   // Process each line's contents.
   std::istringstream str(line);
   getline(str, tab[i++], '\t');
   getline(str, tab[i++], '\t');
   getline(str, tab[i++], '\t');
   getline(str, tab[i++], '\n');
}
确保添加

#include <sstream>
#包括
要加倍确保您没有使用使用越界索引的数组,请添加一个检查

while ( i+4 < RANGE && getline( reader, line))
{
   ...
}
while(i+4
首先

您看到的直接问题是由于您的文件不包含
'\t'
,因此第一个
getline
已经将文件的所有内容读取到
选项卡[0]
(至少这是我在1对1复制文件内容后得到的结果)

你的代码相当困难,因为你在使用变量之前很久就声明了变量,然后又重新使用它们。你有一个固定大小的数组,但当文件中有更多行时,你的代码就会崩溃。同时,将所有内容读入一个普通的字符串数组会使事情变得复杂。访问
forename
或其他字段需要您需要计算数组中的偏移量。最好使用数据结构:

struct file_entry {
    std::string first_name;
    std::string last_name;
    std::string departure;
    std::string phone;
};
然后可以定义输入运算符:

std::istream& operator>>(std::istream& in,file_entry& fe) {
    return in >> fe.first_name >> fe.last_name >> fe.departure >> fe.phone;
};
并使用
std::vector
存储文件中的条目:

int main() {
    std::string contents{"John    Smith    Sales    555-1234\n"
                         "Mary    Jones    Wages    555-9876\n"
                         "Paul    Harris   Accts    555-4321\n"};

    std::stringstream reader{contents};
    std::vector<file_entry> data;    
    std::string line;
    while (std::getline(reader,line)) {
        file_entry fe;
        std::stringstream{line} >> fe;
        data.push_back(fe);
    }

    for (const auto& fe : data) {
        std::cout << "Forename: " << fe.first_name << '\n';
        std::cout << "Surname: " << fe.last_name << '\n';
        std::cout << "Department: " << fe.departure << '\n';
        std::cout << "Telephone: " << fe.phone << '\n';
    }
}
intmain(){
std::字符串内容{“John Smith Sales 555-1234\n”
“玛丽·琼斯工资555-9876\n”
“保罗·哈里斯账户555-4321\n”};
std::stringstream读取器{contents};
std::矢量数据;
std::字符串行;
while(std::getline(读卡器,行)){
文件输入fe;
std::stringstream{line}>>fe;
数据。推回(fe);
}
用于(常数自动和fe:数据){

std::您是否希望在此处使用提取运算符(
>
)而不是
getline
。例如:
reader>>选项卡[i++];
请在问题中包含输出。我得到的结果似乎与其他人对问题的解释不同
std::istream& operator>>(std::istream& in,file_entry& fe) {
    return in >> fe.first_name >> fe.last_name >> fe.departure >> fe.phone;
};
int main() {
    std::string contents{"John    Smith    Sales    555-1234\n"
                         "Mary    Jones    Wages    555-9876\n"
                         "Paul    Harris   Accts    555-4321\n"};

    std::stringstream reader{contents};
    std::vector<file_entry> data;    
    std::string line;
    while (std::getline(reader,line)) {
        file_entry fe;
        std::stringstream{line} >> fe;
        data.push_back(fe);
    }

    for (const auto& fe : data) {
        std::cout << "Forename: " << fe.first_name << '\n';
        std::cout << "Surname: " << fe.last_name << '\n';
        std::cout << "Department: " << fe.departure << '\n';
        std::cout << "Telephone: " << fe.phone << '\n';
    }
}