7 Replies - 475 Views - Last Post: 28 April 2012 - 02:24 PM Rate Topic: -----

#1 johnnyboy420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-March 12

How do I replace a current value in a data file with a new value

Posted 27 April 2012 - 06:21 PM

So in my program i have a function in which i have to let the user modify a value in a 4by4 2darray from a data file to the data file.

Like if the data file has

305 470 550 210
515 680 752 400
629 150 422 430
538 621 737 530

And if i want to change the value 150 to 288



int Data[4][4];
int r=0, c=0;

int main()
{
\\\\\main program
}



void overwrite()
{
int modro, modcol, val;

ofstream outFile("Data.txt", ios::out | ios::app);
outFile.precision(2);
outFile.setf(ios::fixed, ios::floatfield);

cout<< "Enter the Row number in which you want to change a value for: ";	
cin >> modro;
cout<< "Enter the Column number of the row to change: ";
cin >> modcol;

outFile<<Data[modro-1][modcol-1];

cout<< "Enter in the Value to replacw into: ";
cin >> value;

Data[modro-1][modcol-1]=value;

outFile<< Data[modro-1][modcol-1];
outFile.close();

}



This is just adding a new value at the end of 530 but i need to replace a value not add a value.

Is This A Good Question/Topic? 0
  • +

Replies To: How do I replace a current value in a data file with a new value

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 3992
  • View blog
  • Posts: 12,320
  • Joined: 25-December 09

Re: How do I replace a current value in a data file with a new value

Posted 27 April 2012 - 07:25 PM

It is not possible to reliably "overwrite" an item in a text file. If the file is not too large you can read the file into memory, make the changes, erase the data in the file, then write the changed information to the file. Also the app open mode will only write to the end of the file.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: How do I replace a current value in a data file with a new value

Posted 27 April 2012 - 07:48 PM

To put it another way, if your assignment requires you to modify the file (as opposed to re-writing the entire file), the file should not be a .txt file (.dat would be better), you should be opening the file in binary mode (ios::binary), and you should be using the read() and write() functions for input and output, not >> and <<.
Was This Post Helpful? 0
  • +
  • -

#4 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: How do I replace a current value in a data file with a new value

Posted 28 April 2012 - 12:48 AM

Unless you use lower level disk I/O you have 2 choices: read the whole file, edit some data and overwrite the whole file, or copy the file into another file by reading from the old file, editindg a value if needed and writing to the new file.

With lower level disk I/O you can do the same to a single disk block of the file.

The HW only allows the disk to be read or written a disk block at a time.
(Well many disks also do track at a time.)
Was This Post Helpful? 0
  • +
  • -

#5 johnnyboy420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-March 12

Re: How do I replace a current value in a data file with a new value

Posted 28 April 2012 - 10:29 AM

Thanks for the suggestion I have a function called display() which already reads and closes the file for display, do i have to open it again in this function to read and edit or do i just write. How can I overwrite the data without losing anything that is what i am confused about?

View Postturboscrew, on 28 April 2012 - 12:48 AM, said:

Unless you use lower level disk I/O you have 2 choices: read the whole file, edit some data and overwrite the whole file, or copy the file into another file by reading from the old file, editindg a value if needed and writing to the new file.

With lower level disk I/O you can do the same to a single disk block of the file.

The HW only allows the disk to be read or written a disk block at a time.
(Well many disks also do track at a time.)

Was This Post Helpful? 0
  • +
  • -

#6 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: How do I replace a current value in a data file with a new value

Posted 28 April 2012 - 11:24 AM

Say, you read a table from a file.
Then you change some elements.
Then you overwrite the original file with the new edited table.
Not append, but overwrite.

The other way is:
You open the old file for reading and create a new file for writing
You read a line.
If there is nothing to change you write it to the other file
If there is something to change, you change it and write the edited line.
You close both files.

Or you do like in the first case, but instead of the whole file you read and write disk blocks.

Quote

do i have to open it again in this function to read and edit or do i just write.

If all the stuff is read, you only write. It's the same as if you deleted the file and re-created it and wrote the read contents to the new file.

This post has been edited by turboscrew: 28 April 2012 - 11:27 AM

Was This Post Helpful? 0
  • +
  • -

#7 johnnyboy420  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-March 12

Re: How do I replace a current value in a data file with a new value

Posted 28 April 2012 - 11:33 AM

Could you please guide me on how i can insert in the modifiyed data. I am not asking for the solution but a likewise example. Do i need to use a for loop to do this.

View Postturboscrew, on 28 April 2012 - 11:24 AM, said:

Say, you read a table from a file.
Then you change some elements.
Then you overwrite the original file with the new edited table.
Not append, but overwrite.

The other way is:
You open the old file for reading and create a new file for writing
You read a line.
If there is nothing to change you write it to the other file
If there is something to change, you change it and write the edited line.
You close both files.

Or you do like in the first case, but instead of the whole file you read and write disk blocks.

Quote

do i have to open it again in this function to read and edit or do i just write.

If all the stuff is read, you only write. It's the same as if you deleted the file and re-created it and wrote the read contents to the new file.

Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: How do I replace a current value in a data file with a new value

Posted 28 April 2012 - 02:24 PM

You need to understand that a text file contains a char for every character (letter or digit or punctuation) in your data, chars for blank spaces, newlines, sometimes "carriage return" chars, etc., so it's hard to find where to place your new data and hard to be sure that it won't overwrite something that it shouldn't. That's why you should use a binary file for this purpose. If you have a 4x4 array of int stored in a binary file, it will just contain the 16 int values, one after another. Very predictable and easy to handle.

Here's a tutorial on binary file i/o that has the info you need.

Here's a very simple program showing the bare basics of binary file i/o, but you'll still have to study the tutorial to adapt it to your task.


#include<iostream>
#include<fstream>

using namespace std;

int main()
{
    int arr[16] = {1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2};
    ofstream out("test.dat", ios::binary);
    for( int i = 0; i < 16; i++ ) {
        out.write( reinterpret_cast<char*>(&arr[i]), sizeof(int) );
    }
    out.close();

    ifstream in("test.dat", ios::binary);
    int arr2[16];
        
    for( int i = 0; i < 16; i++ ) {
        in.read( reinterpret_cast<char*>(&arr2[i]), sizeof(int) );
    }
    in.close();
    
    for( int i = 0; i < 16; i++ ) {
        cout << arr2[i] << " ";
    }
    cout << endl;

    return 0;
}


This post has been edited by r.stiltskin: 28 April 2012 - 02:24 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1