Skip to content

Instantly share code, notes, and snippets.

@linuxaged
Created August 17, 2012 05:44
Show Gist options
  • Save linuxaged/3376236 to your computer and use it in GitHub Desktop.
Save linuxaged/3376236 to your computer and use it in GitHub Desktop.
c read file into array
#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;
}
Copy link

ghost commented Dec 7, 2019

what 's the pFile?

@linuxaged
Copy link
Author

what 's the pFile?

FILE*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment