Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to input a file to a 2D array, line by line, by calling a function (not in main) through pointers or something similar?
    text
    copied!<pre><code>void input(FILE*, char*); int main() { int operation, l = 0; int a = 0; char keywords[6][10]={"char ", "while(", "while "}; int keyword_count, line_count=0; char source_code[128][128]; char *str_ptr = &amp;source_code[0][0]; FILE *file_in; file_in=fopen("code.c", "r"); if (file_in==NULL) // Checks if file is opened properly { perror("Error opening file"); } else { printf("\n\n This Program will measure some simple software metrics;"); printf("\n\n Please make a selection:\n\n 0 : Number of lines\n\n "); scanf("%d", &amp;operation); //input(file_in, str_ptr); </code></pre> <p>This is the code block I am using to input my file to an array and would like this to be done in a seperate function (possibly by pointers?)</p> <pre><code> while(!feof(file_in)) { fgets(source_code[l],sizeof(source_code[l]),file_in); line_count++; l++; } while(a&lt;line_count) { if(strstr(source_code[a],keywords[0])) { keyword_count++; a++; } } switch (operation) { case 0: printf("\n There are %d lines of code\n", line_count); break; default: printf("\n Bad Command. Please try again: "); break; } } } </code></pre> <p>Input function, where i tried to place code, and save to array line by line, but failed miserably</p> <pre><code>/* void input(FILE *file_in, char* str_ptr) { } */ </code></pre> <blockquote> <p>added, my failed attempt! not quite sure if this is how the array should be passed but I am looking to have the original copy of the array altered aso as it can be used by other functions</p> </blockquote> <pre><code>void input(FILE *file_in, char* str_ptr) { int i = 0; while(!feof(file_in)) { fgets(*str_ptr[i],128,file_in); i++; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload