Question

In: Computer Science

Create Lab project Add the following codes to your main method. Compile and run the program...

  1. Create Lab project
  2. Add the following codes to your main method. Compile and run the program with some arguments and observe the output.

      printf("You have entered %d arguments.\n", argc);

      for (int i = 0; i < argc; ++i)

        printf( "argument %d: %s\n",i+1,argv[i]);

  1. Create a function called simpleFileWrite() and add the following feature: Open a write only file called “myfile.txt” and write characters into it. The set of characters are read form the keyboard until cntrl-z is entered. (use putc() and getc() function; in your c code use EOF to compare with cntrl-z).
  2. Compile, build and run the program. Now print the file contents on the console using the linux command “cat myfile.txt” and observe the contents.
  3. Create a function called simpleFileRead() and add the following feature: Open the file “myfile.txt” as a read only file from within your program and read characters from it and print it on the console using the printf command.
    1. Also count the number of characters in the file
    2. Observe the output to compare whether it matches with the entries that you made from the keyboard earlier.
  4. Create a function called stringHandler() and add the following feature: Write set of strings each of length 40 into a file “stringc.txt”. Close the file and then reopen to read. Now display the contents of the file (use fputs() and fgets() function).
  5. Create a function called mixedDataHandler() and the add the following feature: Write name, age and height of a person into a data file “person.txt”. Close the file and reopen it to read from it (use fprintf() and fscanf() function).
  6. Create a C file called myCopy.c, which will process command line arguments. The first argument will be the name of the program itself, the second argument will be the source file to copy from, and the 3rd argument will be the destination file to copy to. If the user doesn’t provide argument 2 then show error message that “Source file name needed”. If the user does not provide the 3rd argument then use “default.txt” as the name of the output file.

please help me, this is c programming. thank you in advance.

Solutions

Expert Solution

(a).Program

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void simpleFileWrite();
void simpleFileRead();
int countCharcter();

int main()
{
   int count;
   simpleFileWrite();
simpleFileRead();
   count=countCharacter();
//   printf("Number ofcharacter in file is %d\n",count);
  
   return 0;
}

void simpleFileWrite()
{
   FILE *fp;
   char ch;

   //Create the file for write operation
   fp=fopen("myfile.txt","w");
   if(fp==NULL)
   {
       printf("Error on writing\n");
       exit(0);
   }
   printf("Enter line of text until Ctrl+z\n");
   while((ch=getchar())!=EOF)
   {
   putc(ch,fp);
   }

   fclose(fp);
   printf("File created..\n");

}

void simpleFileRead()
{
   FILE *fp;
   char ch;
   fp=fopen("myfile.txt","r");
   if(fp==NULL)
   {
       printf("Error on writing\n");
       exit(0);
   }
   printf("\n");
   while(!feof(fp))
   {
       //takes the characters in the character array
       ch=getc(fp);
       //and print the characters
       printf("%c",ch);
   }
   printf("\n");

   fclose(fp);
}
int countCharacter()
{
   FILE *fp;
   int count=0;
   fp=fopen("myfile.txt","r");
   if(fp==NULL)
   {
       printf("Error on writing\n");
       exit(0);
   }

   while(!feof(fp))
   {

       getc(fp);
       count++;

   }
   printf("\n");

   fclose(fp);
   return (count-1) ;
}

(b) Program

#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void stringHandle();
void mixedDataHandler();
int main()
{

   stringHandle();
   mixedDataHandler();
   return 0;
}

