#include <fstream>
#include <limits>
#include<iostream>
#include<string.h>
usingnamespace std;
//Filling file to skip copying whole file into memory
void make_file(fstream& file)
{
char *a= newchar[100];
memset(a,'*',99);
int i=100;
while(i--)
{
file<<a<<endl;
}
}
std::fstream& GotoLine(std::fstream& file, unsignedint num){
file.seekg(std::ios::beg);
for(unsignedint i=0; i < num - 1; ++i){
file.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
return file;
}
std::fstream& GotoLineWrite(std::fstream& file, unsignedint num){
file.seekg(std::ios::beg);
for(unsignedint i=0; i < num - 1; ++i){
file.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
}
long len=0;
len=file.tellg();
file.seekp(len-1,std::ios::beg);
return file;
}
int main(){
usingnamespace std;
fstream file("bla.txt",ios::in|ios::out);
make_file(file);
GotoLine(file, 2); // This Function takes you to line number 2 to read input
string line="";
getline(file,line);
cout<<line<<endl;
line="+Changed";
GotoLineWrite(file,2); //// This Function takes you to line number 2 to write data. This not working for <=5
file<<line;
cout<<line;
return 0;
}
Hello all,
I wrote this code to read, write and edit in file. I basically initially filled file with characters which are replaced by later data.
Everything is going fine if i choose line number >=5.
But magically i don't know whats happening when i choose to edit line number <=5.
Can you please look into the code and help me a bit.
Its too important for me.