gpt4 book ai didi

c - 给定管道的确切位置,获取由管道分隔的子字符串

转载 作者:行者123 更新时间:2023-11-30 15:16:23 26 4
gpt4 key购买 nike

我进行了搜索,但没有得到一个有效的答案。尽管我知道仍然可能有很多答案。老实说,我找不到它,因为我是 C/C++ 初学者。

我的问题是我有一个文本文件,其中包含由管道('|')分隔的数据。实际上是一个日志文件。在每个条目中,内容由管道('|')分隔,每个条目由换行符('\n')分隔,它真的很长。所以我想做的是,当用户给出序列 sequence=[2,5,7] 时,该函数应该能够读取该数组并仅给出以该管道位置开头的内容。所以这里应该将第二、第五和第七个内容放入文本文件中。下面是我使用的代码。由于某种我找不到的原因它不起作用。它给出的结果文本文件仅打印出“\n”,仅此而已。它也不仅仅是文件中的条目。

minorSeparatorChar 是作为“|”给出的字符

majorSeparatorChar 是以 '\n' 给出的字符

inFile 输入文本文件

outFile输出文本文件

minSepCount 次要分隔符计数

majSepCount 主要分隔符计数

sequence是一个全局const int数组

void getFormattedOutput(char * inFile, char * outFile, char minorSeparatorChar,char majorSeparatorChar){

FILE *readFile,*writeFile;
int charactor=0, minSepCount=0, i=0,majSepCount = 0;
int flagMin = 0;
char charactorBefore = NULL;
readFile = fopen(inFile,"r"); // opens the file for reading
writeFile = fopen(outFile,"w"); // opens the file for writing

if (readFile==NULL || writeFile == NULL){
printf("\nFile creation is not a sucess, Exiting program..\n");
exit(0);
}


while(charactor!=EOF){

charactorBefore = charactor;
if (charactor==minorSeparatorChar)
flagMin=1;
charactor = fgetc(readFile);
if(charactorBefore == minorSeparatorChar){
flagMin = 0;
if (minSepCount==sequence[i]){
fputc(charactor,writeFile);

continue;
}
i++;

minSepCount++;
}

else if (charactorBefore == majorSeparatorChar){
minSepCount=0;
i=0;
majSepCount++;
fputc('\n',writeFile);
}

else{
if(flagMin==1)
fputc(charactor,writeFile);
continue;
}

}

fclose(readFile);
fclose(writeFile);

}

例如,如果输入文件有

33|333|67|787|7889|9876554|56
20151001|0|0|0|0||94|71
1|94|71|1|94|71|1

如果我给出序列[2,5,6]

它应该打印到文件中

67  9876554 56
0 94 71
71 71 1

最佳答案

我最终得出的结论是,您的代码中存在太多标志、控件和变量,而且我无法完全了解它们的用途,因此决定重写代码。例如,我在您的代码中看不到您如何知道序列中有多少个字段。

我用 C11 (C99) 编写,但在这个程序中,这仅仅意味着我在需要时声明变量,而不是在函数顶部声明变量。如果存在问题 (C89/C90),请将声明移至函数顶部。

我还发现使用的名称太长,以至于掩盖了变量的用途。你可能认为我在另一个方向上走得太远了;更重要的是,你的教授(老师)可能会这么认为。就这样吧;名称是可替换的,并且全局搜索和替换效果很好。

我也不明白你的代码应该如何在字段之间插入半任意数量的空格,所以我实际上回避了这个问题。此代码在适当的点输出字段分隔符( minor_sep — 长度减少 minorSeparatorChar )和记录分隔符( major_sep — 从 majorSeparatorChar 减少)。

我注意到代码中的字段编号以字段 0 开头。我不相信你的代码会从字段 0 输出数据,但考虑到重写,这有点离题。

我最终得到:

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

static const int sequence[] = { 2, 5, 7 };
static const int seqlen = 3;

