Question

AccountNumber Type Amount

15 checking 52.42

23 savings 51.51

11 checking 12.21

is my tab delmited file

i would like to be able to search for rows by the account number. say if i put in 23, i want to get that specific row. how would id do that?

also more advance, if i wanted to change a specific value, say amount 51.51 in account 23. how do i fetch that value and replace it with a new value?

so far im just reading in row by row

string line; ifstream is("account.txt");

 if (is.is_open())
 {
while (std::getline(is, line))  // read one line at a time
{
    string value;
    string parseline;
    std::istringstream iss(line);

    getline(line, parseline);
    cout << parseline << endl;  // do something with the value
    while (iss >> value)    // read one value at at time from the line
    {
        //cout << line << "   ";    // do something with the value
    }
}
is.close();
 }
 else
     cout << "File cant be opened" << endl;

return 0;
Was it helpful?

Solution

Given that each line is of variable length there is no way to index to particular row without first parsing the entire file.

But I suspect your program will want to manipulate random rows and columns. So I'd start by parsing out the entire file. Put each row into its own data structure in an array, then index that row in the array.

You can use "strtok" to split the input up into rows, and then strtok again to split each row into fields.

OTHER TIPS

If I were to do this, I would first write a few functions that parse the entire file and store the data in an appropriate data structure (such as an array or std::map). Then I would use the data structure for the required operations (such as searching or editing). Finally, I would write the data structure back to a file if there are any modifications.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top