| | |
About matris allocation and reading pgm files
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2009
Posts: 1
Reputation:
Solved Threads: 0
// 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?
//
#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?
![]() |
Similar Threads
- Reading Binary Files in VB (Visual Basic 4 / 5 / 6)
- reading from text files and manipulating the data....how (Visual Basic 4 / 5 / 6)
- Reading CSV files (Python)
- Reading and writing from files works selectively... (C++)
- Problems reading from files (C)
- problems with reading video files (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: 10 question math quiz
- Next Thread: what is the path to move to every point of a matrix of 5 column and 5 row once?????
Views: 146 | Replies: 0
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