static
void getFormattedOutput(char *inFile, char *outFile, char minor_sep, char major_sep)
{
FILE *ifp = fopen(inFile, "r"); // opens the file for reading
FILE *ofp = fopen(outFile, "w"); // opens the file for writing

if (ifp == NULL || ofp == NULL)
{
printf("\nFile creation is not a success, Exiting program..\n");
exit(0);
}

int c;
int seqnum = 0;
int fieldnum = 0;
while ((c = getc(ifp)) != EOF)
{
if (c == major_sep)
{
putc(major_sep, ofp);
fieldnum = 0;
seqnum = 0;
}
else if (c == minor_sep)
{
if (seqnum < seqlen && fieldnum == sequence[seqnum])
{
putc(minor_sep, ofp);
seqnum++;
}
fieldnum++;
}
else if (fieldnum == sequence[seqnum])
fputc(c, ofp);
}

fclose(ifp);
fclose(ofp);
}

int main(void)
{
getFormattedOutput("/dev/stdin", "/dev/stdout", '|', '\n');
return 0;
}

当我运行它时(我称之为 split ,尽管这不是一个好的选择,因为还有一个标准命令 split ),我得到:

$ echo "fld0|fld1|fld2|fld3|fld4|fld5|fld6|fld7|fld8|fld9" | ./split 
fld2|fld5|fld7|
$ echo "fld0|fld1|fld2|fld3|fld4|fld5|fld6" | ./split
fld2|fld5|
$

唯一可能的反对意见是存在字段终止符而不是字段分隔符。正如您所看到的,终结者并不难实现;将其设置为分隔符(因此,即使该行的字段数量没有序列中的元素那么多,在该行的最后一个字段之后也没有管道 - 请参阅第二个示例输出)是比较棘手的。当代码读取应在第一个此类字段之后打印的字段的第一个字符时,代码需要输出分隔符。这段代码实现了:

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

static const int sequence[] = { 2, 5, 7 };
static const int seqlen = 3;

static
void getFormattedOutput(char *inFile, char *outFile, char minor_sep, char major_sep)
{
FILE *ifp = fopen(inFile, "r"); // opens the file for reading
FILE *ofp = fopen(outFile, "w"); // opens the file for writing

if (ifp == NULL || ofp == NULL)
{
printf("\nFile creation is not a success, Exiting program..\n");
exit(0);
}

int c;
int seqnum = 0;
int fieldnum = 0;
int sep = 0;
while ((c = getc(ifp)) != EOF)
{
if (c == major_sep)
{
putc(major_sep, ofp);
fieldnum = 0;
seqnum = 0;
sep = 0;
}
else if (c == minor_sep)
{
if (seqnum < seqlen && fieldnum == sequence[seqnum])
seqnum++;
fieldnum++;
sep = minor_sep;
}
else if (fieldnum == sequence[seqnum])
{
if (sep != 0)
{
putc(sep, ofp);
sep = 0;
}
putc(c, ofp);
}
}

fclose(ifp);
fclose(ofp);
}

int main(void)
{
getFormattedOutput("/dev/stdin", "/dev/stdout", '|', '\n');
return 0;
}

运行示例:

$ {
> echo "Afld0|Afld1|Afld2|Afld3|Afld4|Afld5|Afld6|Afld7|Afld8|Afld9"
> echo "Bfld0|Bfld1|Bfld2|Bfld3|Bfld4|Bfld5|Bfld6|Bfld7|Bfld8|Bfld9"
> echo "Cfld0|Cfld1|Cfld2|Cfld3|Cfld4|Cfld5|Cfld6|Cfld7|Cfld8|Cfld9"
> echo "Dfld0|Dfld1|Dfld2|Dfld3|Dfld4|Dfld5|Dfld6|Dfld7|Dfld8|Dfld9"
> echo "Efld0|Efld1|Efld2|Efld3|Efld4|Efld5|Efld6|Efld7|Efld8|Efld9"
> } | ./split
|Afld2|Afld5|Afld7
|Bfld2|Bfld5|Bfld7
|Cfld2|Cfld5|Cfld7
|Dfld2|Dfld5|Dfld7
|Efld2|Efld5|Efld7
$

关于c - 给定管道的确切位置,获取由管道分隔的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33116352/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com