views:

61

answers:

2

I've got a few questions concerning text files,list and strings.

I wonder if it is possible to put in a code which reads the text in a textfile,and then using "string line;" or something else to define each new row of the text and turn all of them into one list. So I can sort the rows, remove a row or two or even all of them or search through the text for a specific row.

+1  A: 

The language wasn't specified but I would imagine the design behind it would be the same:

  • Read in your data and store each piece of text in some 'string' structure
  • Store each piece of data in a List type object (eg, std::vector in c++)
  • Define or utilize some interface to perform the requested operations

I suppose then it will be just a matter of which language. So yes it is possible.

Robb
+1  A: 

In C++, you'd typically do this with an std::vector:

std::vector<std::string> data;

std::string temp;

while (std::getline(infile, temp))
    data.push_back(temp);

Sorting them would then look like:

std::sort(data.begin(), data.end());

Deleting row N would look like:

data.erase(data.begin() + N);
Jerry Coffin