Question

Using C, Write a program to alphabetically merge the three word list files (american0.txt, american1.txt, and...

Using C, 
Write a program to alphabetically merge the three word list files (american0.txt, american1.txt, and american2.txt). Each file will have words in random order. The output must be a file called words.txt.

Note that you cannot cheat by using Linux commands to do this. It must be done entirely in your C code.

File format:
apple
banana
pear
  .
  .
  .

Hint:
Program will need to utilize double pointers.

More Hints:
1. Assume no word is bigger that 50 characters.
2. Determine file sizes first.
3. Create a single dynamic array to hold all of the words.
4. Write result to words.txt.
5. man 3 malloc or calloc.
6. man 3 strcmp and strcpy.
7. man 3 feof.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <stdlib.h>   

int main()

{

   int X = 1024 * 1024 - 1;

   FILE *f1 = fopen("american0.txt", "r");

 fseek(f1, X , SEEK_SET);

   FILE *f2 = fopen("american1.txt", "r");

 fseek(f2, X , SEEK_SET);

  FILE *f3 = fopen("american3.txt", "r");

fseek(f3, X , SEEK_SET);

FILE *f4 = fopen("words.txt", "w");

   char c;

  

   if (f1 == NULL || f2 == NULL || f3 == NULL || f4==NULL)

   {

         puts("Couldn't open files");

         exit(0);

   }

   while ((c = fgetc(f1)) != EOF)

      fputc(c, f4);

   while ((c = fgetc(f2)) != EOF)

      fputc(c, f4);

   while ((c = fgetc(f3)) != EOF)

      fputc(c, f4);

printf("Success");

   fclose(fp1);

   fclose(fp2);

   fclose(fp3);

   return 0;

}

Add a comment
Know the answer?
Add Answer to:
Using C, Write a program to alphabetically merge the three word list files (american0.txt, american1.txt, and...
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
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
ADVERTISEMENT