0

The Text file looks like this:

Apple,Itunes,1,7.3   
Microsoft,Windows Media Player,1,10

.... and so on.....

The parse method is:

private IApplication parseLineToApp(String lineFromTxtFile) {
  Scanner lineScanner = new Scanner(lineFromTxtFile);
  lineScanner.useDelimiter(",");

  return new Application(lineScanner.next(), lineScanner.next(), lineScanner.nextInt(), lineScanner.next());
 }

I want to do the same thing in c++ to create a new application(). Note: I already have an application class, and need to add that application to a repository which is a collection of applications

Thanks in advance :)

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
Jon Smith
  • 11
  • 3

3 Answers3

1

You can create a vector of strings using Boost and the STL.

// given std::string lineFromTxtFile
std::vector<std::string> scanner;
boost::split (scanner, lineFromTxtFile, boost::is_any_of(","));

return new Application (scanner[0], scanner[1], scanner[2], scanner[3]);

If you want scanner[2] to be an integer, there's

boost::lexical_cast<int> (scanner[2])
chrisaycock
  • 36,470
  • 14
  • 88
  • 125
0

There is no Scanner class in the C++ standard library, the direct translation is not possible. Of course, you can always implement your own.

Alas, you could create a split function:

unsigned int split(const std::string &txt, std::vector<std::string> &strs, char ch)
{
    size_t pos = txt.find( ch );
    size_t initialPos = 0;
    strs.clear();

    if ( pos != std::string::npos ) {
        // Decompose each statement
        strs.push_back( txt.substr( initialPos, pos - initialPos + 1 ) );
        initialPos = pos + 1;

        pos = txt.find( ch, initialPos );
        while( pos != std::string::npos ) {
            strs.push_back( txt.substr( initialPos, pos - initialPos + 1 ) );
            initialPos = pos + 1;

            pos = txt.find( ch, initialPos );
        }
    }
    else strs.push_back( txt );

    return strs.size();
}

Then you could rewrite your code as:

std::vector<std::string> scanner;
split( lineFromTxtFile, scanner, ',' );

return new Application (scanner[0], scanner[1], scanner[2], scanner[3]);

Hope this helps.

Baltasarq
  • 12,014
  • 3
  • 38
  • 57
  • hey can you please explain what that is doing so i can get a grasp on this? – Jon Smith Dec 03 '10 at 18:56
  • i think i get it i just dont know if it would make a difference if the function was void – Jon Smith Dec 03 '10 at 19:04
  • I am having an error with with the two lines that have npos the compiler says it will always be true – Jon Smith Dec 03 '10 at 19:13
  • The function just updates a pair of variables, pos and initialPos. InitialPos will be 0 at the beginning, and pos will be intialised for the first appearance of the delimiter. If it is found, the substring between initialPos and pos is stored in the vector, and initialPos is now pos, and so on... The function could return nothing, instead, it returns the number of elements the string has been splitted into. – Baltasarq Dec 03 '10 at 19:43
  • std::string::npos is -1, just marks if the index is valid or not. When find searches for a value, it can return npos or the actual position in the string the value is. I guess the problem has to do with using unsigned int, it should be size_t instead. I'll fix this. – Baltasarq Dec 03 '10 at 19:47
0

File operations can be done a few different ways in C/C++. A C++-style approach could look like the following:

std::vector<Application> ApplicationList;

std::ifstream fin("myapplist.txt");  // open a file stream
while (!fin.eof())   // while this isn't the end of the file
{
    char buffer[256] = {0};
    fin.getline(buffer, 256);    // read the current line of text into the buffer
    std::vector<std::string> scanner;
    boost::split(scanner, buffer, boost::is_any_of(",")); // split the buffer and store the results in the scanner vector
    ApplicationList.push_back(Application(scanner[0], scanner[1], scanner[2], scanner[3]); // add the application to the application list
}
fin.close();

Assuming your Application struct/class is copyable (otherwise you'd want to use pointers in your ApplicationList).

Zac Howland
  • 15,777
  • 1
  • 26
  • 42