Created
August 17, 2012 05:44
-
-
Save linuxaged/3376236 to your computer and use it in GitHub Desktop.
c read file into array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define DAT_MAX_LINES 20 | |
int main(void) | |
{ | |
int array[DAT_MAX_LINES][2]; | |
int n = 0; | |
int i; | |
FILE * pRead = fopen("test.dat", "r"); | |
if (!pRead) | |
{ | |
printf("File cannot be opened\n"); | |
return EXIT_FAILURE; | |
} | |
printf("Contents of test.dat:\n"); | |
while ( (n < DAT_MAX_LINES) && (!feof(pRead)) ) | |
{ | |
fscanf(pRead,"%d%d\n", &array[n][0], &array[n][1]); | |
++n; | |
} | |
if ( (!feof(pFile)) && (n == DAT_MAX_FILES) ) | |
{ | |
printf("Error: file to large for array.\n"); | |
} | |
fclose(pFile); | |
for (i = 0; i < n; ++i) | |
{ | |
printf("%d: %d, %d\n", i, array[i][0], array[i][1]); | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what 's the pFile?