How to use Random Access Files in C++

C, C++, Visual C++, C++.Net Topics
Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to use Random Access Files in C++

Post by Neo » Mon Mar 01, 2010 11:25 am

Note that the functions we demonstrate here are fstream, seekp, put and close related to file access.

Code: Select all

#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; 
}
Post Reply

Return to “C/C++ Programming”