Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TABLEROWS 20000
#define TABLECOLUMNS 5
int copytxttodarray(char filename[], double *array, int rows, int columns);
//loads a .txt file(created by copyarraytotxt()) into the array.
int copyfiletoarray(char filename[], double *array, int rows, int columns);
//copies data from a file(created by copyarraytofile()) into the array.
int copyarraytotxt(char filename[], double *array, int rows, int columns);
//copies data from array into a .txt file.
int copyarraytofile(char filename[], double *array, int rows, int columns);
//copies data from array into a file.
int main()
{
int i,j;
double array[TABLEROWS][TABLECOLUMNS];
//here is some code to generate random numbers in the array if you need to test
// srand(time(NULL));
// for(i=0;i<TABLEROWS;i++)
// {
// for(j=0;j<TABLECOLUMNS;j++)
// {
// array[i][j] = rand();
// }
// }
//here are the function calls, uncomment to test them
//copytxttodarray("randinput.txt", array, TABLEROWS, TABLECOLUMNS);
//copyarraytotxt("randoutput.txt", array, TABLEROWS, TABLECOLUMNS);
//copyfiletoarray("randinput", array, TABLEROWS, TABLECOLUMNS);
//copyarraytofile("randoutput", array, TABLEROWS, TABLECOLUMNS);
//printing out the first 120 lines of the array to check for gibberish
for(i=0;i<120;i++)
{
for(j=0;j<TABLECOLUMNS;j++)
{
printf("%15.3lf", array[i][j]);
}
printf("\n");
}
printf("\n\nHave a nice day!");
}
//the functions I made
int copytxttodarray(char filename[], double *array, int rows, int columns)
{
FILE *fp;
int i, stoppoint;
fp = fopen(filename,"r");
stoppoint = rows*columns;
for(i=0;i<stoppoint;i++)
fscanf(fp, "%lf", array++);
fclose(fp);
return 0;
}
int copyarraytotxt(char filename[], double *array, int rows, int columns)
{
FILE *fp;
int i, j;
fp = fopen(filename,"w");
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
fprintf(fp, "%.16e ", *(array++));
}
fprintf(fp, "\n");
}
fclose(fp);
return 0;
}
int copyarraytofile(char filename[], double *array, int rows, int columns)
{
FILE *fp;
fp = fopen(filename, "w");
fwrite(array, sizeof(double),rows*columns,fp);
fclose(fp);
return 0;
}
int copyfiletoarray(char filename[], double *array, int rows, int columns)
{
FILE *fp;
fp = fopen(filename, "r");
fread(array,sizeof(double),rows*columns,fp);
fclose(fp);
return 0;
}