This function is called when the user wants to append information to the end of a file. Im not sure what is wrong with this, but Im sure its something small, basically when this runs, it just ouputs a strange square to the file lol. I am trying to get this to prompt the user for what they want to write to the file, store it as an array of characters, and then write that array of characters to the end of the file followed by a newline. Please help! Thanks very much in advance.
1 2 3 4 5 6 7 8 9 10 11 12 13
void add_to_file(char fileinfo[])
{
char add[MAX];
ofstream output;
output.open(fileinfo, ios::app);
if(output)
{
cout<<"What would you like to add to this file?: ";
cin>>add[MAX]; cin.ignore();
output<<add;
output<<endl;
output.close();
}
cin.getline( add, MAX );
if (cin.fail())
{
cin.clear();
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
cout << "You entered too many characters!\n";
}
output << add;
Frankly, you should avoid direct arrays and instead use the string class:
1 2 3 4 5 6 7 8 9 10 11 12
void add_to_file( char filename )
{
string s;
ofstream output( filename, ios::app );
if (output)
{
cout << "What would you like to add to this file?: ";
getline( cin, s );
output << s << endl;
output.close();
}
}
I am in CS162 and we have not been taught to use strings at this point, so I think she wants us to stick with arrays for the time being. We are doing LLL and Classes next so I will probably be back with more questions.
I really appreciate it though, you have no idea. I have been trying to ask the tutors and aides and they are really not much help.
Yes, I always found the student help part of the University to be less than helpful...
Either the student tutor is clueless (usually the case) or the student tutor figures out the answer and gives you his solution (the remaining times, minus a few).
A good tutor helps you solve the problem yourself...