Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void readFile(int*, int*);
- #define FILE_NAME "numbers.txt"
- void main(void)
- {
- int *array; //main-array with numbers
- int rows; //number of elements in array
- readFile(&array, &rows);
- system("pause");
- }
- void readFile(int *arr, int *pRows)
- {
- int rowCount; // number of rows i file (except the first row)
- int num = 0; // temporary storage
- int i;
- FILE *infile;
- infile = fopen(FILE_NAME, "r");
- if (infile == NULL)
- {
- printf("couldn't open file");
- exit(1);
- }
- fscanf(infile, "%d", &rowCount); //reads first row in file
- int *integers = malloc(sizeof(int)* rowCount);
- for (i = 0; i < rowCount; i++)
- {
- fscanf(infile, "%d", &num);
- integers[i] = num;
- }
- for (i = 0; i < rowCount; i++) //copies numbers from internal array to main array
- {
- *(arr + i) = integers[i];
- }
- *pRows = rowCount;
- fclose(infile);
- free(integers);
- }
Advertisement
Add Comment
Please, Sign In to add comment