Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C中对.txt文件中的数据进行排序_C_File_Sorting_Text Files - Fatal编程技术网

如何在C中对.txt文件中的数据进行排序

如何在C中对.txt文件中的数据进行排序,c,file,sorting,text-files,C,File,Sorting,Text Files,我是C编程新手。现在我正在用C编写一个程序,它读取.txt文件并将数据存储到另一个txt文件中。例如: 打开20150101.txt 然后把数据放进去 2015010103I 2015010102O 然后将其存储在2015JAN.txt中 目前我在对.txt文件的内容进行排序时遇到问题。你能帮我吗 int intCtr; int intCtr2; int intCtr3; char strTempData[MAX_SIZE]; FILE * ptrFileLog; ptrFileLog =

我是C编程新手。现在我正在用C编写一个程序,它读取.txt文件并将数据存储到另一个txt文件中。例如:

打开20150101.txt

然后把数据放进去

2015010103I
2015010102O
然后将其存储在2015JAN.txt中

目前我在对.txt文件的内容进行排序时遇到问题。你能帮我吗

int intCtr;
int intCtr2;
int intCtr3;
char strTempData[MAX_SIZE];

FILE * ptrFileLog; 
ptrFileLog = fopen(strFileName, "r"); 

while(fgets(strTRLog, MAX_SIZE, ptrFileLog) != NULL) {

FILE * ptrSummary;

ptrSummary = fopen(strFileSummary, "a");

for(intCtr = 0; intCtr < MAX_SIZE; intCtr++) {
    strcpy(strTempCopy[intCtr], strTRLog);
}

for(int intCtr = 0; intCtr < MAX_SIZE; intCtr++) {

    for(int intCtr2 = 6; intCtr2 < 7; intCtr2++) {
        if(strcmp(strTempCopy[intCtr -1], strTempCopy[intCtr]) > 0) {
            strcpy(strTempData, strTempCopy[intCtr]);
            strcpy( strTempCopy[intCtr], strTempCopy[intCtr - 1]);
            strcpy(strTempCopy[intCtr -1], strTempData);
        }
    }
}

for(int intCtr = 0; intCtr < 1; intCtr++) {
    fputs(strTempCopy[intCtr], ptrSummary);
}
  }
  fclose(ptrFileLog);   
 fclose(ptrSummary);

为了解决这个问题,我建议逐行阅读并将其存储在字符串列表中。并使用任何排序算法对列表进行排序,例如:冒泡排序。并将结果打印到新文件中。 当循环不是一个好主意时,不要打开里面的文件。在某些情况下,最终可能会丢失打开文件的处理程序

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

#define MAX_LEN 100 // Length of each line in input file.

int main(void)
{
    char *strFileName = "C:\\Users\\sridhar\\untitled4\\data.txt";
    char *strFileSummary = "C:\\Users\\sridhar\\untitled4\\out.txt";
    char strTempData[MAX_LEN];
    char **strData = NULL; // String List
    int i, j;
    int noOfLines = 0;

    FILE * ptrFileLog = NULL;
    FILE * ptrSummary = NULL;

    if ( (ptrFileLog = fopen(strFileName, "r")) == NULL ) {
        fprintf(stderr,"Error: Could not open %s\n",strFileName);
        return 1;
    }
    if ( (ptrSummary = fopen(strFileSummary, "a")) == NULL ) {
        fprintf(stderr,"Error: Could not open %s\n",strFileSummary);
        return 1;
    }

    // Read and store in a string list.
    while(fgets(strTempData, MAX_LEN, ptrFileLog) != NULL) {
        // Remove the trailing newline character
        if(strchr(strTempData,'\n'))
            strTempData[strlen(strTempData)-1] = '\0';
        strData = (char**)realloc(strData, sizeof(char**)*(noOfLines+1));
        strData[noOfLines] = (char*)calloc(MAX_LEN,sizeof(char));
        strcpy(strData[noOfLines], strTempData);
        noOfLines++;
    }
    // Sort the array.
    for(i= 0; i < (noOfLines - 1); ++i) {
        for(j = 0; j < ( noOfLines - i - 1); ++j) {
            if(strcmp(strData[j], strData[j+1]) > 0) {
                strcpy(strTempData, strData[j]);
                strcpy(strData[j], strData[j+1]);
                strcpy(strData[j+1], strTempData);
            }
        }
    }
    // Write it to outfile. file.
    for(i = 0; i < noOfLines; i++)
        fprintf(ptrSummary,"%s\n",strData[i]);
    // free each string
    for(i = 0; i < noOfLines; i++)
        free(strData[i]);
    // free string list.
    free(strData);
    fclose(ptrFileLog);
    fclose(ptrSummary);
    return 0;
}

首先,您应该重新检查代码流。例如,在while循环中多次打开strFileSummary,最后只关闭一次。使用一些for循环至少很有趣

另外,我也不清楚你到底想在那里实现什么样的排序算法。 我的建议是不要重新发明轮子并使用stdlib的qsort函数。
您应该只需要编写一个比较函数。请参见此处的示例:

确切地说,.txt文件的内容不仅仅是int值?结尾是字母,因此内容存储为字符串。我认为您可以发布您的工作,以及您之前所做的工作。。我们可以改进并帮助您。@AmperSand您能详细说明您遇到的问题吗?