i have a text file with known format. which is as fallow
code ItemName Quantity
001 ABC 45
002 BCA 5
003 ACB 10


i want to read and write the line randomly according to the need of a user.
for example if user want to read the data of Item 002 he just enter the code and read the whole line. not only read he can be able to change the Quantity of Item if any item sail out.
i write the following incomplete code:

#include<iostream.h>
#include<fstream.h> 
 
main()
{
  fstream infile;
  char head[200000];
  char body[40];
  long val;
  long code;
  long SelItem;
   
  infile.open("recor.txt");
  if(!infile)
  {
  cout<<"file canot be open";
  exit(1);           
  }
  
cout<<"enter item code :"<<'\n';
cin>>code;             
  
cout<<"entre code item detail is as follow ::"<<'\n';
cout<<"     code"<<"      Name"<<"        Quantity";
//get line function to read required data of enter code//
[B]//hair i nead the help//[/B]






  
cout<<"Enter No Of Itmes Sold :";
cin>>SlItem;
//now subtracting the enter value an Rewright quantity in file//
[B]//hair i nead the help//[/B]







cout<<"Enter Code Item Detail After transaction ::"<<'\n';
cout<<"     code"<<"      Name"<<"        Quantity";
//read the same line and print it after transaction//
[B]//i need help here//[/B]








infile.close();

cout<<'\n';
cout<<'\n';

system("pause");


}

Are you required to update the file with each edit? It's often more practical (barring very large files) to keep the file in memory and work with the copy in memory. Then when a convenient time comes to save the edits, you can overwrite the file. To read the file into memory, for example, you might do this:

#include <fstream>
#include <iostream>
#include <istream>
#include <map>
#include <ostream>
#include <sstream>
#include <string>

class item {
    std::string _code;
    std::string _name;
    int _quantity;
public:
    item(const std::string& code = "", const std::string& name = "", int quantity = 0)
        : _code(code), _name(name), _quantity(quantity)
    {}

    int quantity() const { return _quantity; }
    void quantity(int new_quantity) { _quantity = new_quantity; }

    friend std::ostream& operator<<(std::ostream& out, const item& rhs)
    {
        return out<<'('<< rhs._code <<','<< rhs._name <<','<< rhs._quantity <<')';
    }
};

std::map<std::string, item> load_items(std::istream& in, bool has_header = false)
{
    std::string line;

    if (has_header) {
        // Skip the header line
        std::getline(in, line);
    }

    std::map<std::string, item> items;

    while (std::getline(in, line)) {
        std::istringstream iss(line);
        std::string item_code;
        std::string name;
        int quantity;

        // Just ignoring stream errors for this example
        if (iss>> item_code >> name >> quantity) {
            // Also ignoring duplicates
            items[item_code] = item(item_code, name, quantity);
        }
    }

    return items;
}

int main()
{
    std::ifstream in("test.txt");

    if (in) {
        std::map<std::string, item> items = load_items(in, true);
        std::map<std::string, item>::const_iterator it = items.begin();
        std::map<std::string, item>::const_iterator end = items.end();

        while (it != end) {
            std::cout<< it->second <<'\n';
            ++it;
        }
    }
}

The problem here is that files are not random access, they're serial. You can achieve the effect of random access, but it's both awkward and inefficient.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.