Hi all;
i have some problem with my code.. I want to read txt file which include :
rectangle 0 0 1 1 orange
rectangle 0 1 1 1 green
rectangle 0 2 1 1 white
rectangle 0 3 1 1 orange
rectangle 0 4 1 1 white
rectangle 0 5 1 1 black
rectangle 0 6 1 1 blue
rectangle 0 7 1 1 red
rectangle 0 8 1 1 blue
rectangle 0 9 1 1 white
and so on.(1000 lines or more).. Then ı want to convert this file to xpm (xpix map ) format. here is the xpm format something like that :
/* XPM */
static char * folder[] = {
"100 10 11 1",
"0 c blue" ,
"1 c white" ,
"2 c red" ,
"3 c green" ,
"4 c yellow" ,
"5 c black" ,
"55 c white",
"44 c black" ,
"14 c blue",
"10 c green",
"12 c red",
"012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443",
"012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443",
"012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443",
"012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443",
"012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443",
"012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443",
"012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443",
"012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443",
"012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443",
"012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443012345121014443",
};
AND ALSO here is my code
#include <stdio.h>
#include <stdlib.h>
int line2() {
/* this function count the number of line in the txt file*/
FILE *fp;
int c=-1;
char ch='\0';
fp=fopen("input1.txt","r");
while(ch!=EOF) {
ch=fgetc(fp);
if(ch=='\n') c++;
}
return c;
}
int main ( void )
{
char inputfile[80], outputfile[80],renk[20],isim[20];
int i=0,h,x,y,w,k,l,j,f;
/* get the filenames from the user */
printf("What file do you want to read? ");
scanf("%s", inputfile);
printf("What file name do you want for the write? ");
scanf("%s", outputfile);
/* open the files */
char *a;
a=malloc(246);
for(i=0;i<4;i++){
printf("%c\n",alp[i]);
}
FILE *file = fopen ( inputfile, "r" );
FILE *box;
box = fopen(outputfile, "w");
fgets(a,256,file);
sscanf(a,"%d%d",&w,&h);
int ta=w;
int tb=h;
fputs( "/* XPM */ \n ", box );
fputs( "static char * folder[] = {\n", box );
fputs("\"",box);
fprintf(box,"%d %d",w,h);
fprintf(box," %d ",line2());
fputs("1\",\n",box);
int image[ta][tb];
if ( file != NULL )
{
char line [ 128 ]; /* or other suitable maximum line size */
while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{
sscanf(line,"%s%d%d%d%d%s",isim,&w,&h,&x,&y,renk);
fputs("\"",box);
fprintf(box,"%d ",i);
fputs(" c ",box);
fprintf(box,"%s",renk);
fputs("\" , \n",box);
for(k=w;k<x+w;k++){
for(l=h;l<y+h;l++){
image[k][l]=i;
}
}
i++;
}
for(i=0;i<tb;i++){
fputs("\" ",box);
for(j=0;j<ta;j++){
fprintf(box,"%d",image[i][j]);
}
fputs("\" ,",box);
fputs("\n",box);
}
fputs("};",box);
fclose(file);
fclose(box);
}
else
{
perror ( inputfile); /* why didn't the file open? */
}
return 0;
}
This code is not works properly .The problems are :
1) it gives us segmentation fault ,when we give txt file length more than 1000 lines
2) when we give txt file length less than 1000 lines ,it produce picture but this picture is not proper ?
Any idea? what is the wrong part of that code...