User Tools

Site Tools


c_-_c_files:file_seek

C - C++ Files - File seek

#include <iostream>    
#include <fstream>   
#include <stdlib.h>   
using namespace std;
 
main(int argc, char *argv[])   
{   
  char ch;   
 
  if(argc!=3)
  {
    cout << "Usage: NAME <filename> <starting location>\n";   
    return 1;   
  }   
 
  ifstream in(argv[1]);   
 
  if(!in) 
  {
    cout << "Cannot open file";   
    return 1;   
  }   
 
  in.seekg(atoi(argv[2]), ios::beg);   
 
  while(in.get(ch))   
    cout << ch;   
 
  return 0;   
}

Seek and Skip

#include <fstream>
#include <iostream>
 
using namespace std;
 
int main()
{
  ifstream in("datafile");
  in.unsetf(ios::skipws);                     
  char ch;
 
  while (in >> ch) 
  {
    cout << ch;                                 
  }
 
  cout << '\n';
 
  in.clear();
  in.seekg(ios::beg);                         
  in.setf(ios::skipws);                       
 
  while (in >> ch)
  {
    cout << ch;                            
  }
 
  cout << '\n';
 
  return 0;
}
c_-_c_files/file_seek.txt · Last modified: 2020/07/15 10:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki