Question: This is about C Programming. Q. Implement a function get_numbers and call it in the main function. The function should read one line of the

This is about C Programming.

Q.

Implement a function get_numbers and call it in the main function. The function should read one line of the file which includes integers, and return an integer pointer for the integers.

- do not assume the length of array. -read one number and use it to allocate memory space in get_numbers. -free the memory space in the main function

I wrote some codes for the question, but it doesn't work proplerly...

please fix my code to make it work properly.

#include #include

int *get_numbers(char filename[],int* line);

int main() { int i =0; int *linep = NULL; int numArr[] = {0};

linep = get_numbers("hw4_2_input.txt",linep); while(linep[i]!=" "){ numArr[i] = linep[i]; if(linep == " "){ putchar(" "); }else{ printf("%d ", numArr[i]); } i++; } free(linep); system("PAUSE"); return 0; }

int *get_numbers(char filename[],int* line) { int N = 0; int i = 0;

FILE *data = fopen(filename,"r"); if(data == NULL){ printf("Failed to open file! "); return 0; }

fscanf(data,"%d",&N); line = (int*)malloc((N+1)*sizeof(int)); printf("%d ",N); for(i=0;i

fclose(data); return line; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!