Question
I need this code to be written in C language. Also, I'm using Xcode on mac so I need the steps in order to know how to do it.
Please save the program with the name files.c Write a program that merges two files as follows. The two files are in the do
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The completed C program is given below. I've added comments to explain what each line of code does. In the first section I have added the Source Code and Sample Output. In the next section I have added instructions for creating a C project on XCode:

Source Code:

#include <stdio.h>

//we define a maximum length for our strings as

#define MAX_LEN 500

int main() {

    //Declare three file pointers. We will use fp1 and fp2 to read our files

    //and fp3 to write the merged content into another file

    FILE *fp1, *fp2, *fp3;

    //define two strings (character arrays) for input of username and password from input files

    char username[MAX_LEN];

    char password[MAX_LEN];

    //try to open users.txt for reading

    fp1 = fopen("users.txt","r");

    //if fopen returns NULL, file couldn't be opened. Show an error message and return

    if(fp1 == NULL) {

        printf("File Read Error. Aborting....");

        return 1;

    }

    //try to open passwords.txt for reading

    fp2 = fopen("passwords.txt","r");

    //if fopen returns NULL, file couldn't be opened. Show an error message and return

    if(fp2 == NULL) {

        printf("File Read Error. Aborting....");

        return 2;

    }

    //try to open usernamesPasswords.txt for writing

    fp3 = fopen("usernamesPasswords.txt","w");

    //if fopen returns NULL, file couldn't be opened. Show an error message and return

    if(fp3 == NULL) {

        printf("File Read Error. Aborting....");

        return 3;

    }

    //use a while loop to read the files till we reach EOF (End of File character) in either of the'

    //two input files

    while(!feof(fp1) && !feof(fp2)) {

        //read username from users.txt

        fscanf(fp1,"%s",username);

        //read password from passwords.txt

        fscanf(fp2,"%s",password);

        //merge both and write them to usernamesPasswords.txt

        fprintf(fp3,"%s\t%s\n",username,password);

    }

    //close the open files

    fclose(fp1);

    fclose(fp2);

    fclose(fp3);

    //Ask the user if they want to see the merged fies

    char ch;

    printf("Do you want to view the Output files? (Y/N) ");

    scanf("%c",&ch);

    //if they enter y, we print the output file

    if(ch == 'Y' || ch == 'y') {

        //try to open usernamesPasswords.txt for reading

        FILE *fp4 = fopen("usernamesPasswords.txt","r");

        //if fopen returns NULL, file couldn't be opened. Show an error message and return

        if(fp4 == NULL) {

            printf("File Read Error. Aborting....");

            return 4;

        }

        //use a while loop to read the files till we reach EOF (End of File character)

        while(!feof(fp4)) {

            //read the file character by character by character

            char read = fgetc(fp4);

            printf("%c",read);

        }

        //close the file

        fclose(fp4);

    }

    return 0;

}

Sample Output:

$ ./files Do you want to view the Output files? (Y/N) y foster001 x34rdf3e smithe23 P43e4rff nyuyen002 W32eds22

Instructions:

(1) Creating a C Project in X-Code

  1. Launch XCode and select "Create a new XCode project"

Welcome to Xcode Version 5.1.1 (561008) Create a new Xcode project Start building a new iPhone, iPad or Mac application. Chec

  1. Then Choose Command Line Tool from the OS-X tab

Choose a template for your new project iOS Application Framework & Library Other Cocoa Application OS X Application Framework

  1. Chose a name for your project. Here it is ExampleProject, but you can pick any name you want. Add alos select type as C

Choose options for your new project: Product Name Example Project Organization Name Maxs Company Identifier 131 Bundle Identi

  1. Go to the file navigator on the Side Navbar on the left hand side, click on your project name, then click on the file named "main.c"

Xcode File Edit View Find ө00 Example Project My Q A Example Project 1 target, OS X SDK 10.9 Example Project c main.c Example

  1. Copy paste the code in the main.c file

Xcode File Edit View Find Navigate Editor Product Debug Source Control Wing Example Proje Example Project My Mac 64-bit Examp

  1. Run the file once to create the executable. The first time you will get an output that says "File Read Error. Aborting....". But don't woryy. This will create the executable file. You cna run the program by clicking on the play button in the top-left

