Hi there guys, i've been working on a particular assignment for a prog class and cannot seem to find a way to solve the second requirement (in bold below) of the problem which is:
Write a program that reads the telephone.dat file and displays its contents on the console. The program should allow the user to add new entries to the file. Now the following code is what i've written thus far to allow me to bring up the file's content but i don't really know where to put code for the input code. Please help!! I know this is elementary stuff to most of you but i'm really struggling with this. I have a couple of hours to get this done do any assistance would be more than appreciated!
// This program asks the user for the file to open. The file is then
// opened and its contents are displayed on the screen.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
string fineName; // To hold the file name
char ch; // To hold a character
fstream file; // File stream object
// Get the file name
cout << "Enter a file name: ";
cin >> fineName;
// Open the file.
file.open(fineName, ios::in);
// If the file was successfully opened, continue.
if (file)
{
// Get a character from the file.
file.get(ch);
// While the last read opeation was
// successful, continue.
while (file)
{
// Display the last character read.
cout << ch;
// Read the next character
file.get(ch);
}
// Close the file.
file.close();
}
else
cout << fineName << " could not be opened.\n";
return 0;
}
I believe what the assignment is asking for you to output (or in this case, append) to the file. You're going to want to take the new entry via the command line from the user, and then you're going to want to open the file for appending:
file.open( filename, fstream::app );
From there, you're going to use the insertion operator ( file << data; ) to tack on to the end of the file.
I followed your instructions and this is what i have now:
// This program asks the user for the file to open. The file is then
// opened and its contents are displayed on the screen.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string fileName; // To hold the file name
char ch; // To hold a character
fstream file; // File stream object
fstream data; //File stream obsject
// Get the file name
cout << "Enter a file name: ";
cin >> fileName;
// Open the file.
file.open(fileName, ios::in);
// If the file was successfully opened, continue.
if (file)
{
// Get a character from the file.
file.get(ch);
// While the last read opeation was
// successful, continue.
while (file)
{
// Display the last character read.
cout << ch;
// Read the next character
file.get(ch);
}
// Close the file.
file.close();
{
// Get the file name
cout << " Enter a New Contact";
cin >> ch;
//Open the file for editing.
file.open( fileName, fstream::app );
//Allow for additonal entries
file << data;
}
}
else
cout << fileName << " could not be opened.\n";
return 0;
}
But for some reason, upon debugging it i get extra character (numbers and letters in no specific order) on the line containing "Enter a new contact". Any idea on what could be causing this?
You're not supposed to be pushing an fstream object into another. The line file << data; is incorrect. It's supposed to go something like: file << [string/char*/char/int/double] One of the primitive types pretty much.