How to output the program to txt file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

string ConvertName(string &name, bool ToSpace)
{   if(ToSpace)
    {   for(int i=0;name[i]!='\0';i++)
        {   if(name[i]=='&')
            {   name[i]=' ';
            }
        }
    }
    else
    {   for(int i=0;name[i]!='\0';i++)
        {   if(isspace(name[i]))
            {   name[i]='&';
            }
        }
    }
    return name;
}

string s_title;
int s_size;
class File
{  
      public:
             string name;
             int size;
             ofstream out;
             
             void PrintSize()
             {  cout<<name<<"'s size is "<<size<<" kb"<<endl;
             }
             
             void SaveFile()
             {
                  out<<s_title<<endl;
                  out<<s_size<<endl;
             }             
             
             File();
             File(string name, int size);
             File *nxt, *bfr;
};

File *HEAD = NULL, *TAIL = NULL, *temp;

File::File(string n,int s)
{  name = n;
    size = s;
    nxt = NULL;
    bfr = NULL;
}

void AddFileToHead(File *fail)
{  if(TAIL==NULL)
        TAIL = fail;
        
    if(HEAD!=NULL)
    {  fail->nxt = HEAD;
        HEAD->bfr = fail;
    }
    
    HEAD = fail;
}

void AddFileToTail(File *fail)
{  if(HEAD==NULL)
        HEAD = fail;
        
    if(TAIL!=NULL)
    {  TAIL->nxt = fail;
        fail->bfr = TAIL;
    }

    TAIL = fail;
}

void AddFileToMiddle(File *fail)
{  int x = 0;
    cout<<"Select which position of file to add: "; cin>>x;

    File *temp = HEAD;
    if(x==1 || temp==NULL)
    {  return AddFileToHead(fail);
    }
    else
    {  for(int i=1; temp!=NULL; i++, temp=temp->nxt)
        {  if(i==x)
            {  fail->nxt = temp;
                fail->bfr = temp->bfr;
                fail->bfr->nxt = fail;
                temp->bfr = fail;
                return;
            }
        }
        return AddFileToTail(fail);
    }
}


void PrintFileList()
{  File *temp = HEAD;
    int x=1;
    while(temp!=NULL)
    {  cout<<x<<". "; x++;
        temp->PrintSize();
        temp = temp->nxt;
    }
}


File *loadFile()
{
       File *add=NULL;     
       ifstream load;
       load.open("File.txt");
       string n;
       int s;
       while(true)
       {                  
                        load>>n;
                        load>>s;
                        if(n=="#break#")
                        break;
                        add=new File(ConvertName(n,true),s); AddFileToTail(add);
       }
       load.close();
       return add;
}

int main()
{  
    File *fail;
    loadFile();
    int x = 0; char y = 'n';
    ofstream out;
    while(true)
    {  system("cls");
        PrintFileList();
        cout<<endl<<"Add a file? (y/n): "; cin>>y;
        if(y=='y'||y=='Y')
        {  string n; int s,b;
            cout<<"input file name  "; fflush(stdin);cin>>n;
            cout<<"input file size "; fflush(stdin);cin>>s;
            fail = new File(n,s);
            
            cout<<endl<<endl<<"Do you wish to: "<<endl;
            cout<<"(1) Add a file to the first of list"<<endl;
            cout<<"(2) Add a file to the end of the list"<<endl;
            cout<<"(3) Add a file to the middle of the list"<<endl;
            fflush(stdin);cin>>b;
            if(b==1)
                AddFileToHead(fail);
            else if(b==2)
                AddFileToTail(fail);
            else 
                AddFileToMiddle(fail);
                
            system("cls");
            PrintFileList();
                          
        }
        else
            break;
    }
    
    
    
    while(true)
    {  system("cls");
        PrintFileList();
        cout<<endl<<"Delete a file? (y/n): "; cin>>y;
        if(y=='y'||y=='Y')
        {  cout<<"Select which file to delete: "; cin>>x;
            
            File *temp = HEAD;
            if(x==1)
            {  HEAD = HEAD->nxt;
                HEAD->bfr = NULL;
                delete temp;
            }
            else
            {  for(int i=1; temp!=NULL; i++, temp=temp->nxt)
                {  if(i==x)
                    {  temp->bfr->nxt = temp->nxt;
                        if(temp!=TAIL)
                        {  temp->nxt->bfr = temp->bfr;
                        }
                        else
                        {  TAIL = temp->bfr;
                        }
                    }
                        
                }
            }
        }
        else
            break;
    }

    cout<<endl<<"~end~"<<endl<<endl;
    system("pause");
    return 0;

}



above is my program
which File.txt 's content is:

haha.doc
23
hello.ppt
42
form.xls
23
#break#


I try to save the output of the program by doing this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
temp=HEAD;
    while(true)
             {    
                  string n;
                  int s;
                  cout<<"Save File...."<<endl;
                        if (temp==NULL)
                        {
                          new File(n,s);
                          out.open("File2.txt");
                          while(temp!=NULL)      
                          {
                          s_title=temp->name;
                          s_size=temp->size;
                          ConvertName(s_title,false);
                          temp->SaveFile();// void SaveFile(){out<<s_title<<endl;out<<s_size<<endl;} 
                          temp=temp->nxt;
                          }
                          out<<s_title;
                          out<<"#break#";
                          out.close();
                          cout<<endl<<endl;
                          cout<<"New item added successfully."<<endl;
                          break;
                          }
                       temp=temp->nxt;
                  }

but it doesnt work, so how I gonna to solve this problem??
Topic archived. No new replies allowed.