how to create buffer in c++ to create new file

19,011

In C++, you allocate memory to create buffers like this:

char* buffer = new char[length];

The problem with your code is that you used () instead of [].

When you want to free the memory of those buffers, you use the delete[] operator:

delete[] buffer;

Also, you are reading correctly from the file, but not in the way you expect. It's valid syntax and all, but the problem is, you're overwriting the data in your buffer.

You should probably be reading like this: (where index is an int initialized to 0 before the while loop)

readNewFile.read(&ch[index], sizeof(Admin));
index = index + sizeof(Admin);

As the user in the comments suggested, you can even use an std::vector<char> here because it's just as fast as a char* and it doesn't need a specified size :)

Share:
19,011
Udit
Author by

Udit

Updated on June 05, 2022

Comments

  • Udit
    Udit almost 2 years

    Please have a look on the code. I am a beginner and this is first time I am creating a buffer in C++. How to create buffer in C++ to create new file after reading old contents into that buffer and ignoring the part which is to be deleted while truncating the old data and storing back the contents from the buffer?

    int main() {
        Admin item; // Admin is a class
        ios::pos_type pos;    
        int productId;
        char *ch; // pointer to create buffer
        int length;
    
        cout << "Enter Product Id of item to delete: ";
    
        cin >> productId;
    
        ifstream readFile("Stock.dat", ios::binary | ios:: in | ios::out);
    
        while (readFile) {
            readFile.read(reinterpret_cast<char*>(&item), sizeof(Admin));
    
            if (item.getStatus() == productId) {            
                pos = readFile.tellg() - sizeof(Admin);
                break;
            }
        }
    
        readFile.close();
    
        ifstream readNewFile("Stock.dat", ios::binary | ios:: in | ios::out);
    
        readNewFile.seekg(0, ios::end);
    
        length = readNewFile.tellg();
    
        ch = new char[length]; // is this how buffer is created?if no, please correct it. 
    
        readNewFile.seekg(0, ios::beg);
    
        while (readNewFile) {
            if (readNewFile.tellg() == pos)
                readNewFile.ignore(sizeof(Admin));
            readNewFile.read((ch), sizeof(Admin)); // is this how contents are read into buffer from file stream;
    
            if (readNewFile.eof())
                readNewFile.close();
        }
    
        ofstream outFile("Stock.dat", ios::trunc | ios::app);
    
        outFile.write(ch, sizeof(Admin)); // I am doubtful in this part also
    
    }
    
  • WhozCraig
    WhozCraig about 11 years
    +1 for the answer. That said, the OP should throw out the new/delete entirely and just size-up a std::vector<>.
  • Udit
    Udit about 11 years
    @Magtheridon96 done the correction you said but still the code is not removing desired item from the file, is there something wrong while writing from and reading to buffer in my code?
  • user123
    user123 about 11 years
    Try printing the contents of the variables such as pos and possibly the ch buffer into the console.
  • Udit
    Udit about 11 years
    @Magtheridon96 after doing as you said about adding index following errors occured while reading into buffer F:\codeBlocks\smb\main.cpp|193|error: invalid conversion from 'char' to 'char*' ,F:\codeBlocks\smb\main.cpp|193|error: initializing argument 1 of 'std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::read(_CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>]'| and while writting to file from buffer F:\codeBlocks\smb\main.cpp|206|error: invalid conversion from 'char' to 'const char*'|
  • Udit
    Udit about 11 years
    @Magtheridon96 done everything correct but still the code is not doing what it supposed to be do, it is not creating new altered file. When tried to print value of buffer string s(buffer); cout<<s; while filling the buffer, just a symbol one at a time got printed