i want to be able to read in a file called doc.txt of arbitrary length and not get a seg fault, i know i have to allocate on the heap but am having trouble doing so. i want to pretend like i have no way of knowing the file size or obtaining the file size, just read how ever much there is and allocate to heap, i want the only limitation to be size of physical memory ur machine has.
#include <iostream>
#include <string>
#include <fstream>
using namespace std ;
int main() {
char *file_name = "doc.txt" ;
ifstream fin ;
fin.open( file_name ) ;
if( ! fin ) {
cout << "Problems opening " << file_name << endl ;
return -1 ;
}
const unsigned MAX = 100 ;
string doc[MAX] ;
unsigned word_count = 0 ;
//while( fin >> doc[ word_count++ ] ) ;
while( fin >> doc[ word_count ] ) {
cout << doc[ word_count ] << endl ;
word_count ++ ;
}
fin.close() ;
return 0 ;
}