Xcode

  1. Click on products in the file navigator on the left hand side , and you will see the executable. It will be shown by a black and white icon. Right click on it and select Show in Finder. This will lead you to the location of the executable. In that folder/path save your input files "users.txt" and "passwords.txt". Then run your code again, and it should run correctly

schedule My Mac 64-bit Ta 8 1 < schedule 1 target, OS X SDK 10.9 schedule .e main.c schedule. 1 Products sche Show in Finder

Add a comment
Know the answer?
Add Answer to:
I need this code to be written in C language. Also, I'm using Xcode on mac...
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
  • I need help writing this code in C++ and I’m using xcode for mac the objective...

    I need help writing this code in C++ and I’m using xcode for mac the objective is in the first picture and the rest is a sample output of the code thaf needs to be similar bjectives: . Perform C++ string object manipulation Understand how to manipulate data using arrays of structs Handle input errors and invalid values Design and create a wel-structure program using C++ basic programming constru Description: Write a menu-driven program that provides the following options 1....

  • I need help in C++ assignment. please add statements and i am Xcode complier user. Requested...

    I need help in C++ assignment. please add statements and i am Xcode complier user. Requested files: CountDecades.cpp (Download) Maximum upload file size: 96 KiB Write a C++ program named CountDecades.cpp. In this program you will have two integer arrays. One named inputArray of size 20 containing the values: 83, 2, 23, 11, 97, 23, 41, 67, 16, 25, 1 , 4, 75, 92, 52, 6, 44, 81, 8, 64 in the order specified, and an empty integer array of...

  • I need to create a code based off of what the users first name and last...

    I need to create a code based off of what the users first name and last name are along with a random set of numbers no longer than 10 characters as the picture below demonstrate (Java language) Scenario: You have been appointed by the CS department to create a username generator to create CS email accounts automatically. The CS email will have the following format: [email protected]. Your username must have a limit of 10 characters. Your program, will provide three...

  • Write a java program. I was given the following case. Your boss has determined that employee...

    Write a java program. I was given the following case. Your boss has determined that employee passwords need to be updated and made stronger. The new password policy has the following requirements At least 8 characters in length. Must contain at least one upper case character . Must contain at least one lower case character Must contain one number character Must contain one of the following characters: @,#,%,^,&,* After a couple of weeks, your boss wants to know if all...

  • Please give me that correct code for this by using C++ language. and i hope you...

    Please give me that correct code for this by using C++ language. and i hope you use Visual stduio Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }.  For example, the string  {a(b+ac)d[xy]g} and  kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match...

  • I need #5 done i cant seem to understand it. it must be written in c++...

    I need #5 done i cant seem to understand it. it must be written in c++ 4. Numeric Processing Write a program that opens the file "random.txt", reads all the numbers from the file, and calculates and displays the following: a. The number of numbers in the file. b. The sum of all the numbers in the file (a running total) c. The average of all the numbers in the file numFile.cpp Notes: Display the average of the numbers showing...

  • Hello I just need the code for this GUI thanks Also computer screenshot of the code...

    Hello I just need the code for this GUI thanks Also computer screenshot of the code will be helpful Attached is an incomplete Python template file. button_multi_colour_changer_Q.py Download this file and complete it to solve the following problem. Consider the GUI below (4 Labels, 4 colour Buttons and a "RESET" Button) which has been produced with a Python program using the Tkinter library. MULTI Button Colour - a X MULTI Button Colour - O X Violet Purple Blue | Green...

  • Create a python script to manage a user list that can be modified and saved to...

    Create a python script to manage a user list that can be modified and saved to a text file. Input text file consisting of pairs of usernames and passwords, separated by a colon (:) without any spaces User choice: (‘n’-new user account, ‘e’-edit existing user account, ‘d’- delete existing user account, ‘l’- list user accounts, ‘q’-quit) List of user accounts, error messages when appropriate Output text file consisting of pairs of username and passwords, separated by a colon (:) without...

  • This program should be written as a C code, You need to think how to break...

    This program should be written as a C code, You need to think how to break it down to steps that can do the program. I DON'T WANT YOU TO WRITE THE CODE, I only need you to tell me something like ( first you need a code the do.....) I'm really having a difficult time understanding how it can be written in C or C++

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
Active Questions
ADVERTISEMENT