读取文本文件并在新文件中对其进行排序

时间:2016-01-11 18:58:54

标签: c file sorting

我有一个学校作业,我们必须阅读文本文件,按字母顺序对单词进行排序,并将结果写入新的文本文件。

我已经有一个程序可以读取文件并将其打印在屏幕上,另外一个程序可以对您必须输入的单词进行排序。现在我试图合并这两个程序,以便从文件中读出的数据将被放入分拣程序中。

我们用来制作代码的程序叫做CodeBlocks。以下是两个程序。我希望你能给我一些建议和一个如何解决这个问题的例子,因为我尝试了所有我知道的但无法使它发挥作用。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>
#define MAX_NUMBER_WORDS 100
char* ReadFile(char *filename)
{
    char *buffer = NULL;
    int string_size, read_size;
    FILE *handler = fopen(filename, "r");

    if (handler)
    {
        //seek the last byte of the file
        fseek(handler, 0, SEEK_END);
        //offset from the first to the last byte, or in other words, filesize
        string_size = ftell(handler);
        //go back to the start of the file
        rewind(handler);

        //allocate a string that can hold it all
        buffer = (char*)malloc(sizeof(char) * (string_size + 1));
        //read it all in one operation
        read_size = fread(buffer, sizeof(char), string_size, handler);
        //fread doesnt set it so put a \0 in the last position
        //and buffer is now officialy a string
        buffer[string_size] = '\0';

        if (string_size != read_size)
        {
            //something went wrong, throw away the memory and set
            //the buffer to NULL
            free(buffer);
            buffer = NULL;
        }
    }

    return buffer;
}

int numberOfWordsInDict(char **dict)
{
    int i;
    for (i = 0; i < MAX_NUMBER_WORDS; i++)
    {
        if (dict[i] == NULL)
            return i;
    }
    return MAX_NUMBER_WORDS;
}

void printDict(char **dict)
{
    int i;
    printf("Dictionary:\n");
    for (i = 0; i < numberOfWordsInDict(dict); i++)
        printf("- %s\n", dict[i]);
    if (numberOfWordsInDict(dict) == 0)
        printf("The dictionary is empty.\n");
}


void swapWords(char **dict, char *word, char *word2)
{
    int i, p1 = -1, p2 = -1;
    char *tmp;
    for (i = 0; i < numberOfWordsInDict(dict); i++)
    {
        if (strcmp(dict[i], word) == 0)
            p1 = i;
        if (strcmp(dict[i], word2) == 0)
            p2 = i;
    }
    if (p1 != -1 && p2 != -1)
    {
        tmp = dict[p1];
        dict[p1] = dict[p2];
        dict[p2] = tmp;
    }
}

void sortDict(char **dict)
{
    int swap;
    int i = 0;
    do
    {
        swap = 0;
        for (i = 0; i < numberOfWordsInDict(dict) - 1; i++)
        {
            if (strcmp(dict[i], dict[i + 1]) > 0)
            {
                swapWords(dict, dict[i], dict[i + 1]);
                swap = 1;
            }
        }
    } while (swap == 1);
}

void splitSentenceToWords(char **words, char *sentence)
{
    int p1 = 0, p2 = 0;
    int nrwords = 0;
    char *word;
    while (sentence[p2] != '\0')
    {
        if (isspace(sentence[p2]) && p1 != p2)
        {
            word = (char*)malloc(sizeof(char)*(p2 - p1 + 1));
            words[nrwords] = word;
            strncpy(words[nrwords], &sentence[p1], p2 - p1);
            words[nrwords][p2 - p1] = '\0';
            nrwords++;
            p1 = p2 + 1;
            p2 = p1;
        }
        else
        {
            p2++;
        }
    }
    if (p1 != p2)
    {
        word = (char*)malloc(sizeof(char)*(p2 - p1 + 1));
        words[nrwords] = word;
        strncpy(words[nrwords], &sentence[p1], p2 - p1);
        words[nrwords][p2 - p1] = '\0';
        nrwords++;
        p1 = p2 + 1;
        p2 = p1;
    }
}

int main(void)
{
    char sentence[1024];
    char *dict[MAX_NUMBER_WORDS] = {};
    char *words[MAX_NUMBER_WORDS] = {};
    char *string = ReadFile("test.txt");
    if (string)
    {
        puts(string);
        free(string);
    }
    //printf("Type een zin in: ");
    scanf("%[^\n]s", &sentence);
    splitSentenceToWords(words, &sentence);
    printDict(words);
    printf("Words has been sorted\n");
    sortDict(words);
    printDict(words);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

你走在正确的轨道上。问题是,在读入文件后,您没有使用输入来构建单词列表。而不是;

splitSentenceToWords(words, &sentence);

尝试:

splitSentenceToWords(words, &string);

删除
free(string)

这将帮助您入门。当你理解它时,你将不得不清理它。