Okay...I changed some things on it and got it a little closer to working properly....It may help more if I display the full program....The file that the program is reading from has the following in it.
2
2
3
3
4
5
5
6
6
7
7
8
8
9
The file that the program writes to after the program is run displays the following.
5
3
2
which isn't quite right. Here is my updated code.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *fptr;
int main()
{
int array[15];
int i=0;
int j=0;
int k=0;
int MaxElements = 15;
char readFile[30];
char writeFile[30];
printf("What is the filename that you're going to read from: ");
scanf("%s",readFile);
printf("What is filename that you're going to write to: ");
scanf("%s",writeFile);
//open the file
fptr = fopen(readFile,"r");
if((fptr = fopen(readFile, "r")) == NULL)
exit(1);
//read input
while(fscanf(fptr,"%d",&array[i]) != NULL)
{
++i;
if(i >= MaxElements) break;
}
--i;
fclose(fptr);
for (i = 0; i<MaxElements; i++)
{
for (j = i; j<MaxElements ; j++)
{
if ( array[i] == array[j] )
for (k=j; k < MaxElements; k++)
{
array[k] = array[k+1];
MaxElements = MaxElements - 1;
}
}
}
fptr = fopen(writeFile, "w");
if(fptr)
{
while(i > 0)
{
fprintf(fptr, "%d\n", array[--i]);
}
fclose(fptr);
}
}