Question

Write a simple C/C++ function named int match_content(char * file1, char *file2) that would take two...

Write a simple C/C++ function named int match_content(char * file1, char *file2) that would take two text file name as a parameter, and return 1, if the content is same in both the files, 0 otherwise. Your program should be case insensitive and should support all the ascii characters.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

int match_content(char * file1, char *file2) {
    FILE *f1 = fopen(file1, "r");
    FILE *f2 = fopen(file2, "r");
    if (f1 && f2) {
        char c1, c2;
        while ((c1 = (char)getc(f1)) != EOF && (c2 = (char)getc(f2)) != EOF) {
            if (c1 >= 'a' && c1 <= 'z') {
                c1 -= 32;
            }
            if (c2 >= 'a' && c2 <= 'z') {
                c2 -= 32;
            }
            if (c1 != c2) {
                return 0;
            }
        }
        return getc(f1) == EOF and getc(f2) == EOF;
    }
    return 0;
}

int main() {
    printf("%d\n", match_content("input1.txt", "input2.txt"));
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
Write a simple C/C++ function named int match_content(char * file1, char *file2) that would take two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a simple C/C++ function, void copy_create(char * file), that would take one text file name...

    Write a simple C/C++ function, void copy_create(char * file), that would take one text file name as parameter and create a file named “duplicate.txt”, having the content of the file name sent as parameter.

  • C++.Write a program that opens two text files and reads their contents into two separate queues....

    C++.Write a program that opens two text files and reads their contents into two separate queues. The program should then determine whether the files are identical by comparing the characters in the queues. When two nonidentical characters are encountered, the program should display a message indicating that the files are not the same. If both queues contain the same set of characters, a message should be displayed indicating that the files are identical. Sample Run Suppose we have two files...

  • Complete a Python function compare_files(fn1,fn2) to compare file1 and file2, if their content are same return...

    Complete a Python function compare_files(fn1,fn2) to compare file1 and file2, if their content are same return True, otherwise return False. should start with def compare_files(f1,f2): myf3.txt contains: a b c d myf2.txt contains: A B C D myf1.txt contains: a b c d

  • you need to write a C function named rem. The prototype is: int rem(char*, char*); The...

    you need to write a C function named rem. The prototype is: int rem(char*, char*); The function accepts 2 strings: the first is a string of text to process; the second is where the output will be placed. rem() should remove all occurrences of a lowercase 'x' from the first string, and save the resulting string in the second arg. You may use the strlen() function; no other built-in fuctions should be used. Test your program thoroughly. Then, once you...

  • a. Ask the user for the input of a letter (char type). Write a function that...

    a. Ask the user for the input of a letter (char type). Write a function that takes a char as the argument (parameter) and returns the ASCII value of that char back to the caller. Call the function using the char variable and taking input from the user (no validation required). Display the value in ASCII. b. Write a program that takes a char as the argument (parameter) and uses a loop to interrogate the char, calling a function that...

  • ​Write a C++ function int calculate(char, int , int ) which takes a char of either ‘+’, ‘ ‘--’, ‘*’, or ‘/’ as operator and two integers as operands.

    Write a C++ function int calculate(char, int , int ) which takes a char of either ‘+’, ‘ ‘--’, ‘*’, or ‘/’ as operator and two integers as operands. The function should return the result of applying the corresponding operator on the operands if all of them are of proper types and values. It should throw a char exception if the operator is unknown and a char* exception if the operator is ‘/’ and the second operand is zero. Write...

  • 1.) Write a recursive function named isPalindrome that will take a single string as an argument...

    1.) Write a recursive function named isPalindrome that will take a single string as an argument and determine if the argument is a palindrome. The function must return True if the argument is a palindrome, False otherwise. Recall that any string is a palindrome if its first and last characters are the same and the rest of it is also a palindrome (therefore, a single character is a palindrome): 2.) Consider a text file named samplestrings.txt that contains a collection...

  • This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your...

    This assignment uses functions, files, and strings. Enough flexibility is provided for you to apply your knowledge of basic C++ programing to develop your solution. Develop a functional flowchart and then write a C++ program to solve the following problem. 1. Create a text file named file1.txt and write your brand of car (like Honda, Toyota, etc) in the file. You will be reading the name of the file from the keyboard as a string, using the string class. Your...

  • Write a C# or C++ code to do the following: -Ask user for name of file1...

    Write a C# or C++ code to do the following: -Ask user for name of file1 and file2 (These files should contains numbers representing rows and cols in a table- see below) -Read the content of the files into 2 arrays or something -Ask user which column to compare in array1 and which col in array2 to join the two tables. Loop through arrays and if the element in specified col of array1 matches the element in specified col of...

  • Write a function named insertBeforeKey that takes the parameters as follows list as an char array,...

    Write a function named insertBeforeKey that takes the parameters as follows list as an char array, capacity for the capacity of the array, numItems has the number of items in the array, key is an element of the array before which newVal is to inserted. Assume key is always present in the array. and newVal has the new value to be inserted into the array. If the array is at capacity then the function should return -1, otherwise return 0...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT