I've been trying for ages to get this code to work but it just presents me with a blank file every time. Any help is very much appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int x, y;
} PPMImage;
typedef struct
{
int rgb_comp_colour;
char buff[16];
char filetype[2];
int height;
int width;
} PPMHead;
PPMHead head[3];
int **Array; //Double pointer defines as a pointer pointing to a pointer that is pointing to an integer
int main(void)
{
FILE *fp;
char fname[100];
printf("Enter file name: ");
scanf("%s", fname);
fseek(stdin,0,SEEK_END);
fp = fopen(fname, "r");
if (fp == NULL)
{
printf("\tError while opening the file\n");
}
else
{
printf("\tReading in %s\n", fname);
}
Array = malloc(head[0].width*sizeof(int*)); //Points to malloc
for (int i=0;i<head[0].width;i++)
{
Array[i] = (int*)malloc(head[0].height*sizeof(int));
}
for(int j=0;j<head[0].height;j++)
{
for(int i=0;i<head[0].width;i++)
{
fscanf(fp, "%3d ", &Array[i][j]); //Scans the address of Array[i][j]
}
}
fclose(fp);
//Save PPM Array Into New PPM File
FILE *pf;
int i, j;
char fname2[100];
printf("Enter file name: ");
scanf("%s", fname2);
fseek(stdin,0,SEEK_END);
pf = fopen(fname2, "w");
if (pf == NULL)
{
printf("\tError while opening the file\n");
}
else
{
printf("\tWriting in %s\n", fname2);
}
for(j=0;j<head[0].height;j++)
{
fprintf(pf, "\n");
for(i=0;i<head[0].width;i++)
{
fprintf(pf, "%3d ", Array[i][j]);
//fprintf(pf, "%3d ", (Array+j*head[0].width + i));
}
}
fclose(pf);
free(Array);
return 0;
}