HI, I was trying to learn more about I/O with files in C++ and I can't figure out why this program can't output to the file...

#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
using namespace std;

char userinp;


void print_interface()
{
	cout << "Enter E to Encrypt" << endl << "Enter D to Decrypt" << endl;
	cout << "Enter R to Erase the file" << endl << "Enter X to Exit" << endl;
}

int main()
{
	int cipher = 0, i = 0, iter = 0;
	string data[1000];
	fstream myfile;

	myfile.open("myfile.txt"); // For some reasons this gives me error : myfile.open("myfile.txt", ios::app, ios::in, ios::out);
	if(! myfile.is_open())      // It was something about overloaded function can't take 4 arguments...
	{
		cout << "Could not open the file." << endl;
	}

	do
	{
		print_interface();

		cin >> userinp;

		if (userinp == 'E' || userinp == 'e')
		{
			system("cls");

			cout << "Enter the cipher to user (Number)" << endl;
			cin >> cipher;

			while(! myfile.eof())
			{
				getline(myfile, data[iter]);

				cout << data[iter];

				for(i = 0; i < data[iter].length(); i++)
				{
					data[iter][i] += cipher;
				}

				cout << data[iter];

				iter++;
			}

			for(i = 0; i < iter; i++)
			{
				cout << data[i] << endl;
				myfile << data[i] << endl;
			}
		}
		else if (userinp == 'D' || userinp == 'd')
		{
		}
		else if (userinp == 'R' || userinp == 'r')
		{
		}
		else if (userinp == 'X' || userinp == 'x')
		{
			break;
		}
	} while(1);

	myfile.close();
	
	return 0;
}

What I want this program to do is take input from myfile.txt and then in the end, completely overwrite the data read with the scrambled code. It reads in the data but it can't write to the file. I tried completely erasing the file between the reading and writting part. It didn't make any difference.


This didn't work either :

for(i = 0; i < iter; i++)
			{
				outfile.open("myfile.txt");
				cout << data[i] << endl;
				myfile << data[i] << endl;
				outfile.close();
			}

outfile is ofstream file...

You need to join the modes with bitwise OR ( | ), not as comma separated parameters.

myfile.open( "myfile.txt", ios::app | ios::in | ios::out );

Opening with append mode will not allow you to overwrite the file content, all you can do is add to the end. If you really want to overwrite, you need to use the At End mode ( ios::ate ). After reading the data, you will need to seek back to the beginning before writing.

Don't keep opening and closing it INSIDE the for loop!

You need to join the modes with bitwise OR ( | ), not as comma separated parameters.

myfile.open( "myfile.txt", ios::app | ios::in | ios::out );

Opening with append mode will not allow you to overwrite the file content, all you can do is add to the end. If you really want to overwrite, you need to use the At End mode ( ios::ate ). After reading the data, you will need to seek back to the beginning before writing.

Thanks that got me on the right track! But, the ate doesn't work. I can't overwrite it. When I use ate, no output is sent to the file. I don't know why...

@ Salem : Thanks, for the info. It didn't work anyways. :(

OK, a little bit more is needed. Remember that ios::app and ios::ate cause the file pointer to be initially pointing to the next byte past the last current content of the file. So to read the file you must first seek to beginning. When you wish to overwrite, you must again seek back to beginning.

See this example for some idea how to structure this.

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


int main()
{
	fstream the_file;
	string str = "";

	the_file.open( "dummy.txt", ios::in | ios::out | ios::ate );

	if( !the_file )
	{
		cout << "Error opening file.  quitting. \n\n";
		return -1;
	}
	the_file.seekg( 0, ios::beg ); //move to beginning of file for reading

	getline( the_file, str );

	cout << str << endl << endl;

	str.replace( 0, 5, "Hello" ); //modify the beginning of the string
	cout << str << endl << endl;

	the_file.seekg( 0, ios::beg ); //move back to beginning for writing

	the_file << str << endl;

	the_file.close( );
	the_file.clear( );

	str = "";   //reset the string to empty

	the_file.open( "dummy.txt", ios::in ); //just open for reading
    //no seekg needed, reading only operation will start at beginning
	
	if( !the_file )
	{
		cout << "Error opening file.  quitting. \n\n";
		return -1;
	}

	getline( the_file, str );

	cout << str << endl << endl;

	the_file.close( );

	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.