Iam Trying to implement a basic text editor in c++. I've used a doubly linked list for storing the typed characters in a line and have implemented another linked list (No 2)to store many such lines for a document...
When i try to save the file using list No2,i find that the stored file contains junk...but the underlying retrieval of data from the list seems to work fine enough...can someone help me on this....
The code i implemented for the save method is as follows

void lines::save()//save is a method of class lines(list 2)
{
    dnode *ptr2;//ptr2 is a node class object(pointer) for line list
    dlnode *ptr1;//ptr1 is a node class object for lines list(list 2)
    char x;
    cout<<"ENTER FILE NAME TO SAVE:\n";
    cin>>fname;
    strcat(fname,".txt");
    fstream buf(fname,ios :: out);
    for(ptr1=start->getNext();ptr1;ptr1=ptr1->getNext())
    {
        for(ptr2=ptr1->getLineStart()->getNext();ptr2;ptr2=ptr2->getNext())
//method getLineStart is in dlnode and method getNext is in dnode.This is to get starting location of each line node from class dnode.ptr2=ptr2->getNext() moves to the next node location.
        {
                          buf<<ptr2->getChar();
        }
    }
    buf.close();
}

Thanks.

I can't really help much without seeing most of the code, but below won't work with filenames that contain spaces. and hopefully fname is defined to be max size for your operating system so that it can contain full path to the file. Id suggest you use std::string for the filename instead of C-style character array buffer, and use getline() instead of cin's insertion operator.

cin>>fname;
strcat(fname,".txt");

std::string fname;
getline(cin,fname);
fname += ".txt";
fstream buf(fname.c_str(),ios :: out);

I have Used Turbo C++ For My Text Editor.

I have Used Turbo C++ For My Text Editor.

I hope you are not using that ancient compiler in an attempt to learn the c++ language -- if you are then you will be learning the wrong language. Its like reading Shakespear to learn modern-day English, not very useful. Get one of the free c++ modern compilers, such as Dev-C++

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.