Comparison of multiple files in folder with base file

Hi, I've run into a bit of a snag when working with this code. The desired functions are:

1. Open a single file as a 'base file' and then cycle through all files ('sub files') placed in a different specified folder.
2. Read all data from base file into a deque of strings and all data from sub files into strings (note - each sub file string must experience a small adjustment to properly match base file string - this, I have working)
3. Compare base file strings to sub file strings. If a sub file string matches the base file string, that string must be removed from the sub file ALONG WITH subsequent files determined by the 102nd integer character in the base file string.
4. Output the corrected sub file and move onto the next one, which will NEVER contain the same strings removed in the previous sub file.

So far, I've managed to the first three parts, but whenever I try to move to the next sub file, no changes appear to occur. I've spent the better part of the last two days trying to figure this out with my limited knowledge of C++, so I figured I would come to the experts.

Code:

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
#include <fstream> //initializes data libraries
#include <string>
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <deque>
#include <sstream>

using namespace std;
 
int main () {
    
  string value; //string value
  string comp;
  string rem="0"; //initialization of null substrings
  string hold;
  int counter=0;
  int cancel=0;
  unsigned int a=0;
  unsigned int line_number(1); //protection of first line

  string path = ""; //initialization of string path
    string searchPattern = "*.trn"; //specification of file type
    string fullSearchPath = path + searchPattern; //combination for full scan
 
    WIN32_FIND_DATA FindData; //initialization of FIND_DATA
    HANDLE hFind;
 
    hFind = FindFirstFile( fullSearchPath.c_str(), &FindData ); //filename string initialization
 
    if( hFind == INVALID_HANDLE_VALUE ) //error checking for scan
    {
        cout << "Error searching directory\n";
        return -1;
    }
 
    do{
        string filePath = path + (FindData.cFileName); //intialization of path
        ifstream compare("/MT April backlog trans.trn");
        ifstream in( filePath.c_str() ); //string initialization
        ofstream temp;        //initialization of output stream
        temp.open("temp.txt"); //open temp file  
        if( compare ){
                while(compare>>comp){
                                 deque<string> comparison; //initialize deque from file
                                 comparison.push_back(comp); //fills deque
                                         if( in){
                                             while(in>>value){
                                                              value.replace(9,4,"");
                                                              for(a=0;a<comparison.size();a++){
                                                                  if(comparison[a]!=value){ //test iteration versus file
                                                                                   if(counter==0) //checks the counter
                                                                                                  temp<<value<<endl; //output
                                                                                   else
                                                                                       counter--; //decrements the counter
                                                                                   }
                                                                  else{
                                                                  comparison.pop_front();
                                                                  rem=value.substr(102,1); //initializes substring for counter
                                                                  stringstream ss(rem); //stringstream utilized to pass value down
                                                                  ss>>counter;
                                                                  }
                                                              }
          
                                     }
                                     }
                                 temp.close(); //close temp file
                                 in.close();            
                                 }
                                 }
        
        compare.close();  // close file
  
  remove(filePath.c_str()); //delete original file for rewrite with new file
  rename("temp.txt",filePath.c_str()); //rewrite and rename
  cout<<endl<<endl<<endl;

  }
    while( FindNextFile(hFind, &FindData) > 0 ); //activation loop for multiple iterations
 
    if( GetLastError() != ERROR_NO_MORE_FILES ) //error verification
    {
        cout << "Something went wrong during searching\n";
    }
 
    system("pause");

  return 0;
}


Any ideas?

EDIT: my apologies for wasting time, but I did manage to figure out the iteration problem. A static deque and a properly placed for loop was all that was required.
Last edited on
Topic archived. No new replies allowed.