c++ - 从数据文件c++读取时第一个字母被删除

标签 c++ arrays

有人可以解释为什么从数据文件中读入时只删除第一个字母,但只删除数组的 1/2/3 部分而不是 0 部分吗? (抱歉真的不知道怎么解释)(我只会包括我得到的部分以及数据文件)

我得到了什么

GoogleyleSmith01@gmail.comyleman27 安全问题:带 watch 的白兔 Deviantartragonmaster27andalfthegreyNULL

它应该是什么

GoogleKyleSmith01@gmail.comKyleman27 安全问题:Whiterabbitwithawatch DeviantartDragonmaster27GandalfthegreyNULL

和原始数据文件

Google;KyleSmith01@gmail.com;Kyleman27;安全问题:带 watch 的白兔; Deviantart;Dragonmaster27;GandalftheGrey;空;

我不会包含所有代码,因为它不应该与这个问题相关

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

const int NCOLS = 4;
const int NROWS = 10;

void description_and_options(string data[][NCOLS], int count[NCOLS]);
void available_options();
void view_line_data(int choice,string data[][NCOLS]);

int main()
{
    ifstream file_name;//create the new file
    string user_input_file;//the files name inputed by the user 
    int stringlength;
    string read_in_array[NROWS][NCOLS];
    string line;
    int counter[10] = { 1,2,3,4,5,6,7,8,9,10 };
    string user_option_choice;
    string small_exit = "x";
    string large_exit = "X";
    int view_choice;

    cout << "Enter the name of the input file: ";

    cin >> user_input_file;

    if (user_input_file.length() > 4)// check to see if its more than 4 in length
    {
        stringlength = user_input_file.length(); //saves length
        if (user_input_file.substr(stringlength - 4, 4) == ".txt")//checks to see if its .dat
        {
            file_name.open(user_input_file.c_str());
            if (file_name.fail())
            {
                cerr << "The file " << user_input_file << " failed to open.\n";//tells user if it fails
                exit(1);
            }
        }
    }
    else
    {
        user_input_file += ".txt";//adds .dat to non .dat 
        file_name.open(user_input_file.c_str());
    }

    if (file_name.fail())
    {
        cout << "File failed to open" << endl;
        system("PAUSE");
        exit(1);
    }
    for (int row = 0; row <= 9; row++)
    {
        for (int col = 0; col < 4; col++)
        {
            if (getline(file_name, line, ';'))
            {
                file_name.ignore(1, '\n');
                read_in_array[row][col] = line;
                cout << read_in_array[row][col];
            }
        }
        cout << endl;
    }
    //[updown][leftright]
    file_name.close();

有没有办法在不完全更改代码的情况下解决这个问题?

最佳答案

它忽略了第一个字符,因为你告诉它

file_name.ignore(1, '\n');

将在每次调用 getline 后忽略流中的第一个字符。看起来您这样做是因为您认为文件中的 ; 仍然存在。关于 getline,您需要记住的是它会丢弃您使用的定界符。这意味着它将一直读取直到找到 ;,然后将 ; 扔掉。这意味着您无需忽略它,因为它已不存在。

只是删除对 ignore 的调用并不足以解决问题。由于您正在尝试解析整行,我们需要做的是将该行读入 stringstream,然后在流上调用 getline 以获取各个部分。这是因为只要阅读 ; 就会捕获换行符。

快速重构你的代码会给你一些应该看起来像的东西

for (int row = 0; row <= 9; row++)
{
    std::string temp;
    std::getline(file_name, temp)
    std::stringstream ss(temp)
    for (int col = 0; col < 4; col++)
    {
        if (getline(ss, line, ';'))
        {
            read_in_array[row][col] = line;
            cout << read_in_array[row][col];
        }
    }
    cout << endl;
}

关于c++ - 从数据文件c++读取时第一个字母被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41146455/

相关文章:

c++ - 奇怪的 lambda 问题(编译器错误?)

c++ - 定义带有添加前缀的新宏的宏

c++ - 解引用void指针以将值分配给struct

ios - AnchroPoint x :0, y:0 不适用于方程式

c - C 中不使用函数查找字符串的最后一个字符

c - 交换数组中的元素时出现错误输出

c++ - 无法在ubuntu中编译opencv程序

java - 如何检查数组的所有元素是否不等于某个值? (例如不为空)

javascript - 迭代嵌套对象并使用 Lodash 连接到字符串

c++ - 文件输入和处理数据