-
February 23rd, 2006, 02:31 AM
#1
how to overwrite the data into te file on a particular position
hi,
plz tell how to overwrite the data into a file on a particular position... i am having a file which is not having a constant data... i have to search a particular string in the file.. them have to go back on a particular data and overwrite that data with my new data...
plz help.. its urgent...
-
February 23rd, 2006, 02:50 AM
#2
Re: how to overwrite the data into te file on a particular position
Why not reading the file into memory, change in the memory however you want then write the whole file again?
Cheers
-
February 23rd, 2006, 02:52 AM
#3
Re: how to overwrite the data into te file on a particular position
or maybhi somethin like a " find and replace " ???
if i am not wrong there is an article in CG for doin the same . also other threads discussing the same issue.
- Sreehari
"Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
" Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." – Calvin
-
January 3rd, 2018, 02:18 AM
#4
Re: how to overwrite the data into te file on a particular position
It's easy, you need to create two files, one input one output, just copy data from one two another by reading and change the data from where you want to edit, if you want I can even send you the code.
-
January 3rd, 2018, 02:23 AM
#5
Re: how to overwrite the data into te file on a particular position
If you have a CSV file that contains this
21 22 23
21 22 23
27 28 29
And want to do it like this:
21 22 23
24 25 26
27 28 29
You need to overwrite from second line
You can do it like this:
Last edited by A_Singh; January 3rd, 2018 at 02:28 AM.
-
January 3rd, 2018, 02:26 AM
#6
Re: how to overwrite the data into te file on a particular position
Code :
Code:
#include <string>
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
int main()
{
int cnt = 1;
string line;
ifstream myFile;
ofstream resFile;
myFile.open("test.csv");
resFile.open("test1.csv");
if (myFile.is_open() && resFile.is_open())
{
while(getline(myFile, line))
{
if (cnt == 2) { resFile << "22, 23, 24" << endl; }
else resFile << line << endl;
cnt++;
}
cout << "File open successful" << endl;
cout << "Finished writing to file, will now close" << endl;
myFile.close();
resFile.close();
cout << "File close successful" << endl;
}
_getch();
return 0;
}
Assuming that test is the input file you give and test1 is the output file you receive. cnt is incremented and when it reaches 2 it overwrites the line while reading.
If you like my answer, do give some repotution.
Last edited by A_Singh; January 3rd, 2018 at 02:28 AM.
-
January 3rd, 2018, 02:35 AM
#7
Re: how to overwrite the data into te file on a particular position
You can even make the pointer jump.
To make the pointer jump to the lth line of file.
Code:
resFile.seekg(0);
string line;
for (size_t k = 1; k < l; ++k)
getline(resFile, line);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|