Question

LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the...

LANGUAGE C

The codes below reads another .c or .txt file. Then, it will read the contents of every line and store the contents into a string array.

Example: .c file that is to be read:

#include <stdio.h>

int main(){
   printf("Hello World");
   return ;
}

_______________(END OF THE FILE)_______________

Therefore, the code will read the contents above and store the contents in every line into an array, such that....

array[0] = "#include <stdio.h>\n"

array[1] = "\n"

array[2] ="int main(){\n "

and it goes on....

The codes below are my uncompleted code, can sir/madam show me the correct steps to do so? Any assistance is appreciated in advance.

#include<stdio.h>

int main(int argc,char **argv)
{  
   char array[100];
   FILE *fp=fopen(argv[1],"r");
  
   fclose(fp);

   return 0;
}

NOTE: I use gcc compiler in unix environment, and the contents of the .c or .txt file is not fixed, i.e. the number of lines is not fixed.

Homework Answers

Answer #1

Please comment, if you need any corrections.Please give a Thumps up if you like the answer.

Program

#include<stdio.h>
#include <string.h>

int main(int argc,char **argv)
{
   char array[100][100];
   int num_rows=0;
   FILE *fp=fopen(argv[1],"r");
   if(fp == NULL)
   {
       perror("Error opening file");
       return(-1);
    }
  
   while (!feof(fp)) {
       fgets (array[num_rows], 60, fp);
       num_rows++;
   }

   fclose(fp);
  
   for(int i=0;i<num_rows;i++)
       printf("%s",array[i]);
   return 0;
}

Output

./a.out hello.c


#include <stdio.h>
int main()
{
printf("Hello World");
return ;
}

Know the answer?
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
Similar Questions
PLEASE EXPLAIN THE ANSWER The file data1 contains: 0 3 6 9 12 The file data2...
PLEASE EXPLAIN THE ANSWER The file data1 contains: 0 3 6 9 12 The file data2 contains: 10 8 6 4 2 The file data3 contains: 20 10 0 20 80 Give the output when run with: ./a.out data1 data2 data3 #include <stdio.h> int main( int argc, char *argv[] ) { FILE *fpOne = fopen(argv[1], "r"); FILE *fpTwo = fopen(argv[2], "r"); FILE *fpThree = fopen(argv[3], "r"); int a, num1, num2, num3, sum1=0, sum2=0; for (a=0; a<5; a++) { fscanf(fpOne, "%d",...
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100];...
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100]; while(1) { printf("Welcome to my shopping list\n\n"); printf("Main Menu:\n"); printf("1. Add to list\n"); printf("2. Print List\n"); printf("3. Delete List\n"); printf("4. Remove an item from the List\n"); printf("5. Exit\n\n"); scanf("%i", &choice); switch(choice) { case 1: //add to list //get the input from the user printf("Enter item: "); scanf("%s", item); //open the file fp = fopen("list.txt","a"); //write to the file fprintf(fp, "\n%s", item); //close the file...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h>...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h> #define SIZE 5 int main(int argc, char *argv[]) {    int numerator = 25;    int denominator = 10;    int i = 0;    /*    You can assume the files opened correctly and the    correct number of of command-line arguments were    entered.    */    FILE * inPut = fopen(argv[1], "r");    FILE * outPut = fopen(argv[2], "w");    float result =...
Write a program in C that takes a file name as the argument on the command...
Write a program in C that takes a file name as the argument on the command line, and, if the file is a symbolic link, prints out the contents of that link (i.e. the file name that the link points to). If it is not a symbolic link, the program should print an error int main ( int argc , char * argv [] ) { // Check if the user gave an argument , otherwise print " ERROR :...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char...
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 extern char **environ;    5 void output(char *a[], char *b[]) { 6 int c = atoi(a[0]); 7 for (int i = 0; i < c && b[i]; ++i) { 8 printf("%s", b[i]+2); 9 } 10 } 11 12 void main(int argc, char *argv[]) { 13      14 switch (argc) { 15 case 1: 16 for (int i = 0; environ[i]; ++i) {    17 printf("%s\n", environ[i]); 18 } 19 break; 20 default: 21 output(argv +...
I am attempting to cause a simple stack buffer overflow in the C language. This is...
I am attempting to cause a simple stack buffer overflow in the C language. This is the code I have: #include <stdio.h> #include <string.h> int main(int argc, char *argv[]){ char buffer[10]; strcpy(buffer,argv[1]); }; When I run this code I use the input "0123456789" a total of 10 bytes and I recieve an error that the core was dumped. But I thought since I defined a buffer of size 10 any input greater than 10 would deliver that error, inputs less...
#include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]){ int fd1, fd2;...
#include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> int main(int argc, char *argv[]){ int fd1, fd2; char buffer[100]; long int n1; if(((fd1 = open(argv[1], O_RDONLY)) == -1) || ((fd2 = open(argv[2], O_CREAT|O_WRONLY|O_TRUNC,0700)) == -1)){ perror("file problem "); exit(1); } while((n1=read(fd1, buffer, 512) > 0)) if(write(fd2, buffer, n1) != n1){ perror("writing problem "); exit(3); } // Case of an error exit from the loop if(n1 == -1){ perror("Reading problem "); exit(2); } close(fd2); exit(0); } There is an issue with this...