CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2012
    Posts
    9

    Question C++ adding new string into existing file

    Hi,

    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.

    void List::EditText()
    {
    char nameoffile[60];
    char a[50][100];
    string line;
    ifstream thefile;
    ofstream myFile;
    cout <<"The following files are available for editing: \n";
    cout <<"alphabet.txt\n";
    cout <<"buildings.txt\n";
    cout <<"engineering.txt\n";
    cout<< "Name the .txt file that you wish to open\n";
    cin.getline(nameoffile, 60);
    cout << "Below displays the content of the file you selected" << endl << endl;
    thefile.open(nameoffile);

    if(!thefile.is_open())
    {
    exit(1);
    }

    thefile >> myfile;
    while(thefile.good())
    {
    cout << myfile<< " " << endl;
    thefile >> myfile;
    }

    int response, i;
    cout << "Enter the number of entries you would like to edit" << endl;
    cin >> response;
    myFile.open(nameoffile, fstream::app);
    cout << endl;
    for(i=0; i < response; i++)
    {
    cout << "Enter your item" << endl;
    cin >> a[i];
    myFile << a[i] << endl;
    }

    cout<< "the new file will read\n";

    for(i=0; i < response; i++)
    {
    cout << a[i] << endl;
    }

    cout << myfile; // does not print content that is actually in the file
    thefile >> myfile;
    myFile<<myfile;
    myFile.close();
    }
    Attached Images Attached Images  
    Last edited by captjack; April 29th, 2012 at 07:26 PM. Reason: changed my code

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: C++ adding new string into existing file

    Quote Originally Posted by captjack View Post
    Hi,

    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.
    Code:
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
       ofstream ofile("mytest.txt", ios::app);
       if ( ofile )
       {
           ofile << "Add another line";
           ofile.close();
       }
    }
    Here is mytest.txt:
    Code:
    abc123
    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.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured