**I know the code looks big, but it's a very small isolated section at the bottom (starting at line 164) that's giving me the problem.**
When this program is run, when I try to open the file I just appended some text to, it doesn't open... can anyone spot why? Thanks....
-Matt
p.s. Yes this is a program I made after following a tutorial, incase you're wondering.
//#include <iostream.h> <--No need for this because fstream includes it
#include <iomanip.h>
#include <fstream.h>
int main()
{
char a, b, c, d;
char ch;
//four character arrays
char stringOne[256];
char stringTwo[256];
char stringThree[256];
char stringFour[] = "This is a string of characters!";
cout << "Enter 3 letters: ";
cin.get(a).get(b).get(c).get(d);
cout << "A: " << a << endl
<< "B: " << b << endl
<< "C: " << c << endl
<< "D: " << d << endl; //for the terminating character thing
cout << "Enter string one: ";
cin.get(stringOne,256);
cout << "stringOne: " << stringOne << endl << endl;
cout << "Enter string two: ";
cin >> stringTwo;
cout << "stringTwo: " << stringTwo << endl << endl;
/* If the user enters something with a space and then more after the space up in string two, then the remainder after
the white space in string two will be left to linger in the input buffer and then will be immediatly put into string
three below, you won't even get a chance to enter anything in string three */
cout << "Enter string three: ";
cin.getline(stringThree,256);
cout << "stringThree: " << stringThree << endl << endl;
cout << "Try again..." << endl << "Enter string two: ";
cin >> stringTwo;
cout << "stringTwo: " << stringTwo << endl << endl;
cin.ignore(255,'\n');
cout << "And now you actually get to enter string three: ";
cin.getline(stringThree,256);
cout << "stringThree: " << stringThree << endl << endl;
/* The following section of code demonstrates the put() function */
cout.put('H').put('e').put('l').put('l').put('o').put('\n');
/* peek() and putback() */
cout << "Enter a phrase: ";
while ( cin.get(ch) )
{
if (ch == 'e')
{
cin.putback('E');
}
else
{
cout << ch;
}
while (cin.peek() == ' ')
{
cin.ignore(1,' ');
}
if (ch == '\n')
{
break;
}
}
/* The write() function */
int fullLength = strlen(stringFour);
int tooShort = fullLength -4;
int tooLong = fullLength + 6;
cout << "using write(), with a parameter that's correct:" << endl;
cout.write(stringFour,fullLength) << endl << endl;
cout << "using write(), with a parameter that's too short:" << endl;
cout.write(stringFour,tooShort) << endl << endl;
cout << "using write(), with a parameter that's too long:" << endl;
cout.write(stringFour,tooLong) << endl << endl;
/* The width() function */
cout << "Width function:";
cout.width(25);
cout << "The paramter was 25" << endl << endl;
cout << "Width function again:";
cout.width(25);
cout << "Paramter also 25\n";
cout << "Width only goes to the NEXT output." << endl << endl;
cout << "Width function AGAIN: ";
cout.width(4);
cout << "Paramter was 4, way too short, but that's the same as just enough." << endl << endl;
/* The setf() function */
const int number = 234;
cout << "The number is: " << number << endl;
cout << "That number, " << number << ", in hex is " << hex << number << endl;
cout << "The hex number, with it's base shown: ";
cout.setf(ios::showbase);
cout << hex << number << endl;
cout << "The number, formated with 10 width: ";
cout.width(10);
cout << hex << number << endl;
cout << "Number with alignment 'left': ";
cout.width(10);
cout.setf(ios::left);
cout << hex << number << endl;
cout << "Number with alignment 'internal': ";
cout.width(10);
cout.setf(ios::internal);
cout << hex << number << endl;
cout << "Using concatenated setw() to set width to 10:"
<< setw(10) << hex << number << endl << endl;
/* File reading and writing */
char fileName[80];
char buffer[255]; //for user input
cout << "Enter filename: ";
cin >> fileName;
ofstream fout(fileName); // open for writing
fout << "This was written to file in the background..." << endl;
cout << "Enter text for file writing: ";
cin.ignore(1,'\n'); // eat the newline after the file name
cin.getline(buffer,255); // get the user's input
fout << buffer << "\n"; // and write it to the file
fout.close(); // close the file, ready for reopen
ifstream fin(fileName);
cout << "Here is what's currently in the file: " << endl;
char ch2;
while(fin.get(ch2))
{
cout << ch2;
}
cout << " -=-=-=-=-= End of file contents =-=-=-=-=- " << endl << endl;
fin.close(); //just being neat and tidy
/* file writing in append mode */
cout << "Opening file in append mode: " << endl;
fout.open(fileName,ios::app); //reasign existing fout object
if(!fout) //if unable to open file for output.... same as if(fout.fail())
{
cout << "Was unable to open file for output..." << endl;
}
cout << "Enter more text for the previous file: " << endl;
cin.ignore(1,'\n');
cin.getline(buffer,255);
fout << buffer << "\n";
fout.close();
fin.open(fileName); //reasign existing fin object
if(!fin) //if unable to open for file input
{
cout << "Unable to open for file input..." << endl;
}
cout << "Here's the contents of the file: " << endl;
while(fin.get(ch2))
{
cout << ch2;
}
cout << " -=-=-=-=-= End of file contents =-=-=-=-=- " << endl << endl;
fin.close();
/* The fill() function */
cout << "Using fill() to add astericks wherever whitespace is added from width(): " << endl;
cout << "Start --> ";
cout.width(25);
cout.fill('*');
cout << " <-- End" << endl << endl;
system("PAUSE");
return 0;
}