ifstream csv 用逗号从文件写回

ifstream csv writing back from a file with commas

我正在使用 C++ 开发此库系统以进行编程 class,但我似乎无法让 ifsteam 使用逗号分隔各个部分。我这样做是为了让您可以输入书名、出版商、作者、ISBN、主题、页码以及您希望文件具有的名称。这一切都很好。我做了一个名为rBook的函数,用来return书的信息,让它找到文件路径。正如我所说,这些部分用逗号分隔,但每次我使用此代码 运行 它都不会用逗号打印列表中的所有内容。

void rBook(){
string line, filePath, fileName;
cin >> fileName;
filePath="C:/Users/Jsadlowski/Desktop/Books/"+fileName+".txt";
ifstream myfile (filePath);
system("cls");
while ( getline (myfile,line) )
{
    int comma1 = find_Nth(line, 1, ",");
    int comma2 = find_Nth(line, 2, ",");
    int comma3 = find_Nth(line, 3, ",");
    int comma4 = find_Nth(line, 4, ",");
    int comma5 = find_Nth(line, 5, ",");

    //cout << comma1 << " " << comma2 << " " << comma3 << " " << comma4 << " " << comma5;

    string Title, Publisher, Author, ISBN, Subject,Pages;

    Title = line.substr(0,comma1);
    Publisher = line.substr(comma2 + 1,comma1);
    Author = line.substr(comma3 + 1,comma2);
    ISBN = line.substr(comma4 + 1,comma3);
    Subject = line.substr(comma5 + 1,comma4);
    Pages = line.substr(line.length(), comma5 + 1);
    cout << Title << endl;
    cout << Publisher << endl;
    cout << Author << endl;
    cout << ISBN << endl;
    cout << Subject << endl;
    cout << Pages <<  endl;

    cout << line << endl;
}
myfile.close();

此代码用于return文件中的信息。

我猜你在使用 substr 函数时遇到了问题,它最多需要 2 个参数:

  1. 要复制的第一个字符的位置
  2. 要复制的字符数(这是可选的,如果省略 它上升到行尾)

因此,例如,如果您有以下行,它们应该是逗号 1 到 5 的值:

     5         15     22   27      35
title,publisher,author,ISBN,subject,pages

在代码中,示例如下:

string line = "title,publisher,author,ISBN,subject,pages";

//this is what I guess is returned by your find_Nth function
int comma1 = 5;
int comma2 = 15;
int comma3 = 22;
int comma4 = 27;
int comma5 = 35;

string Title, Publisher, Author, ISBN, Subject, Pages;

/**
 * using your approach will get incorrect values in some of your variables,
 * the first is ok, as it start on position 0 and then grab 5 characters,
 * the second says to start in position 16 and grab 5 characters which 
 * gets an incorrect value, and so on
 */
Title = line.substr(0,comma1);                  //substr(0, 5)   -> title
Publisher = line.substr(comma2 + 1,comma1);     //substr(16, 5)  -> autho 
Author = line.substr(comma3 + 1,comma2);        //substr(23, 15) -> ISBN,subject,pa
ISBN = line.substr(comma4 + 1,comma3);          //substr(28, 22) -> subject,pages
Subject = line.substr(comma5 + 1,comma4);       //substr(36, 27) -> pages
Pages = line.substr(line.length(), comma5 + 1); //substr(40, 35) -> 

/**
 * A possible solution can be to change it like the following, 
 * to indicate the position of the starting comma and then 
 * calculate the length using the value of the following comma
 */
Title = line.substr(0, comma1);                           //substr(0, 5)  -> title
Publisher = line.substr(comma1 + 1, comma2 - comma1 - 1); //substr(6, 9)  -> publisher 
Author = line.substr(comma2 + 1, comma3 - comma2 - 1);    //substr(16, 6) -> author
ISBN = line.substr(comma3 + 1, comma4 - comma3 - 1);      //substr(23, 4) -> ISBN
Subject = line.substr(comma4 + 1, comma5 - comma4 - 1);   //substr(28, 7) -> subject
Pages = line.substr(comma5 + 1);                          //substr(36)    -> pages