您好我有一点问题,但我找不到解决方案了。我有一个文件(data.txt),其中包含下一个信息:
5,5,6,2,5,2
1,4,2,3,7,2
4,2,5,2,3,4
5,5,6,2,5,2
4,5,2,6,2,4
2,1,5,6,3,2
我想将所有重复的行设置为0,例如:5,5,6,2,5,2出现两次,因此最终文件必须如下所示:
0,0,0,0,0,0
1,4,2,3,7,2
4,2,5,2,3,4
0,0,0,0,0,0
4,5,2,6,2,4
2,1,5,6,3,2
我尝试过使用辅助文件,但无法获得解决方案,我会在任何帮助下做到这一点。
我可以将这两行中的任何一行设置为0但不能同时设置为两行,如果同一行显示的次数超过两次,则相同。
答案 0 :(得分:1)
您可以通过以下方式实现这一目标:
"r+"
模式下打开输入/输出文件要正确检测配对,您可以遵循以下逻辑:
1
0
并继续将您的线与其余部分进行比较1
的变量的状态修改或不修改最初选择的行这是一个有效的代码,除非你没有足够的内存,否则应该没有缺陷:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
void settozero( char * line ) {
for ( int i = 0; line[i] != '\0'; i++ )
if ( line[i] != ',' )
line[i] = '0';
}
int main( ) {
char ** lines = NULL;
FILE * input;
if ( ( input = fopen( "input", "r+" ) ) == NULL ) {
printf( "error at line %d", __LINE__ );
return -1;
}
int currentchar;
int newline = 1;
int linecount = 0;
int lineindex;
int linesize;
while ( ( currentchar = fgetc( input ) ) != EOF ) {
if ( newline ) {
linecount++;
lineindex = 0;
linesize = 128;
lines = realloc( lines, linecount * sizeof * lines );
lines[linecount - 1] = malloc( linesize );
newline = 0;
}
if ( lineindex == linesize ) {
linesize *= 3;
lines[linecount - 1] = realloc( lines[linecount - 1], linesize );
}
if ( currentchar == '\n' ) {
newline = 1;
currentchar = '\0';
}
lines[linecount - 1][lineindex++] = currentchar;
}
if ( !newline ) {
if ( lineindex == linesize )
lines[linecount - 1] = realloc( lines[linecount - 1], linesize + 1 );
lines[linecount - 1][lineindex] = '\0';
}
int * linestoskip = calloc( linecount, sizeof * linestoskip );
for ( int i = 0; i < linecount; i++ ) {
if ( linestoskip[i] )
continue;
int unique = 1;
for ( int j = i + 1; j < linecount; j++ ) {
if ( linestoskip[j] )
continue;
if ( strcmp( lines[i], lines[j] ) == 0 ) {
unique = 0;
settozero( lines[j] );
linestoskip[j] = 1;
}
}
if ( !unique )
settozero( lines[i] );
}
free( linestoskip );
fseek( input, 0L, SEEK_SET );
for ( int i = 0; i < linecount; i++ ) {
for ( int j = 0; lines[i][j] != '\0'; j++ ) {
fputc( lines[i][j], input );
}
if ( i != linecount - 1 || newline )
fputc( '\n', input );
free( lines[i] );
}
free( lines );
putchar( 10 );
return 0;
}