saving extra letters

i wrote this program and what i want it to do is
copy all the data from the Memblock and put it into the DelDupTemp except for the letter that was last entered
like this:

user inputs: xyzy
program outputs to file:xzy

the actual program output is like this
xzzy

this is because when i put the information from the DelDupTemp array into the file since DelDupTemp is smaller then Memblock one of the letters from Memblock is left over

i can't get anything to work
someone please help

is their a way to make it so the DelDupTemp will replace the Memblock completely?
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
void AI::SaveInput()
{
    //create first memory block
    
    char * TempMemblock;
    //when ever the user inputs a character 
    //the size of the memory block is increased
    //create a memory block
    //save the data to it
    
        std::ofstream file("data.bin", std::ios::out | std::ios::app | std::ios::ate | std::ios::binary);
        TempMemblock = new char;
        *TempMemblock = *pCurrentChar;
        
       
        file.write(TempMemblock, 1);
        delete TempMemblock;
        std::cout << "letter written to file" << std::endl;  
        file.close();
        
    
     
    
    
    
}



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
void AI::ManipulateData()
{
    
    bool Check = true;
   
    int Size = 0;
    
   
    
    char * Memblock;
    //opens files
    std::ifstream fileOutput("data.bin", std::ios::out | std::ios::ate | std::ios::binary);
    std::ofstream fileInput("data.bin", std::ios::in | std::ios::ate | std::ios::binary);
    
    
    if(fileInput.is_open())
    {
        //gets size
        Size = fileOutput.tellg();
        
        
        Memblock = new char [Size];
        fileOutput.seekg(0, std::ios::beg);
        fileOutput.read(Memblock, Size);
        
    
        
    
    
       
            
            for(int i = 0; i < Size && Check == true; i++)
            {
                
                    
                    
                    
                    
                //make comparisions //  don't want to read the first input because 
                //that is the input that was just typed in
                
                if(*(Memblock+i) == *pCurrentChar && Size != 1)
                {
                 
                     std::cout << "This is a duplicate letter" << std::endl; 
                     ////////////////delete the duplicate letter
                     //leave the letter that was just typed in and
                     //delete the letter the duplicate that had already been typed 
                     //in   
                     //rewrite the data to the file without the duplicate letter
                     //a pointer that will referance the beginning of the location 
                     //where the data will be stored until it is written to file
                     //
                     std::cout << "Deleting duplicate" << std::endl;
                     char * DelDupTemp;
                     DelDupTemp = new char [Size-1];
                     int k = 0;
                     for(int m = 0; m < Size; m++)
                     {
                        std::cout << "First Block " << *(Memblock+m) << std::endl;
                     }
                     for(int j = 0; j < Size; j++)
                     {
                             
                             //of the current character is the one in memory
                             //skip over it when copying
                             //std::cout << "Before change" << std::endl;
        //                         std::cout << "Current " << *(pCurrentChar) << std::endl;
        //                         std::cout << "Memblock " << *(Memblock+j) << " " << j <<std::endl;
        //                         std::cout << "TempDup " << *(DelDupTemp+k) << " " << k << std::endl;
        //                         
                             if( *pCurrentChar != *(Memblock+j))
                             {
                                    
                                    
                                    *(DelDupTemp+k) = *(Memblock+j);
                                    k++;
                            }
                             std::cout << "After: " << std::endl;
                              for(int m = 0; m < Size-1; m++)
                             {
                                std::cout << "First Block " << *(DelDupTemp+m) << std::endl;
                             }   
                             std::cout << "First Block " << *(pCurrentChar) << std::endl;
                             
                            
             
                   
        //                         std::cout << "After change" << std::endl; 
        //                         std::cout << "Current " << *(pCurrentChar) << std::endl;
        //                         std::cout << "Memblock " << *(Memblock+j) << " " << j << std::endl;
        //                         std::cout << "Temp " << *(DelDupTemp+k) << " " << k << std::endl;
        //                         std::cout << std::endl;
                            
                             
                             
                             
                             
                     }
                     
                     std::cout << "After After: " << std::endl;
                     for(int m = 0; m < Size; m++)
                     {
                        std::cout << "First Block " << *(DelDupTemp+m) << std::endl;
                     }
                           
                     
                     if(fileInput.is_open())
                     {
                            std::cout << "After After: " << std::endl;
                             for(int m = 0; m < Size; m++)
                             {
                                std::cout << "First Block " << *(Memblock+m) << std::endl;
                             }
                         fileInput.seekp(0, std::ios::beg);   
                         fileInput.write(DelDupTemp, Size-1);
                         
                         
                         delete [] DelDupTemp;
                        
                         
                         
                     }
                     else std::cout << "Unable to open file" << std::endl;        
                             
        
        
                     //reload the data
                     delete [] Memblock;
                     
                     Check = false;
                     
        
                     
                    
                }
                
        
            
        }
       
        delete [] Memblock;      
        fileInput.close();
        fileOutput.close();
        
        
    }
    else std::cout << "Unable to open file" << std::endl;
      
}
I have a couple questions.
1. Do you really need to use a file? I mean, is that part of the requirements, or is this just a workaround for something you don't know how to do?
2. Ignoring the fact that AI::SaveInput() is opening a file to write a single byte to it, what's the point of TempMemblock?
1
2
3
4
5
void AI::SaveInput(){
    std::ofstream file("data.bin", std::ios::out | std::ios::app | std::ios::ate | std::ios::binary);
    file.write(pCurrentChar, 1);
    std::cout << "letter written to file" << std::endl;    
}
1. I would like it to be saved to a file so i can access it later
2. I didn't know i could do that, thank you.
Do you need to keep the file updated at all times? Reading and writing to the same file without closing all streams that use it is rather messy, and more often than not an unnecessary complication. If you don't need to, it'd be better to keep the keystrokes in a structure of some sort and dump them all at once later. That's not only more organized, but also much, much faster than writing one byte at a time.
Last edited on
Topic archived. No new replies allowed.