Public Group

# file compare/manipulation

This topic is 4883 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

ok i need a little c app that will cross check 2 text files and make changes to one of those files accordingly. i have one text file with all the file names i SHOULD have, and another text file with all the names of the files i DO have. heres a few lines from each respectivly; first file: 1001-C-0012 1001-C-0013 1001-C-0014 1001-C-0015 1001-C-0016 1001-C-0017 1001-C-0020 1001-C-0021 1001-C-0022 1001-C-0023 1001-C-0024 1001-C-0025 1001-C-0026 1001-C-0027 1001-C-0028 1001-C-0029 1001-C-0030 second file: 1001c0012r1.tif 1001c0013r2.tif 1001c0015r1.tif 1001c0016r1.tif 1001c0017r1.tif 1001c0020r2.tif 1001c0021r1.tif 1001c0022r1.tif 1001c0025r1.tif 1001c0026r1.tif 1001c0027r4.tif 1001c0029r5.tif 1001c0030r3.tif so if you notice the first 9 characters from the second file are the names from the first but with a suffix rX.tif. the rX being the revision number which isnt important. But if you compare the file names you will notice that the second file is missing some of the names from the first file.. I need an app that will compare the files and if the name is missing from the second file it will add a new line, making a space for where the name should be so when i compare them side by side it will look like: 1001-C-0012 1001c0012r1.tif 1001-C-0013 1001c0013r2.tif 1001-C-0014 1001-C-0015 1001c0015r1.tif 1001-C-0016 1001c0016r1.tif 1001-C-0017 1001c0017r1.tif 1001-C-0020 1001c0020r2.tif 1001-C-0021 1001c0021r1.tif 1001-C-0022 1001c0022r1.tif 1001-C-0023 1001-C-0024 1001-C-0025 1001c0025r1.tif 1001-C-0026 1001c0026r1.tif 1001-C-0027 1001c0027r4.tif 1001-C-0028 1001-C-0029 1001c0029r5.tif 1001-C-0030 1001c0030r3.tif pretty straight forward eh? but i am a bit off a noob when it comes to programming. if anyone can just gimme a bit of a hand, a few lines off code and such ill be able to throw something together. The text files are 3300+ lines so i dont want to have to go down manualy and do it. Thanks

##### Share on other sites
It'd probably be much easier to use a language that supports regular expressions. Perl is well-known for its RE support, I believe Java has RE stuff as part of its standard library and you can likely find RE libraries for C\C++.

##### Share on other sites
I assume that both files are "sorted" and in the same order as in your example.

We need an algorithm like this:

Set x as the first line of file 1.Set y as the first line of file 2.Repeat until we reach the end of either file:  If x and y match:    Output (x concatenated with y).    Set x to the next line of the file.    Set y to the next line of the file.  Else if x is "after" y:    Set y to the next line of the file.  Else: (i.e. x is "before" y)    Set x to the next line of the file.    Output x.

So the next thing we need is to be able to "compare" file names from both. To do this, we can map each file name to the numeric value that it represents, and compare the numbers. That numeric value is simply what you get if you read all the digits in the filename:

int valueOf(std::string text) {  int result = 0;  int len = text.length();  // Loop through each character in the data  for (int i = 0; i < len; i++) {    char c = text; // Examine the current character    if (c >= '0' && c <= '9') { // This is a digit,      result = result * 10 + c; // so append it to the number.      // We do that by 'shifting' all the digits over (multiply by 10), and      // writing the current digit into the ones place (add it).    }  }  return result;}

To read lines from each input file, we need to open the file first. Here is an example:

void readWholeFile(std::string filename) {  std::string line; // we will read into here  std::istream file(filename.c_str()); // open the file  while (std::getline(file, line)) {    // Each time through the loop, 'line' holds the next line of the file.    // The result will be false (in boolean context) when there are no more lines.    std::cout << line << endl; // or whatever else you'd want to do.  }  file.close(); // close the file again.}

Stitching these pieces together is left as an exercise, as is conversion to C (if that's really important).

##### Share on other sites
wow ok Zahlman thats some crazy stuff, thanks mate

as for pieceing anything together.. im way over my head here. C is realy all i know, c++ is kinda out of my league right now.

its looking like i may have to just do it manually :(

...and learn how to program in c++

1. 1
2. 2
Rutin
25
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631764
• Total Posts
3002210
×