1) fflush(stdin) is undefined behavior as fflush is meant to be used for output streams
FAQ > Why fflush(stdin) is wrong - Cprogramming.com
2) You're trying to store ints in your buffer (You're reading into the buffer then storing into the buffer so it all gets overwritten)
3) Sizeof(int) should just be BUFSIZE, (or maxsize if you really want), or sizeof(userInputText)
4) You need an array of ints to store your numbers
You're code is quite the mess you should clean it up and make sure you're formatting properly and then check if there's easier ways to go about what your doing.
That's good for starters.
Here's your code with the array, and buffer issue fixed.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#define flush fflush(stdin)
#define cls system("cls");
#define pause system("pause")
#define MAXSIZE 100000
void loadArray();
//void displayArray(int numbers[], int count);
int main()
{
loadArray();
return 0;
}
void loadArray()
{
int userInput[MAXSIZE] = {0}, temp=0, count=0, textCount=0;
int counter=0, textCounter=0, i=0, x=0, textTemp=0;
char userInputText[BUFSIZ];
// Array used to store integers read from buffer
int userInts[MAXSIZE];
char fileChoice = ' ';
char viewChoice = ' ';
FILE *binaryPointer;
FILE *textPointer;
printf("\nPress 'B' to work with a binary file.\n");
printf("\nPress 'T' to work with a text file.\n\n");
printf("Enter selection - ");
scanf("%c", &fileChoice);
fileChoice = toupper(fileChoice);
flush;
cls;
if (fileChoice == 'B')
{
binaryPointer = fopen("binaryFile.bin", "rb");
do
{
if(binaryPointer)
{
while(fread(&count, sizeof(int), 1, binaryPointer) > 0)
{
userInput[counter] = count;
counter++;
}
fclose(binaryPointer);
}
else
{
printf("The file does not exist, press any key to create one and write data to it\n");
pause;
cls;
}
binaryPointer = fopen("binaryFile.bin", "ab+");
while (temp != -999)
{
printf("Please enter a number to be saved. Enter -999 view file and exit ");
scanf("%d", &temp);
flush;
if (temp == -999)
{
break;
}
userInput[counter] = temp;
counter++;
count++;
fwrite(&temp, sizeof(int), 1, binaryPointer);
}
}
while (temp!= -999);
fclose(binaryPointer);
}
else if (fileChoice == 'T')
{
textTemp = 0;
textPointer = fopen("textFile.txt", "r");
if (textPointer)
{
while(fgets(userInputText, sizeof(userInputText, textPointer) != NULL)
{
sscanf(userInputText, "%d", &textCount);
userInts[textCounter] = textCount;
textCounter += 1;
}
fclose(textPointer);
}
else
{
printf("The file does not exist, press any key to create one and write data to it\n");
pause;
}
textPointer = fopen("textFile.txt", "a+");
while (textTemp != -999)
{
printf("Please enter a number to be saved. Enter -999 view file and exit ");
scanf("%d", &textTemp);
flush;
if (textTemp == -999)
{
break;
}
userInputText[textCounter] = textTemp;
textCounter++;
fprintf(textPointer, "%d\n", textTemp);
}
fclose(textPointer);
}
else
{
cls;
printf("\nReally! Two choices and you could not get it right. Try again BYE.\n");
}
cls;
printf("Enter B to view the binary file or T to view text file -");
scanf("%c", &viewChoice);
flush;
viewChoice = toupper(viewChoice);
if (viewChoice=='B')
{
cls;
//counter -= 1;
for (i=0; i<counter; i++)
printf("%d", userInput[i]);
pause;
}
else if (viewChoice =='T')
{
cls;
textPointer = fopen("textFile.txt", "a+");
if (textPointer)
{
for (x=0; x<textCounter; x++)
printf("%d\n", userInts[x]);
pause;
}
else
{
printf("This file has not been created yet.\n Please exit and re-enter program to create a text file.\n");
pause;
exit(0);
}
}
else
{
printf("This time you really dont get a second chance!\n");
pause;
exit(0);
}
return 0;
}