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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started