2016-02-25 2 views
0

Я работаю над этой библиотечной системой в C++ для моего класса программирования, и я не могу заставить ifsteam работать с запятыми, разделяя разделы. Я сделал так, чтобы вы могли вводить название книги, издателя, автора, ISBN, тему, страницы и имя, которое вы хотите, чтобы файл имел. Все это прекрасно работает. Я сделал функцию с именем rBook, чтобы вернуть информацию из книги и сделал ее так, что нашел путь к файлу. Секции разделены запятыми, как я сказал, но каждый раз, когда я запускаю его с помощью этого кода, он не печатает все в списке с запятыми.ifstream csv запись из файла с запятыми

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(); 

Этот код предназначен для возврата информации из файла.

+1

Не могли бы вы добавить несколько строк из входного файла и образец вывода вы получаете? – Marco

ответ

0

Я предполагаю, что у вас есть проблемы при использовании 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 
Смежные вопросы