0

I have a text file with movie info, separated by commas. I will provide a line for insight:

8,The Good the Bad and the Ugly,1966,2

I need to take this line, and divide the different pieces by their comma to fit the format of this function:

void addMovieNode(int ranking, std::string title, int releaseYear, int quantity);

The text file's information is in order with the function but I am unclear on how the getline operation operates.

I know I can pass in the text file like

getline("moveInfo.txt", string, ",");

but how will that translate in terms of what is actually going on with the output?

I read the manual on the cplusplus website but that did not help to clarify very much.

1 Answers1

1

You can use a string and stringstream:

#include <sstream>
#include <string>
#include <fstream>

ifstream infile( "moveInfo.txt" );    
while (infile)
{
    std::string line;
    if (!std::getline( infile, line,',' )) break;
    std::istringstream iss(line);
    int ranking, releaseYear, quantity;
    std::string title;
    if (!(iss >> ranking >> title >> releaseYear >> quantity)) { break; } 
}
BugsFree
  • 540
  • 1
  • 6
  • 24
  • 1
    Same comment as above, this breaks with `I, Robot` – coredump Oct 11 '16 at 21:25
  • if (!(iss >> ranking >> title >> releaseYear >> quantity)) What is this line doing? –  Oct 11 '16 at 21:39
  • This looks like the standard format for cin<< or cout>>, is this functionally doing the same thing as each input is broken? –  Oct 11 '16 at 21:40
  • @coredump You're meaning to say that it breaks as in the operation, not that it provides an error? –  Oct 11 '16 at 21:48
  • @Whatamia I didn't test, but the comma inside the name of the movie will be treated as a separator, which gives one more field than expected. Besides, the type being read won't match the expected one: "Robot" won't be read as an integer `releaseYear`, which is what `>>` is supposed to do. I imagine it will crash, ie. runtime error. – coredump Oct 11 '16 at 21:52
  • `if (!(iss >> ranking >> title >> releaseYear >> quantity))` this line extracts the value stored in `iss` and it "redistributes" the values in variables. Yes, it's doing the same thing like `cin >> var`. Pay attention, @coredump is right, if film title contains a "," this breaks. – BugsFree Oct 11 '16 at 21:52
  • If you can change the `.txt` file, you can think to use semicolon like separator (`std::getline("moveInfo.txt", line,";")`), and everything should be fine. I can't think to a film with a semicolon in the title. – BugsFree Oct 11 '16 at 21:57
  • 1
    While your concerns are valid for a normal situation, this is for a school assignment and they provided us with a text file wherein that will not be an issue. This is a small piece of a BST that I'm working on but I couldn't rationale how to separate the outputs. Thank you all! –  Oct 11 '16 at 22:10
  • @BugsFree "target argument deduction/substitution failed: mismatched types. 'std:asic_istream<_CharT, _Tratis>' and const char [15]' while(std::getline("Movies.txt", line, ",")) –  Oct 11 '16 at 22:27
  • @BugsFree I tried it and the same error is being produced. The issue is within the "if(!std:getline(infile, line, ",")) break;" statement. Commenting this out resolved the error. However, I don't know how that's going to effect the output. –  Oct 11 '16 at 22:49
  • I think error was due to the declaration of the delimiter. I set the delim as "char delim = ',' " (note i'm using the ' instead of " as well as declaring it as a char and that seemed to have fixed the issues. –  Oct 11 '16 at 22:52
  • Yes, my fault, I corrected the answer. getline gets like third parameter a char not a string so you have to use the single apex `','`. – BugsFree Oct 11 '16 at 22:55
  • @BugsFree Yep yep! I noticed that and cheered for a moment haha. Thanks for your help! –  Oct 11 '16 at 23:04