Hi all. I'm new to c++, and am trying to write a stream of interleaved 3 byte (1 byte per color RGB), pixel data to a binary file. The fwrite fails when 3 lines of commented code below are enabled. I don't get an error, the program appears to run correctly, but no file is created. Any thoughts? I thought it might be the nested loop, so I tried 'for' loops instead, but got the same problem. Please don't pick on me too hard if my programming is lousy, I'm really new to all of this.
Cindy
#include "stdafx.h"
#include <stdio.h>
#include "iostream.h"
int main(int argc, char* argv[])
{
int red[256]; //Red Level
int green[256]; //Green Level
int blue[256];// Blue Level
char buffer [768]; //stream to write to file
int a=0; //array positioning device for interleaving
int g=0;
int x=0;
while (x<256)
{
int y=0;
while (y<256)
{
float interval= (255-x)/255;
if(y>0) {g=g+interval;} else {g=x;};
red[y]=255;
green[y]=g;
blue[y]=y;
int lBR=a;int lBG=a+1;int lBB=a+2;
/* The following 3 lines of code causes no file to be created by the fwrite function called below. If I reference constant, numeric values for the array element (instead of lBR etc., then it works and a file is generated by fwrite. */
buffer[lBR]=red[y];
buffer[lBG]=green[y];
buffer[lBB]=blue[y];
a=a+3;
y++;
}
x++;
}
FILE *fp;
fp = fopen("c:\\test.raw", "wb");
if(fp != NULL)
{
fwrite( buffer, 1, 768, fp);
fclose(fp);
}
return (0);
}
Cindy
#include "stdafx.h"
#include <stdio.h>
#include "iostream.h"
int main(int argc, char* argv[])
{
int red[256]; //Red Level
int green[256]; //Green Level
int blue[256];// Blue Level
char buffer [768]; //stream to write to file
int a=0; //array positioning device for interleaving
int g=0;
int x=0;
while (x<256)
{
int y=0;
while (y<256)
{
float interval= (255-x)/255;
if(y>0) {g=g+interval;} else {g=x;};
red[y]=255;
green[y]=g;
blue[y]=y;
int lBR=a;int lBG=a+1;int lBB=a+2;
/* The following 3 lines of code causes no file to be created by the fwrite function called below. If I reference constant, numeric values for the array element (instead of lBR etc., then it works and a file is generated by fwrite. */
buffer[lBR]=red[y];
buffer[lBG]=green[y];
buffer[lBB]=blue[y];
a=a+3;
y++;
}
x++;
}
FILE *fp;
fp = fopen("c:\\test.raw", "wb");
if(fp != NULL)
{
fwrite( buffer, 1, 768, fp);
fclose(fp);
}
return (0);
}