help on void modifyfile()

i need help on void modifyfile(). just to modify the output by void write2file(). I tried to do but each time gave me some error.

#include<cmath>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

/*Function declarations*/
void readffile();
void write2file();
void modifyfile();

void main()
{
	char c,ans;

	do
		{
		cout<<"Main Menu:\n"
			<<"============\n\n"
			
			<<"(D)ata Input\n"
			<<"(M)odify Data\n"
			<<"(V)View Data\n"
			<<"(E)exit Program"
			<<endl;

			cout<<endl<<"Enter your selection: ";
			cin>>c;

			if(c=='D'||c=='d')
				write2file();
			else if(c=='M'||c=='m')
				modifyfile();
			else if(c=='V'||c=='v')
				readffile();
			else if (c=='E'||c=='e')
			break;
 
			else
 

			cout<<"Wrong Selection"<<endl;
 

			cout<<"Do you want to continue. Y(Yes), N(No) ?";
			cin>>ans;

			}while(ans!='N');

			cout<<"Thank You. Have a nice day."<<endl;

		}

		void write2file()
		{
			/*Filetype declaration*/
			ofstream output;

			/*Variable declaration*/
			char filename[25];


			/*Enter filename*/
			cout<<"Enter filename to save:";
			cin>>filename;
			/*Open the required file*/
			output.open(filename, ios::out);
			
			/*Test the existence of file*/
			if (output.fail())
			{
				cout<<"File " << filename << "not exist!" <<endl;
				exit(1);
			}

			/*Perform computation*/
			int i;
			cout << " key in the value" << "\n" ;
			cin >> i;
			output<< pow(i,2) <<endl;


			/*Tell the user that the operation is complete*/


			
		}

		void modifyfile()
				
		{
			
	

		}

		
		void readffile()
		{
			/*variable declarations*/
			char filename[25];
			int numbers;

			/*Enters filename*/
			cout<<"Enter filename to read:";
			cin>>filename;
			fstream input;
			/*open the required file*/
			input.open(filename);

			/*test the existence of the file*/
			if (input.fail())
			{
			cout<<"File " << filename << "not exist!" <<endl;
			exit(1);
			}

			cout<<"Data from " << filename<<endl;

			/*Perform Read operation*/
			input>>numbers;

			/*Read till the end of file*/

			while(!input.eof())
			{
				cout<<numbers<<endl;
				input>>numbers;
			}

	/*close the file*/
	input.close();

	/*Tell the user that the operation is complete*/
	cout << "Done" << endl;
}

First you should tell what do you want modifyfile() to do.
You should also tell us what you expected the program to do and what is it actually doing. Unless you post in a good way, it is hard to expect answers for question/problem.

By looking at your program, it seems that you want to input some numbers into a file, and read them when the user wants.

using only ios::out will cause your program to overwrite the file. Use ios::app (or ios::ate if you are using the function istream::seekp(int)) when opening files for writing. This will output everything at the end.

Modifying a file could mean a lot of things. It could mean modifying a particular record (at a given position or having a given value), inserting a record at a given place, deleting a record, sorting the file, etc.
Thanks for your reply. For example, the file created by the Data input is 9 after i key in 3 (3^2=9) . i just want to edit the 9 to other results. Maybe 10 or any other integer. Not to overwrite the whole file but to modify or edit the integer inside the file.
You are still not explaining it right.

By looking at your readfile() function, it seems that you are storing multiple numbers in you file. The example you gave in your most recent post is about replacing a single number. Now there can be 2 things that you might want to do (or something very similar):

1. User will enter 2 numbers x and y and then replace the number at position x by y

Suppose your file is:
4
16
36
9
16


Suppose in the function modifyfile(), then user will enter 3 and then 5.
This will replace the number at 3rd position by 25(52)

This will make your file
4
16
25
9
16


2. User will enter x and y and replace every occurrence of x2 by y2.

Suppose in modifyfile() the user enters 4 and 7.
The program will replace all occurences of 16 by 49.

This will make your file:
4
49
36
9
49


Now you must specify what type of modification you want to do (type 1 or type 2).

For both these types, you would have to
1. Make another file (obviously with a different name).
2. Using a loop
a. read a number from original file (into an int variable, suppose n)
b. modify n if required (by checking its position or value).
c. write n to the other file (don't write if you want to delete instead of modify)
3. Close both files
4. Delete the original file by using remove(char* filename) in <stdio.h>
5. Rename the new file to that of the original file by using rename(char* oldfile,char* newfile) in <stdio.h>

This has to be done if you are using a text file because in text files integers can have varying sizes. For eg, 234 will use 3 bytes while 36543423 will use 8 bytes.

I strongly recommend you to use binary files (as you are storing integers) as this will rid you of this long process, it will be faster and all integers will only occupy sizeof(int) bytes (usually 4 bytes). It will store integers in files the same way it is stored in RAM during a program.

The write() function of ostream class can be used to write an int to a binary-mode-opened file.

This is how the function's prototype looks:
ostream& ostream::write(char* arr,int size);
It writes n characters from the char array arr to the file.

This is how you encode integers and write them to a binary file.

1
2
int a=5;
file.write((char*)&a,sizeof(int));

Similary you can read from binary files using

file.read((char*)&a,sizeof(int));

Also, in text files, as you don't know the size of an object, you have to continuously read data to reach an object at a given index. But in binary files you can directly jump to the point by using seekg() or seekp().
Last edited on
Topic archived. No new replies allowed.