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

2024-04-24

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

open 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);

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

#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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

  • 如何验证文本文件中的用户名和密码? | Winforms C#

    首先我制作了textbox1 用于用户名 textbox2 用于密码 和button1 检查 后 private void button1 Click object sender EventArgs e FileStream fs new
  • 为什么json序列化器不符合多态性?

    我在 NET 4 5 Windows 应用商店应用程序中使用库存 JSON 序列化器 System Runtime Serialization Json DataContractJsonSerializer 我有一个由 API 提供商提供的
  • 不带()的sizeof有什么作用? [复制]

    这个问题在这里已经有答案了 作者是这个问题 https stackoverflow com questions 18898410 2 dimensional array simple understanding当我问他什么时 他只是取笑我s
  • ASP.NET Core 3:如何在自定义库中引用 3.0.0 程序集?

    我看到引用的应用程序Microsoft AspNetCore App框架 又称为 ASP NET Core 3 0 使用程序集中的类型Microsoft AspNetCore Mvc Abstractions Version 3 0 0 0
  • 类似于 Active Directory 中的搜索

    我正在使用 C 中的以下代码搜索 LDAP 以轮询用户的活动目录 DirectoryEntry entry new DirectoryEntry ldapPath userName password DirectorySearcher Se
  • Request.Url.Port 给出错误的端口

    我的支持团队为我提供了一个 Live IP 例如http 201 121 152 168 68 现在在正常情况下你会认为68是端口 但是 当我执行 Request Host 时 我得到201 121 152 168当我执行 Request
  • 将密码存储到sql中的最佳方法

    在我当前的 C Windows 应用程序中 密码已以纯文本形式存储 这显然不好 所以我只想知道加密密码并存储到 SQL Server 中的最佳方法是什么 我读到使用哈希 盐更好 但我觉得sql 2005中的 EncryptByPassPhr
  • Microsoft ASP.NET Web Pages 2 Data Nuget 包的用途是什么?

    据我了解 ASP NET MVC 4 项目所需的最低 Nuget 包是 微软 ASP NET MVC 4 微软 ASP NET 剃刀 2 微软 ASP NET 网页 2 微软网络基础设施 不过我很想知道 以下包会添加到项目中什么 Micro
  • 私有方法和属性的 JetBrains Rider C# 命名风格

    我想将私有方法的首字母设为小写 将公共方法的首字母设为大写 然而 在 Rider 中 C 命名风格下似乎只有一个选项可以应用所有方法 属性和事件 告诉 Rider 仅对私人使用不同约定的最佳方式是什么 也可以看看 私有方法和属性的 ReSh
  • 结构体前向声明编译失败

    我有以下代码 但编译器说 sender wrapper 未定义 即使我向前声明了它 我不能对结构进行前向声明吗 用VS2003编译 struct send wrapper struct IPSend IPSend IPSend const
  • 在函数内部使用时,c 数组大小会发生变化

    我有这段代码 include
  • ThemeInfo 属性有什么用?

    每当我创建新的 WPF 应用程序或 WPF 用户控件库时 AssemblyInfo cs文件包含以下属性 assembly ThemeInfo ResourceDictionaryLocation None where theme spec
  • 如何按分层类别结构中的值对 pandas 中的数据框进行排序

    我有一个 pandas 数据框 pd DataFrame category Transport Transport Car Transport Train Household Household Utilities Household Ut
  • 函数的动态返回类型

    如何创建一个具有基于参数类型的动态返回类型的函数 Like protected DynamicType Test DynamicType type return 为此 您必须使用泛型 例如 protected T Test
  • 使用资源文件进行本地化不起作用

    我添加了新的 Rosource 文件 UserNotification resx 然后我添加了两个文件进行本地化 并将其命名为 UserNotification hr HR resx 和 UserNotification sl SI res
  • 执行按钮单击时使 wpf UI 响应

    在我的 wpf c 应用程序中 当用户按下按钮时会执行一个很长的过程 当按下按钮直到执行完整的代码时 窗口将冻结 用户无法在窗口中执行任何其他任务 如何使按钮单击代码作为后台进程 以便窗口响应用户 我尝试过以下方法 但没有成功 privat
  • 如何使用 html 敏捷包获取自定义标签?

    需要创建摘要 索引 为此我有标签
  • C 中的静态和外部内联函数[重复]

    这个问题在这里已经有答案了 我正在尝试详细了解静态函数和外部函数之间的区别 我知道静态内联函数和外部内联函数之间的基本区别 我的理解如有错误请指正 静态内联函数仅对定义它的翻译单元可见 外部内联函数可以在多个翻译单元中访问 最好在头文件中定
  • exit() 和 abort() 有什么区别?

    在C和C 中 有什么区别exit and abort 我试图在发生错误 不是例外 后结束我的程序 abort http en cppreference com w c program abort退出程序而不调用使用注册的函数atexit h
  • .NET Web API - 添加日志记录

    我正在寻找有关处理 API 日志记录的最佳方法的帮助 我想将所有请求和响应记录到 sql 或文本文件 如果这是最好的方法 目前我已经在 SQL Server 的日志表中插入一行 我使用名为 LogAction 的静态方法来执行此操作 并在

随机推荐