Demonstrate File random access. : fstream « File « C++






Demonstrate File random access.

 
#include <iostream> 
#include <fstream> 
#include <cstdlib> 
using namespace std; 
 
int main(int argc, char *argv[]) 
{ 
  if(argc != 3) { 
    cout << "Usage: CHANGE <filename> <byte>\n"; 
    return 1; 
  } 
 
  fstream out(argv[1], ios::in | ios::out | ios::binary); 
  if(!out) { 
    cout << "Cannot open file.\n"; 
    return 1; 
  } 
 
  out.seekp(atoi(argv[2]), ios::beg); 
 
  out.put('X'); 
  out.close(); 
 
  return 0; 
}

           
         
  








Related examples in the same category

1.fstream seekp: Seek file pointer position
2.Reverse file content
3.To read or write to a file, you include
4.Reverses the first N characters within a file
5.File Stream Objects as Function Arguments