Comparing Files for Differences
-
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
-
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
-
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.
-
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.
.
-
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
-
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
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?
-
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?
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
-
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?
I would use the C standard library functions
fopen
,fread
(full file),fgets
(read line),strchr
(find NL), andstrncmp
(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; -
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?
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.
-
Beyond Compare[^] can do something like that, though to actually do the write (vs. cut and paste) can be a little tricky.
-
Beyond Compare[^] can do something like that, though to actually do the write (vs. cut and paste) can be a little tricky.
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; -
here is the solution: Selection Sort in C++ | Codinglio[^]