line shuffle

kelime ezberleme veya sinav sorularina calisirken sorulari ayni sira ile cozme yerine, kullanici karsisina tum sorularin karistirilarak verilmesi icin yazildi.

// coded by: yasin tasan

#include "stdafx.h"
#include "stdio.h"
#include <stdlib.h>
#include <time.h>
#include<conio.h>

void ReadFileOpen();
int CountLines(char *filename);
void ReadFileClose();
void ShuffleLines();
void swap(int *a, int *b);
void WriteFileOpen();
void WriteFileClose();
void WriteShuffledData();

FILE *ReadFilePointer;
FILE *WriteFilePointer;
errno_t err;
char Line[1000];
unsigned int LineNumber;
int *RandomizeArray;
unsigned char EndChar;
unsigned int LineCounter;

int main()
{
    printf("\n");
    printf("Explanation: Line shuffling in a text file\n");
    printf("Max line length must be 1000 characters\n");
    printf("Input file name must be file.txt\n");
    printf("Output shuffled file name will be shuffled_file.txt\n");
    printf("\n");

    ReadFileOpen();
    WriteFileOpen();

    LineNumber = CountLines("file.txt");
    printf("Total line number: %d\n", LineNumber);
    ShuffleLines();

    WriteShuffledData();

    ReadFileClose();
    WriteFileClose();

    printf("\nShuffling process has done.\n");

    printf("\nPress any key to exit...\n");
    _getch();
}

void WriteShuffledData()
{
    int i = 0;
    LineCounter = 1;

    fseek(ReadFilePointer, 0, SEEK_SET);

    while (i<LineNumber)
    {
        fgets(Line, 1000, ReadFilePointer);

        if (RandomizeArray[i] == LineCounter)
        {
            fputs(Line, WriteFilePointer);
            if (LineCounter == LineNumber)
            {
                fputs("\n", WriteFilePointer);
            }
            i++;
            LineCounter = 1;
            fseek(ReadFilePointer, 0, SEEK_SET);
        }
        else
        {
            LineCounter++;
        }
    }
}

void ShuffleLines()
{
    RandomizeArray = (int *)calloc(LineNumber, sizeof(int));
    srand(time(NULL));

    for (int i = 0; i < LineNumber; i++)
    {
        RandomizeArray[i] = i + 1;
    }

    for (int i = LineNumber - 1; i > 0; i--)
    {
        int j = rand() % (i + 1);
        swap(&RandomizeArray[i], &RandomizeArray[j]);
    }
}

void ReadFileOpen()
{
    if ((err = fopen_s(&ReadFilePointer, "file.txt", "rt")) != 0)
        printf("Read file is not opened. Check the file name. It must be file.txt\n");
    //else
        //printf("Read file is opened\n");
}

void WriteFileOpen()
{
    fopen_s(&WriteFilePointer, "shuffled_file.txt", "w");
    //printf("Write file is opened\n");
}

int CountLines(char *filename)
{
    int ch = 0;
    int lines = 0;

    lines++;
    while ((ch = fgetc(ReadFilePointer)) != EOF)
    {
        if (ch == '\n')
            lines++;
    }
    return lines;
}

void ReadFileClose()
{
    fclose(ReadFilePointer);
    //printf("Read file is closed\n");
}

void WriteFileClose()
{
    fclose(WriteFilePointer);
    //printf("Write file is closed\n");
}

void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}