C++ adding new string into existing file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: C++ adding new string into existing file

Threaded View

  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

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center