我试着像这样解决它:
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef struct
{
int r;
int c;
char *word;
} triplet;
int main()
{
triplet *a,aux;
char **b;
int j,i,n;
FILE *f,*g,*p;
f=fopen("data.txt","r");//Se deschide fisierul text
g=fopen("outfile.txt","w");
a=calloc(10,sizeof(triplet));//Se aloca dinamic spatiu pentru structura
i=0;
while (!(feof(f)))
{
a[i].word=malloc(60);
fscanf(f,"[ %d %d %s ]\n",&(a[i].r),&(a[i].c),a[i].word);
i++;
}
n=i;
printf("%d \n",n);
b = malloc(n * sizeof(char *));
for (i = 0; i < n; i++)
b[i] = calloc(n, sizeof(char));
for(i=0; i<n; i++)
{
printf("%d %d %s\n",a[i].r,a[i].c,a[i].word);
}
for(i=0; i<n; i++)
{
b[i]=realloc(b,a[i].c+strlen(a[i].word)*sizeof(char));
for(j=0; j<n; j++)
{
b[i][j]=' ';
for(i=0; i<n; i++)
{
for(j=0; j<strlen(a[i].word); j++)
// if ( b[a[i].r,a[i].c+j]==' ')
b[a[i].r,a[i].c+j]=a[i].word[j];
//else printf("collide at... \n");
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
printf("%c ",b[i][j]);
}
}
}
<!-- end snippet -->
#include <stdlib.h>
#include<string.h>
typedef struct
{
int r;
int c;
char *word;
} triplet;
int main()
{
triplet *a,aux;
char **b;
int j,i,n;
FILE *f,*g,*p;
f=fopen("data.txt","r");
g=fopen("outfile.txt","w");
a=calloc(10,sizeof(triplet));
i=0;
while (!(feof(f)))
{
a[i].word=malloc(60);
fscanf(f,"[ %d %d %s ]\n",&(a[i].r),&(a[i].c),a[i].word);
i++;
}
n=i;
printf("%d \n",n);
b = malloc(n * sizeof(char *));
for (i = 0; i < n; i++)
b[i] = calloc(n, sizeof(char));
for(i=0; i<n; i++)
{
printf("%d %d %s\n",a[i].r,a[i].c,a[i].word);
}
for(i=0; i<n; i++)
{
b[i]=realloc(b,a[i].c+strlen(a[i].word)*sizeof(char));
for(j=0; j<n; j++)
{
b[i][j]=' ';
for(i=0; i<n; i++)
{
for(j=0; j<strlen(a[i].word); j++)
// if ( b[a[i].r,a[i].c+j]==' ')
b[a[i].r,a[i].c+j]=a[i].word[j];
//else printf("collide at... \n");
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
printf("%c ",b[i][j]);
This is what I needed to do:
}
}
}
当我尝试实现它时,它只会读取结构,如果我不知道行数,我怎么能为矩阵动态分配空间。我做错了什么?任何帮助都会非常感谢你。我怎么能处理碰撞问题?我想过使用strlen但不确定它会起作用。
答案 0 :(得分:0)
以下提议的代码:
现在建议的代码:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct sTriplet
{
int r;
int c;
char word[40];
};
typedef struct sTriplet triplet;
int main( void )
{
char line[62];
triplet *sourceArray = NULL;
FILE *fin = fopen( "data.txt", "r" );
if( !fin )
{
perror( "fopen to read data.txt failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
size_t lineCount = 0;
while( fgets( line, sizeof line, fin ) )
{
lineCount++;
triplet *temp = realloc( sourceArray, lineCount*sizeof( triplet ) );
if( !temp )
{
perror( "realloc failed" );
free( sourceArray );
exit( EXIT_FAILURE );
}
// implied else, realloc successful
sourceArray = temp;
if( 3 != sscanf( line, "[ %d %d %s ]",
&(sourceArray[lineCount].r),
&(sourceArray[lineCount].c),
sourceArray[lineCount].word) )
{
fprintf( stderr, "parsing of line from input file failed" );
free( sourceArray );
exit( EXIT_FAILURE );
}
}
char collisionChecker[ lineCount ][ MAX_LINE_LEN };
memset( collisionChecker, ' ', MAX_LINE_LEN * lineCount );
memcpy( &collisionChecker[0], &sourceArray[0], sizeof( triplet ) );
for( size_t i = 1; i < lineCount; i++ ) // 1 because 0 already set
{
// place logic here to check for overlaps and print status
}
free( sourceArray );
// note following loop may output some extra blank lines
for( size_t i=0; i< lineCount; i++ )
{
printf( "%s\n", collisionChecker[i] );
}
}