从数据文件C 读取时,将删除第一个字母

First letters being erased when reading from data file c++

本文关键字:删除 第一个 数据 文件 读取      更新时间:2023-10-16

有人可以解释为什么从数据文件读取时只有第一个字母被删除,而仅在数组的1/2/3部分而不是0部分?(对不起,真的不知道如何解释)(我只包括我所获得的部分以及数据文件)

我得到的

googleylesmith01@gmail.comymerman27Ecurity问题:与手表的白兔子deviantartragonmaster27andalfthegreynull

应该是

googlekylesmith01@gmail.comkyleman27securityquestion:whiterabbitwithawatchDeviantArtdraganmaster27gandalfthegreynull

和原始数据文件

google; kylesmith01@gmail.com; kyleman27;安全问题:带有手表的白兔子;DeviantArt; Dragonmaster27; Gandalfthegrey;null;

我不会包含所有代码,因为它应该与此问题无关

#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以获取各个部分。这是因为仅阅读到;将捕获Newline。

快速重构代码会为您提供看起来像

的东西
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;
}

您正在错误地使用ifstream :: ignore()。

从输入序列中提取字符并丢弃它们,直到 提取了n个字符,或者一个字符的比较等于 特尔姆。

file_name.ignore(1, 'n');始终驳回首字母。在您的情况下,"之后的首字母排队。

file_name.ignore(1, 'n');将使流从输入中忽略一个字符。

通过阅读您的代码:

  • 对于您所谓的" 0部分",在循环中的第一个getline之前尚未调用ignore
  • 对于"零件1/2/3",ignore语句使流程跳过下一个字符
  • 对于其余部分,有一个空间或一个" n",以使可读字母没有被跳过。