Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Comparing Files for Differences

Comparing Files for Differences

Scheduled Pinned Locked Moved C / C++ / MFC
14 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U User 13401082

    .

    J Offline
    J Offline
    jeron1
    wrote on last edited by
    #2

    Can you solve it on a piece of paper? While many could, no one is going to write the code for you. Give it a shot, and if you have specific questions, people here are usually more than willing to help.

    "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

    1 Reply Last reply
    0
    • U User 13401082

      .

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #3

      Have you considered FC from a command prompt?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      1 Reply Last reply
      0
      • U User 13401082

        .

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #4

        If you are looking for a solution and not just to code it then you can google for an open source software called winmerge. It allows for a command line option to produce a diff and provides for UI view as well as various configuration options. There are other tools as well.

        U 1 Reply Last reply
        0
        • J jschell

          If you are looking for a solution and not just to code it then you can google for an open source software called winmerge. It allows for a command line option to produce a diff and provides for UI view as well as various configuration options. There are other tools as well.

          U Offline
          U Offline
          User 13401082
          wrote on last edited by
          #5

          .

          J 1 Reply Last reply
          0
          • U User 13401082

            .

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #6

            Member 13433022 wrote:

            regardless of the order in which those paragraphs are placed in the two different files

            With that requirement I don't know any existing tool because those usually try to find the next identical sequence. But writing such a tool for your requirement is not very difficult:

            • Read file 2 completely into allocated memory (one more byte to append a NULL char)
            • Count the number of lines (number of LF chars)
            • Allocate a buffer for char* pointers to hold the pointers to the beginning of each line
            • Fill the line pointer buffer (search for next LF and store address plus one)
            • Read file 1 line by line and check if that line matches any line from the file 2 buffer
            • If there is no match, write the line to file 3
            U 1 Reply Last reply
            0
            • J Jochen Arndt

              Member 13433022 wrote:

              regardless of the order in which those paragraphs are placed in the two different files

              With that requirement I don't know any existing tool because those usually try to find the next identical sequence. But writing such a tool for your requirement is not very difficult:

              • Read file 2 completely into allocated memory (one more byte to append a NULL char)
              • Count the number of lines (number of LF chars)
              • Allocate a buffer for char* pointers to hold the pointers to the beginning of each line
              • Fill the line pointer buffer (search for next LF and store address plus one)
              • Read file 1 line by line and check if that line matches any line from the file 2 buffer
              • If there is no match, write the line to file 3
              U Offline
              U Offline
              User 13401082
              wrote on last edited by
              #7

              Thank you for the instructions. The program can evidently be short enough and easy to write. Since I do not have experience in programming, do you know what the source code should be in order to compile it with Visual Studio or other software?

              J J J 3 Replies Last reply
              0
              • U User 13401082

                Thank you for the instructions. The program can evidently be short enough and easy to write. Since I do not have experience in programming, do you know what the source code should be in order to compile it with Visual Studio or other software?

                J Offline
                J Offline
                jeron1
                wrote on last edited by
                #8

                Please read this link HOW TO ASK A QUESTION - C / C++ / MFC Discussion Boards[^] near the top of this page, specifically #2. Many people here are very knowledgeable and will help you with questions during your coding endeavors, but generally will not serve up code to order.

                "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                1 Reply Last reply
                0
                • U User 13401082

                  Thank you for the instructions. The program can evidently be short enough and easy to write. Since I do not have experience in programming, do you know what the source code should be in order to compile it with Visual Studio or other software?

                  J Offline
                  J Offline
                  Jochen Arndt
                  wrote on last edited by
                  #9

                  I would use the C standard library functions fopen, fread (full file), fgets (read line), strchr (find NL), and strncmp (find match). A quick try from scratch without error checking (therefore untested):

                  // Maybe not all required header are shown here
                  #include #include // Get file size, allocate buffer, and read file 2 into memory
                  struct stat st;
                  stat(file2, &st);
                  char *buf = new char[st.st_size + 1];
                  FILE *f2 = fopen(file2, "rb");
                  fread(buf, 1, st.st_size, f2);
                  fclose(f2);
                  buf[st.st_size] = 0;

                  // Count lines
                  unsigned lines = 0;
                  char *p = buf;
                  while (NULL != (p = strchr(p, '\n')))
                  {
                  p++;
                  lines++;
                  }

                  // Get line pointers
                  char **lineptr = new char* [lines+1];
                  p = buf;
                  unsigned i = 0;
                  lineptr[0] = buf;
                  while (NULL != (p = strchr(p, '\n')))
                  {
                  lineptr[++i] = ++p;
                  }

                  // Must be large enough to hold max. line length
                  char linebuf[1024];
                  FILE *f1 = fopen(file1, "rb");
                  FILE *f3 = fopen(file3, "wb");
                  do
                  {
                  if (NULL == fgets(linebuf, sizeof(linebuf), f1))
                  break;
                  if (0 == linebuf[0])
                  break;
                  // Ignore empty lines
                  if ('\r' == linebuf[0] || '\n' == linebuf[0])
                  continue;
                  for (i = 0; i < lines; i++)
                  {
                  if (0 == strncmp(linebuf, lineptr[i], strlen(linebuf)))
                  break;
                  }
                  if (i >= lines)
                  fputs(linebuf, f3);
                  }
                  while (!feof(f1));
                  fclose(f3);
                  fclose(f1);
                  delete [] lineptr;
                  delete [] buf;

                  1 Reply Last reply
                  0
                  • U User 13401082

                    Thank you for the instructions. The program can evidently be short enough and easy to write. Since I do not have experience in programming, do you know what the source code should be in order to compile it with Visual Studio or other software?

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #10

                    Member 13433022 wrote:

                    Since I do not have experience in programming,

                    What does that mean? If you have zero experience then this shouldn't be the first thing to undertake. There are other tools besides the one I mentioned and one of them might allow you to get close enough, by messing with configuration to either produce exact result or one that you can manually modify to reach a result. If this sort of task is something that you are going to be doing on a normal basis then you should pick a language and start learning it. As a recommendation unless you want to be a programmer, I would use perl (a language) as an ad hoc tool to supplement other occupations. If however you want to program then you should start with one of the object languages, probably java or C#. Those would be easier initially, I think/guess, than C++. However regardless of language choice it is going to be better, as the first step, to either take an actual class or follow a tutorial exactly, unless you have an actual real world person that can help you when problems show up. The very first steps in programming can be very difficult when relying on the web.

                    1 Reply Last reply
                    0
                    • U User 13401082

                      .

                      J Offline
                      J Offline
                      Joe Woodbury
                      wrote on last edited by
                      #11

                      Beyond Compare[^] can do something like that, though to actually do the write (vs. cut and paste) can be a little tricky.

                      U 1 Reply Last reply
                      0
                      • J Joe Woodbury

                        Beyond Compare[^] can do something like that, though to actually do the write (vs. cut and paste) can be a little tricky.

                        U Offline
                        U Offline
                        User 13401082
                        wrote on last edited by
                        #12

                        Thank you for the suggestions on programming languages and showing how to write a code to perform the task. I had to ask others because I do not know how to write programs. Learning how to program can take much time. I simply needed to do that particular task of comparing files because Microsoft Word does not have such a function. In the code given by Jochen Arndt, do you know where to specify the locations of “File 1,” “File 2,” and “File 3”? Or does the program asks to specify the locations when run?

                        #include
                        #include

                        // Get file size, allocate buffer, and read file 2 into memory
                        struct stat st;
                        stat(file2, &st);
                        char *buf = new char[st.st_size + 1];
                        FILE *f2 = fopen(file2, "rb");
                        fread(buf, 1, st.st_size, f2);
                        fclose(f2);
                        buf[st.st_size] = 0;

                        // Count lines
                        unsigned lines = 0;
                        char *p = buf;
                        while (NULL != (p = strchr(p, '\n')))
                        {
                        p++;
                        lines++;
                        }

                        // Get line pointers
                        char **lineptr = new char* [lines+1];
                        p = buf;
                        unsigned i = 0;
                        lineptr[0] = buf;
                        while (NULL != (p = strchr(p, '\n')))
                        {
                        lineptr[++i] = ++p;
                        }

                        // Must be large enough to hold max. line length
                        char linebuf[1024];
                        FILE *f1 = fopen(file1, "rb");
                        FILE *f3 = fopen(file3, "wb");
                        do
                        {
                        if (NULL == fgets(linebuf, sizeof(linebuf), f1))
                        break;
                        if (0 == linebuf[0])
                        break;
                        // Ignore empty lines
                        if ('\r' == linebuf[0] || '\n' == linebuf[0])
                        continue;
                        for (i = 0; i < lines; i++)
                        {
                        if (0 == strncmp(linebuf, lineptr[i], strlen(linebuf)))
                        break;
                        }
                        if (i >= lines)
                        fputs(linebuf, f3);
                        }
                        while (!feof(f1));
                        fclose(f3);
                        fclose(f1);
                        delete [] lineptr;
                        delete [] buf;

                        1 Reply Last reply
                        0
                        • U User 13401082

                          .

                          U Offline
                          U Offline
                          User 13406711
                          wrote on last edited by
                          #13

                          here is the solution: Selection Sort in C++ | Codinglio[^]

                          1 Reply Last reply
                          0
                          • U User 13401082

                            .

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #14

                            Take file A and split into paragraphs. Search file B for each paragraph appending them to file C when not found. Swap files A & B then repeat. You could do this in a single command line in bash I reckon.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups