Hi folks,
Have been having some issues with fileIO today, I am trying to read from a file containing the alphabet backwards and then perform a merge sort to put it back into order and then writing it back to a different file.
Everything is fine up until the write operation, that is the array from the merge contains the alphabet in the correct order.
Code:
#include "stdafx.h"
#include "stdio.h"
FILE *fp = 0;
FILE *outfp = 0;
unsigned int store = 0;
unsigned char i = 0;
unsigned int temp = 0;
unsigned char alphabet[26];
unsigned int input[26];
unsigned int scratch[26];
void merge_reccur(unsigned int *input, unsigned int left, unsigned int right, unsigned int *scratch);
void merge_sort(unsigned int *input, const unsigned int size);
int _tmain(int argc, _TCHAR* argv[])
{
//Note the double back slashes to stop escape chars being generated.
fp = fopen("C:\\Users\\Pete\\Desktop\\fileio.txt", "r+b"); //Read and write binary.
fread(alphabet, sizeof(char), sizeof(alphabet), fp); //Read in the alphabet (backwards).
for(i = 0; i < 26; i++)
input[i] = alphabet[i]; //Copy into integer array
merge_sort(input, (sizeof(input)/sizeof(int))); //Merge sort the array.
for(i = 0; i < 26; i ++)
alphabet[i] = input[i]; //Copy sorted array back into char array.
for(i = 0; i < 26; i ++)
printf("%c", alphabet[i]); //Print sorted results back to console.
fclose(fp);
//Write to a different text file.
outfp = fopen("C:\\Users\\Pete\\Desktop\\output.txt", "wb");
for(i = 0; i < 26; i++)
fprintf(outfp, "%c", alphabet[i]);
temp = fwrite(alphabet, 1, sizeof(alphabet), outfp);
printf("%d", temp);
fclose(outfp);
while(1);
}
I have tried to use both fprintf and fwrite but neither seem to be working, although the value returned from fwrite is 26 which would seem to indicate that the array is being written somewhere!
It would be greatly appreciated if someone could shed anylight on this ? I am using a win32 console application template on win64, could that make a difference??
Cheers,
Pete.