CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2005
    Posts
    41

    Question 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...

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    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
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    1,051

    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

  4. #4
    Join Date
    Mar 2017
    Posts
    105

    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.

  5. #5
    Join Date
    Mar 2017
    Posts
    105

    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.

  6. #6
    Join Date
    Mar 2017
    Posts
    105

    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.

  7. #7
    Join Date
    Mar 2017
    Posts
    105

    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
  •  





Click Here to Expand Forum to Full Width

Featured