How I gonna output file as 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
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;

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 AddFileToTail(File *fail)
{  if(HEAD==NULL)
        HEAD = fail;
        
    if(TAIL!=NULL)
    {  TAIL->nxt = fail;
        fail->bfr = TAIL;
    }

    TAIL = 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(n,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);

                AddFileToTail(fail);
            system("cls");
            PrintFileList();
                          
        }
        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??
Last edited on
So your program works fine but just saving the output gives problems?

Are there any error messages?

From what I can see the second block initializes a File object with uninitialized variables n and s.
Last edited on
Topic archived. No new replies allowed.