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
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++)...
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 +...
Miscellaneous C knowledge. (Put all answers in the same q5.txt file, they’re all short.) (a) [2...
Miscellaneous C knowledge. (Put all answers in the same q5.txt file, they’re all short.) (a) [2 marks] On an old 16-bit computer system and its C compiler,sizeof(double)=8andsizeof(int)=2. Given the two type definitions below, what weresizeof(s)andsizeof(lingling)on that system? Assume that ins, there is no gap between the twofields. typedef struct s { double r[5];int a[5]; } s; typedef union lingling { double r[5];int a[5]; } lingling; (b) [2 marks] Given the following declarations, two questions: What is the type ofq? Whatis...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Code here } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to manipulate the elements...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
why's is my csis.txt saving ? #include <stdio.h> FILE*fp; void calculateBMI(void); int main(void) {    fp...
why's is my csis.txt saving ? #include <stdio.h> FILE*fp; void calculateBMI(void); int main(void) {    fp = fopen("csis.txt", "w"); for (int i= 0; i<4; ++i){ calculateBMI(); } fclose(fp); return 0; } void calculateBMI(void){ double weight,height,bmi;    printf("Enetr your weight in pounds:\n\n"); fprintf(fp,"Enter your weight in pounds:\n\n"); scanf("%lf",&weight); printf("Enter height in inches:\n\n"); fprintf(fp,"Enter height in inches:\n\n"); scanf("%lf",&height); bmi=(703*weight)/(height*height);    if (bmi<18.5){ printf("You have a BMI of %.lf, and your weight status is underweight\n",bmi);    fprintf(fp,"You have a BMI of %.lf, and...
Using the C programming language implement Heapsort in the manner described in class. Here is some...
Using the C programming language implement Heapsort in the manner described in class. Here is some example code to use as a guideline. Remember, you need only implement the sort algorithm, both the comparison and main functions have been provided. /* * * after splitting this file into the five source files: * * srt.h, main.c, srtbubb.c, srtinsr.c, srtmerg.c * * compile using the command: * * gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR |...