关于如何使用C以特定方式反转文本文件顺序的建议

关于如何使用C以特定方式反转文本文件顺序的建议,c,C,我有一个有趣的假设问题,我想知道如何解决。理想情况下,我现在希望使用C完成此操作。因此,问题是: 如果文本文件中包含以下内容: 1.50–1.62(2H,重叠m)、1.77–1.89(2H,重叠m)、1.97–2.07(2H,重叠m)、2.10–2.18(9H,重叠m) 如何将其改为: 2.10-2.18(9小时,重叠米)、1.97-2.07(2小时,重叠米)、1.77-1.89(2小时,重叠米)、1.50-1.62(2小时,重叠米) 我是否必须将每个段存储在链接列表中的字符数组中,然后以这种方

我有一个有趣的假设问题,我想知道如何解决。理想情况下,我现在希望使用C完成此操作。因此,问题是:

如果文本文件中包含以下内容:

1.50–1.62(2H,重叠m)、1.77–1.89(2H,重叠m)、1.97–2.07(2H,重叠m)、2.10–2.18(9H,重叠m)

如何将其改为:

2.10-2.18(9小时,重叠米)、1.97-2.07(2小时,重叠米)、1.77-1.89(2小时,重叠米)、1.50-1.62(2小时,重叠米)

我是否必须将每个段存储在链接列表中的字符数组中,然后以这种方式颠倒顺序?解释每一段的不同长度可能很棘手


谢谢

您可以使用文件处理/操作,特别是以下功能:

fseek()
fseek()
函数用于将流的文件位置指示器设置为新位置。此函数接受三个参数。第一个参数是
fopen()
函数返回的文件流指针。第二个参数“offset”表示要查找的字节数。第三个参数“whence”说明从何处查找字节数的“offset”。可用的值有
SEEK\u SET
SEEK\u CUR
SEEK\u END
。这三个值(按顺序)描述文件的开始、当前位置和文件的结束。成功后,此函数返回
0
,否则返回
-1

如何颠倒文本文件内容的顺序


因此,您可以开始向后读取文件,而不是保存所有内容,然后颠倒顺序。

我理解您的问题,您希望按相反顺序对一些数据组进行排序。然后将结果数据组输出到新文件

我的建议是:

1) read the input file, a line at a time, perhaps with fgets() or getline()
2) generate a linked list where each node in the list contains one data group.
3) always insert at the 'head' of the linked list 
4) To output, start at the 'head' and walk through the list, outputting the contents of each node.
5) you could use strstr() to find each occurrence of the end of a data group, then use memcpy to place a copy of the data into the new node to be inserted into the front of the linked list.

将整个文件读取到
char*
缓冲区。然后前后遍历它,查找
),
片段。找到后--将上一个
),
中的所有文本打印到结果文件中。我会用这个想法开始编码,也许在这个过程的中间你可以看到一些微妙的、优雅的方式来做:尝试先编码一些东西,否则你在错误的网站上…除此之外,我不明白你想做什么,除了举个例子,你应该解释:o[这看起来像是一个排序问题,但标准是神秘的]缺少代码说明你没有试图自己解决这个问题。我们不是来设计您的算法,也不是来为您提供代码的。我们在这里解决“它不编译”和“它产生错误的输出或seg错误”这类问题,即编译时间和运行时问题。这种方法将在硬盘上的文件上进行大量查找。与所有其他硬盘操作相比,查找速度非常慢,并且比从文件中一次读取一行并应用某种算法反转数据慢几个数量级groups@user3629249他的数据是结构化的,可以按一个元素的大小递增(
1.50–1.62(2H,重叠m
)。我非常怀疑递增比读取、存储和反转元素算法实现慢。但是,这只是一种可能的解决方案,优化和复杂性是不同的主题。建议使用“,”作为传递给strstr()的搜索字符串
1) read the input file, a line at a time, perhaps with fgets() or getline()
2) generate a linked list where each node in the list contains one data group.
3) always insert at the 'head' of the linked list 
4) To output, start at the 'head' and walk through the list, outputting the contents of each node.
5) you could use strstr() to find each occurrence of the end of a data group, then use memcpy to place a copy of the data into the new node to be inserted into the front of the linked list.