I have created a list class, that allows the user to create or edit a text file with a list of information inside the .txt file. When I read into the alphabet.txt (a .txt with a list of all the letters), it prints out the letter "z" twice. The editing that the user does is shown between the two "z's." I'm attaching a picture that explains my dillemna.
Another problem is when I print the contents of the file, it does not print the contents of the entire file.
Thank You in advance.
below is a chunk of my List.cpp file.
cout <<"The following files are available for editing: \n";
cout<< "Name the .txt file that you wish to open\n";
cout << "Below displays the content of the file you selected" << endl << endl;
int response, i;
cout << "Enter the number of entries you would like to edit" << endl;
cin >> response;
cout << endl;
for(i=0; i < response; i++)
cout << "Enter your item" << endl;
cin >> a[i];
myFile << a[i] << endl;
I have created a list class, that allows the user to create or edit a text file with a list of information inside the .txt file. My problem is trying to add the new information into the file without erasing the existing data.
1) Your question is not specific to Visual C++. It is a general C++ issue, which makes it a topic for the Non-Visual C++ forum.
2) How about trying a very simple, 3 or 4 line main() program to experiment how you go about doing this? Once you are familiar with what you need to do in the smaller program, then you apply it to the larger program. Stuff about list classes and whatever else you are doing is not important or even necessary.
3) Please use code tags when posting code.
using namespace std;
ofstream ofile("mytest.txt", ios::app);
if ( ofile )
ofile << "Add another line";
Here is mytest.txt:
This is line 2
This is line 3
Now, given this very simple program, what are the results? If they are not what you want, you work on the program above to get the desired results. Then you take what you've learned in the small program and apply it to the larger program.
What does this accomplish?
1) You learn how to use a facility of the streams library without all of that unnecessary information.
2) You make it easier for others to help you to see what you're doing.
3) If in the remote chance there is a bug in the C++ library, you have a small app to send off to the Visual C++ development team.