void stringHandle()
{
   FILE *fp;
   char str[40];
   int n,i;

   fp=fopen("string.txt","w");
   if(fp==NULL)
   {
       printf("Error on writing\n");
       exit(0);
   }
   printf("Enter number of strings towrite\n");
   scanf("%d",&n);
   getchar();
   printf("Enter %d strings\n",n);
   for(i=0;i<n;i++)
   {
   gets(str);

   fputs(str,fp);
   fputs("\n",fp);
   }

   fclose(fp);
   printf("File created..\n");
   fp=fopen("string.txt","r");
   if(fp==NULL)
   {
       printf("Error on writing\n");
       exit(0);
   }

   printf("String in the files are....\n");
   while(!feof(fp))
   {
   fgets(str,40,fp);

       puts(str);


   }

   fclose(fp);

}
void mixedDataHandler()
{
   FILE *fp;
   char name[40];
   int n,i,age;
   float height;

   fp=fopen("person.txt","w");
   if(fp==NULL)
   {
       printf("Error on writing\n");
       exit(0);
   }
   printf("Enter number of records\n");
   scanf("%d",&n);
   getchar();
   printf("Enter %d records\n",n);
   for(i=0;i<n;i++)
   {
   printf("Name Age Height?\n");
   scanf("%s%d%f",name,&age,&height);
   fprintf(fp,"%s %d %f",name,age,height);

   }


   fclose(fp);
   printf("File created..\n");
   fp=fopen("person.txt","r");
   if(fp==NULL)
   {
       printf("Error on writing\n");
       exit(0);
   }

   printf("String in the files are....\n");
   while(!feof(fp))
   {
   fscanf(fp,"%s%d%f",name,&age,&height);
   printf("%s %d %f\n" ,name,age,height);
   }


   fclose(fp);

}


Related Solutions

Define Loan Class – Add to your project. And write program in main method to test...
Define Loan Class – Add to your project. And write program in main method to test it. Note: Assume year is number of years, rate is the annual interest rate and P is principle is loan amount, then the total payment is Total payment = P *(1+ rate/12)^ year*12; Monthly Payment = TotalPayment/(year*12); java
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the...
UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the following commands, and at the end, use CTRL+C to terminate the program: ls ls -l tryShell* date whoami hostname uname -a ctrl+C    (2) Run the program (tryShell) with "time -p" with a few commands: time -p ./tryShell (3) Edit the program (tryShell.c) so that it will exit (terminate the program) when the input command string is "exit" try shell.c code at bottom //////////// #include...
Lab Text Manipulation Inside the main method, do the following: Create an ArrayList of strings and...
Lab Text Manipulation Inside the main method, do the following: Create an ArrayList of strings and call it parks. Read in the names of national parks from the user until the user enters done(or DONE, or dOnE, .. ) Keep in mind, that the names of some national parks consist of more than one word, for example, Mesa Verde. As you read in the national parks, add them to the list. Next, we are going to build a string based...
** USING MATLAB TO PROGRAM The main objective of this lab is to create a game...
** USING MATLAB TO PROGRAM The main objective of this lab is to create a game that involves betting on the sum of two dice. The player will start out with some initial total amount of money. During each round, the player can bet some money that the sum of the two dice will be equal to a certain number. If the player wins the bet, that player adds the amount of the bet to his or her current total....
Complete the attached program by adding the following: a) add the Java codes to complete the...
Complete the attached program by adding the following: a) add the Java codes to complete the constructors for Student class b) add the Java code to complete the findClassification() method c) create an object of Student and print out its info in main() method of StudentInfo class. * * @author * @CS206 HM#2 * @Description: * */ public class StudentInfo { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application...
in C++ Compile and run the program for the following values: r = 2 Cm, h...
in C++ Compile and run the program for the following values: r = 2 Cm, h = 10 Cm. The answer should be: The cross section area of the cylinder is 3.8955634 c The side area of the cylinder is 19.474819 inch-sqr Did you get the same answer? Explain the reason for such an error and fix the problem. Modify the previous program to include a new function called total_area, that computes the total suface area of a cylinder. The...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user...
Java Codes: 1. Create a class named Proficiency1Practice In the main method, first ask the user to enter a short sentence which ends in a period. Use Java String class methods to determine the following about the sentence and display in the console: • How many total characters are in the sentence? • What is the first word of the sentence? Assume the words are separated by a space. • How many characters are in the first word of the...
Create a C program that performs the following (please comment the codes): a) Create a Stack...
Create a C program that performs the following (please comment the codes): a) Create a Stack ADT. Stack should be implemented using the linked list. b) Enter 10 random integer numbers between 0 to 50 in the stack. c) After pushing each element, print the content of the top of the stack. c) Then pop out those 10 integer numbers and print those numbers. d) Finally destroy the Stack.
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT