About matris allocation and reading pgm files

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2009
Posts: 1
Reputation: firtina is an unknown quantity at this point 
Solved Threads: 0
firtina firtina is offline Offline
Newbie Poster

About matris allocation and reading pgm files

 
0
  #1
Nov 5th, 2009
// Tuba.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>



unsigned char** pgmRead(char *fileName)
{
unsigned char **pixel;
int versiyon;
int yukseklik;
int genislik;
int griSeviyesi;
char yorum[256];

int head[3];
char c1,c2;
char ch;
unsigned char *block;

int i,j;

FILE *inf;
inf = fopen(fileName, "rb");
if (inf == NULL)
{
printf("Dosya yok!n");
exit(1);
}
else
{
fscanf(inf, "%c%c", &c1, &c2);
printf("%c %c",c1,c2);
if (c1 != 'P' || (c2 != '2' && c2 != '5'))
{
printf("PGM formatı desteklemeyen dosya!n");
fclose(inf);
exit(1);
}
}

for (j=0; j<3; j++) {
do {
i=0;
ch = fgetc(inf);
if (ch == '#')
do {
yorum[i++]=fgetc(inf);
} while (yorum[i-1]!= 'n');
yorum[i]=0;
// printf("COMMENT: %s",str); //*** if wished comments can be printed out here
} while(ch < '0' || ch > '9');

i = 0;
yorum[i++] = ch;
do {
ch = fgetc(inf);
yorum[i++] = ch;
} while(ch >= '0' && ch <= '9');
yorum[i-1] = 0;
head[j] = atoi(yorum);
i=0;
}

versiyon= c2-'0';
genislik= head[0];
yukseklik=head[1];
griSeviyesi=head[2];


//____________hata bunun altında bi yerlerde
pixel=(unsigned char **)calloc(genislik,sizeof(unsigned char));
for(i=0;i<genislik;i++)
{
pixel[i]=(unsigned char*)calloc(yukseklik,sizeof(unsigned char));
}
block=(unsigned char*)calloc(genislik,sizeof(unsigned char));

for(i=0;i<genislik;i++){
for(j=0;yukseklik;j++){
fread((void *)block,sizeof(unsigned char),genislik*yukseklik,inf);
pixel[i][j]=(unsigned char)block[(j*genislik)+i];
}
}
free(block);
fclose(inf);

return (pixel);
}


//int _tmain(int argc, _TCHAR* argv[])
int main()
{
char dosyaIsmi[15];
printf("Cevirilecek dosyayı giriniz.\n");
scanf("%s",&dosyaIsmi);

pgmRead(dosyaIsmi);

return 0;
}





This code is fail cause of memory allocaiton;but I don't understand why.

How can I correct it?
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 147 | Replies: 0
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC