从文件中读取行,并创建按字母顺序排序的数组 [英] Read lines from a file and create alphabetically sorted array

查看:134
本文介绍了从文件中读取行,并创建按字母顺序排序的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习C和我想这样做特定的任务。我知道有一些类似的问题和答案,但仍...我会尝试更具体。比方说,我有如下的文件:

  program01
程式
aprogram
1program
PROG
5program

和我现在想用一个数组:

  1program
5program
aprogram
PROG
program01
程式

所以,只有拉丁小字母和数字字符串中,不能有空格。我知道如何执行一些独立的步骤,但想要得到和感觉整个(和正确)的概念,可以这么说。也许这可以首先从文件中读取时的飞行做一些整理决策?手动排序pferred对我的具体情况$ P $,只为更好的学习和可能的优化的缘故。比方说,一条线的最大长度为256,行最大数目为提前256感谢。


解决方案

 以下完全编译
不过,我没有测试它您可能要修改它从文件名
命令行#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;的#define MAX_ROWS(256)
#定义MAX_COLUMNS(256)
#定义FILE_NAMEmyInputFile//原型
无效bubbleSortWordsArray(的wordCount INT);
无效printWordsArray(的wordCount INT);静态字符字[MAX_ROWS] [MAX_COLUMNS] = {{'\\ 0','\\ 0'}};INT主要(无效)
{
    FILE * FP = NULL;    如果(NULL ==(FP = FOPEN(FILE_NAME,R)))
    {
        PERROR(FOPEN失败);
        出口(EXIT_FAILURE);
    }    //暗示一样,成功的fopen    //读取文件中的每一行成词数组项
    INT I = 0;
    而(与fgets(字[I],MAX_COLUMNS,FP))
    {
        //删除字符串结尾的换行符
        字[I] [strlen的(字[I]) - 1] ='\\ 0';
        我++;
    }     //'我'中含有的话有效条目数[] []
    //字符串数组排序
    bubbleSortWordsArray(ⅰ);    printWordsArray(ⅰ);    返回(0);
} //结束功能:主
无效bubbleSortWordsArray(INT的wordCount)
{
    INT℃;通过行//外指数
    INT D组; //通过行内指数
    交换的char [MAX_COLUMNS] = {'\\ 0'};    为(C = 0; C<(的wordCount - 1),C ++)
    {
        为(D = 0; D&≤(的wordCount - C - 1),D +)
        {
            如果(0 GT;的strcmp(字并[d],词语并[d + 1]))
            {//然后的话需要被交换
                的strcpy(交换,字[D]。);
                的strcpy(字[D]字[D + 1]);
                的strcpy(字[D + 1],交换);
            } // end如果比较/交换
        } //结束了
    } //结束对各行
} //函数结束:bubbleSortWordsArray
无效printWordsArray(INT的wordCount)
{
    INT I; //循环索引    的printf(\\ n); //启动新的输出线
    对于(i = 0; I<的wordCount;我++)
    {
        的printf(%S \\ n,字[I]);
    }
} //函数结束:printWordsArray

I am learning C and I want to do this specific task. I know there is a number of similar questions and answers, but still... I will try to be more specific. Lets say, I have a file with following lines:

program01
programs
aprogram
1program
prog
5program

And I want now an array with:

1program
5program
aprogram
prog
program01
programs

So there are ONLY latin small letters and numbers in strings, no spaces. I know how to perform some separate steps, but want to get and feel the whole (and proper) concept, so to say. Probably it could make some sorting decisions on the fly when reading from file first? Manual sort is preferred for my particular case, just for the sake of better learning and possible optimisation. Lets say, maximal length of one line is 256, maximal number of lines is 256. Thanks in advance.

解决方案

The following cleanly compiles
however, I have not tested it

you might want to modify it to get the file name from 
the command line

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_ROWS (256)
#define MAX_COLUMNS (256)
#define FILE_NAME "myInputFile"

// prototypes
void bubbleSortWordsArray( int wordCount );
void printWordsArray( int wordCount );

static char words[MAX_ROWS][MAX_COLUMNS] = {{'\0','\0'}};

int main(void)
{
    FILE *fp = NULL;

    if( NULL == (fp = fopen( FILE_NAME, "r") ) )
    {
        perror( "fopen failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    // read each line from file into entry in words array
    int i = 0;
    while( fgets(words[i], MAX_COLUMNS, fp ) )
    {
        // remove trailing newline from string
        words[i][strlen(words[i])-1] = '\0';
        i++;
    }

     // 'i' contains number of valid entries in words[][]
    // sort the array of strings
    bubbleSortWordsArray(i);

    printWordsArray(i);

    return(0);
} // end function: main


void bubbleSortWordsArray( int wordCount )
{
    int c;  // outer index through rows
    int d;  // inner index through rows
    char swap[MAX_COLUMNS] = {'\0'};

    for (c = 0 ; c < ( wordCount - 1 ); c++)
    {
        for (d = 0 ; d <  (wordCount - c - 1); d++)
        {
            if(  0 > strcmp( words[d], words[d+1] ) )
            { // then words need to be swapped
                strcpy( swap, words[d]  );
                strcpy( words[d], words[d+1]);
                strcpy( words[d+1], swap );
            } // end if compare/swap
        } // end for
    } // end for each row
} // end function: bubbleSortWordsArray


void printWordsArray( int wordCount )
{
    int i; // loop index

    printf( "\n" ); // start on new output line
    for( i=0; i<wordCount; i++ )
    {
        printf( "%s\n", words[i] );
    }
} // end function: printWordsArray

这篇关于从文件中读取行,并创建按字母顺序排序的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