The following code reads a .txt file that contain only numbers. Read the first one, create a dynamic array and put the other numbers on it, made some procedures and in the end write a file with the int total.
I want to know if there is a way to run the code faster (the maximum procedure time should be 1s) if I had a .txt file with 1,000,000 numbers.
Is there a more stable way to write this code? Do I have to take all the restrictions I could?
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
static int total = 1;
int process( int *my_array, int *endp) {
int a, b;
//static int total = 1;
if ( my_array == 0 ) return 0;
if ( my_array == endp ) return INT_MIN;
else a = *my_array++;
if ( (b= process( my_array, endp )) == INT_MIN ) return a;
if ( a > b )
{total++; return printf( "%d > %d and total now is %d\n", a, b, total ), a; }
return b;
}
int main ()
{
int t_size;
FILE * fp;
fp = fopen ("xxx.txt", "r");
if (fp == NULL) // error here if there was a problem!
{
printf("Opening 'xxx.txt file failed: %s\n",strerror(errno)); // No such file or directory
getchar(); // wait the user press a key
return 0; // returning an int of 0, exit the program
}
else {
fscanf(fp, "%d", &t_size);
int readch = 0;
long int filepos = 0L;
filepos = ftell ( fp); // get the file position
while ( ( readch = fgetc ( fp)) != EOF) { // read each character of the file
if ( isalpha ( readch)) { // check each character to see if it is a letter
printf ( "File contains letters\n"); // print error message
fclose ( fp); // close file
getchar(); // wait for the user to press a key
return 1; // exit the program
}
}
fseek(fp, 0, SEEK_END); // check to see if the file contains characters!
if (ftell(fp) == 0) { // if not...
printf("The file can't be empty\n"); // empty file error
getchar(); // wait the user press a key
return 1;
}
fseek(fp, 0, SEEK_SET);
fseek ( fp, filepos, SEEK_SET);
if(t_size<1 || t_size>1000000) // set restrictions to integer
{
printf("The number must be 1<= N <= 1.000.000",strerror(errno)); // error with the integer number
getchar(); // wait the user press a key
return 0; // returning an int of 0, exit the program
}
else
{
printf("Create a size of %d array\n", t_size);
int* my_array = NULL;
my_array = malloc(t_size*sizeof(*my_array));
if (my_array==NULL) {
printf("Error allocating memory!\n"); //print an error message
return 1; //return with failure
getchar();
}
int i =0;
for ( i = 0; i < t_size; i++ )
{
fscanf(fp, "%d",&my_array[i]); /*p[i] is the content of element at index i and &p[i] is the address of element at index i */
}
//if all working smoothly...
printf("All up and running! Array created! :D \n\n Array values: \n");
for(i = 0; i < t_size; i++ )
{
printf(" %d : %d\n", i, my_array[i]);
}
printf("\n");
process(my_array, my_array + t_size);
printf("\n%d kids see the entrance!\n\n\nI made it!!! The program finished! \nCopyright George ", total);
getchar();
free(my_array);
}
fclose(fp);
}
FILE *fp_write;
fp_write = fopen("test.txt", "w+");
fprintf(fp_write, "%d", total);
fclose(fp_write);
}