-2
struct node
{
string info;
struct node *next;
}*start, *last;

long nodecount=0;
class teditor
{
public:
    node* create_node(string);
    void insert_pos();
    void save();
    void display();
    void delete_pos();
    teditor() 
    {
        start = NULL;
    }
 };
 node *teditor::create_node(string value)
{
struct node *temp, *s;
temp = new(struct node); 
if (temp == NULL)
{
    cout<<"Memory not allocated "<<endl;
    return 0;
}
else
{
    temp->info = value;
    temp->next = NULL;     
    return temp;
 }
}
void teditor::save()
{   
struct node *info;
ofstream listfile;
listfile.open("example.txt",ios::out|ios::app |ios::binary) ;
node *temp;
temp=start;
if(!listfile){
 cout<<"\nError";
 }
 else{
while(temp!=NULL)
{
    listfile.write((char*)(temp),sizeof(nodecount));
    temp=temp->next;
}
}   
listfile.close();
cout<<"\n\n\n\t\tLink list has been saved in file example.txt in current folder.";
cout<<"\n\n\t\tPress a key to continue ... ";getch();
}
void teditor::insert_pos()
{
string value; int counter;
int pos;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
nodecount++;
int i;
s = start;
while (s != NULL)
{
    s = s->next; counter++;
}
if (pos == 1)
{
    if (start == NULL)
    {
        start = temp;
        start->next = NULL;
    }
    else
    {
        ptr = start;
        start = temp;
        start->next = ptr;
    }
}
else if (pos > 1 )
{
    s = start;
    for (i = 1; i < pos; i++)
    {
        ptr = s;
        s = s->next;
    }
    ptr->next = temp;
    temp->next = s;
 }
else
{
    cout<<"Positon out of range"<<endl;
}
}

void teditor::display() 
{
/*
Need to merge as a string and show to display just in one line like writing
why cannot save health because of application saving pointers.
*/
node *temp;
temp=start;
cout<<"\n\n\n";
while(temp)
{
    cout<<"\t\t\t"<<temp->info;
    temp=temp->next;
}
cout<<"\n\n\t\t "<<nodecount<<" records displayed ,Press a key to continue.....";getch();
}
void teditor::delete_pos()
{
int pos, i, counter = 0;
if (start == NULL)
{
    cout<<"List is empty"<<endl;
    return;
}
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
    start = s->next;
}
else
{
    while (s != NULL)
    {
        s = s->next;  
    }
    if (pos > 0 && pos <= counter)
    {
        s = start;
        for (i = 1;i < pos;i++)
        {
            ptr = s;
            s = s->next;
        }
        ptr->next = s->next;
    }
    else
    {
        cout<<"Position out of range"<<endl;
    }
    free(s);
}cout<<s<<" Element Deleted"<<endl;nodecount--;
 cout<<"There is left "<<nodecount<<" nodes"<<endl; 

  }

Hi,guys!I have problem while I trying to save my linked list to txt.Everytime I tryin and txt gave me a chinese writing.Teacher also said I need to merged with string or I need to give node to string that application can easily save that string line.Maybe because of I 'm trying to write node *temp.Anybody know how can I solve my problem?After other processes it will be copy,cut,paste and replace with nodes.

4 Answers4

0

Change

listfile.write((char*)(temp),sizeof(nodecount));

to

listfile << temp->info;
kmdreko
  • 42,554
  • 6
  • 57
  • 106
0

You did not want to write the pointer temp into your files, you wanted the info that is inside the node that temp is pointing at. Then why you are writing temp instead of writing the info? YOu could do that "through" your pointer temp, right?

The below will do the above for you:

listfile << temp->info;
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
  • but still show same thing Am I did mistake while I insert string to linked list.? – Bahadır Soybakış Apr 29 '16 at 17:27
  • @BahadırSoybakış thats the only thing I can think of if you're still getting garbage. try to include the code that populates the list in the question – kmdreko Apr 29 '16 at 17:28
  • @bahad Post a [MCVE] or you would need to _step-into_ your code statement by statement to watch your logic and the values of every variable. – Khalil Khalaf Apr 29 '16 at 17:28
0

ok, you have a serialization problem. You want text in the file. But you are simply dumping memory to the file.

listfile.write((char*)(temp),sizeof(nodecount));

just writes the raw memory of a node.

Ask yourself - what should the text file look like? What do you expect it to look like in an editor. THen you have to write code to do that.

YOu need to do

listfile << temp->info

How do you want to save the next and prior. Maybe it is implicit in the ordering. SO this is all you need. Maybe you need line numbers ans to say next=4, prior=14

pm100
  • 48,078
  • 23
  • 82
  • 145
  • [link](http://stackoverflow.com/questions/523872/how-do-you-serialize-an-object-in-c) shows that serialization. I think as you say I need to define some variable to relate.@pm100 – Bahadır Soybakış Apr 29 '16 at 17:49
-2

Your problem will be solved after doing these steps:

  1. Right click on the project name, go to properties
  2. In general -> project defaults -> character set
  3. Choose not set
sirandy
  • 1,834
  • 5
  • 27
  • 32