>文件名; filePath=“C:/Users/Jsadlowski/Desktop/Books/”+fileName+“.txt”; ifstream myfile(文件路径); 系统(“cls”); while(getline(myfile,line)) { int comma1=查找第n行(第1行,“,”); int comma2=查找第n行(第2行,“,”); int comma3=查找第n行(第3行,“,”); int comma4=查找第n行(第4行,“,”); int comma5=查找第n行(第5行,“,”); //cout,c++,csv,ifstream,C++,Csv,Ifstream" /> >文件名; filePath=“C:/Users/Jsadlowski/Desktop/Books/”+fileName+“.txt”; ifstream myfile(文件路径); 系统(“cls”); while(getline(myfile,line)) { int comma1=查找第n行(第1行,“,”); int comma2=查找第n行(第2行,“,”); int comma3=查找第n行(第3行,“,”); int comma4=查找第n行(第4行,“,”); int comma5=查找第n行(第5行,“,”); //cout,c++,csv,ifstream,C++,Csv,Ifstream" />

ifstream csv从带有逗号的文件写回 我在C++中为我的编程类工作这个库系统,我似乎无法用I逗号来处理IfStudio,将各部分分开。我这样做是为了让你可以输入书名、出版商、作者、ISBN、主题、页面以及你想要的文件名。这一切都很好。我创建了一个名为rBook的函数来返回书的信息,并使其能够找到文件路径。正如我所说,这些部分用逗号分隔,但每次我用这段代码运行它时,它不会用逗号打印列表中的所有内容 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(); void rBook(){ 字符串行、文件路径、文件名; cin>>文件名; filePath=“C:/Users/Jsadlowski/Desktop/Books/”+fileName+“.txt”; ifstream myfile(文件路径); 系统(“cls”); while(getline(myfile,line)) { int comma1=查找第n行(第1行,“,”); int comma2=查找第n行(第2行,“,”); int comma3=查找第n行(第3行,“,”); int comma4=查找第n行(第4行,“,”); int comma5=查找第n行(第5行,“,”); //cout

ifstream csv从带有逗号的文件写回 我在C++中为我的编程类工作这个库系统,我似乎无法用I逗号来处理IfStudio,将各部分分开。我这样做是为了让你可以输入书名、出版商、作者、ISBN、主题、页面以及你想要的文件名。这一切都很好。我创建了一个名为rBook的函数来返回书的信息,并使其能够找到文件路径。正如我所说,这些部分用逗号分隔,但每次我用这段代码运行它时,它不会用逗号打印列表中的所有内容 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(); void rBook(){ 字符串行、文件路径、文件名; cin>>文件名; filePath=“C:/Users/Jsadlowski/Desktop/Books/”+fileName+“.txt”; ifstream myfile(文件路径); 系统(“cls”); while(getline(myfile,line)) { int comma1=查找第n行(第1行,“,”); int comma2=查找第n行(第2行,“,”); int comma3=查找第n行(第3行,“,”); int comma4=查找第n行(第4行,“,”); int comma5=查找第n行(第5行,“,”); //cout,c++,csv,ifstream,C++,Csv,Ifstream,我猜您在使用substr函数时遇到了问题,它最多可读取两个参数: 要复制的第一个字符的位置 要复制的字符数(这是可选的,如果是ommited 它一直延伸到生产线的末端) 因此,例如,如果您有以下行,则应为逗号1至5的值: 5 15 22 27 35 title,publisher,author,ISBN,subject,pages 在代码中,示例如下所示: string line=“标题、出版商、作者、ISBN、主题、页面”; //我猜这就是fi

我猜您在使用substr函数时遇到了问题,它最多可读取两个参数:

  • 要复制的第一个字符的位置
  • 要复制的字符数(这是可选的,如果是ommited 它一直延伸到生产线的末端)
  • 因此,例如,如果您有以下行,则应为逗号1至5的值:

         5         15     22   27      35
    title,publisher,author,ISBN,subject,pages
    
    在代码中,示例如下所示:

    string line=“标题、出版商、作者、ISBN、主题、页面”;
    //我猜这就是find\n函数返回的结果
    int comma1=5;
    int comma2=15;
    int comma3=22;
    int comma4=27;
    int comma5=35;
    字符串标题、出版商、作者、ISBN、主题、页面;
    /**
    *使用您的方法会在某些变量中得到不正确的值,
    *第一个是ok,因为它从位置0开始,然后抓取5个字符,
    *第二个命令是从16位开始,抓取5个字符
    *获取不正确的值,依此类推
    */
    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,主题,pa
    ISBN=line.substr(comma4+1,comma3);//substr(28,22)->主题,页面
    Subject=line.substr(comma5+1,comma4);//substr(36,27)->pages
    Pages=line.substr(line.length(),comma5+1);//substr(40,35)->
    /**
    *一个可能的解决方案是如下所示对其进行更改,
    *指示起始逗号的位置,然后
    *使用以下逗号的值计算长度
    */
    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
    
    您能从输入文件中添加几行内容并获得输出的示例吗?