Hello

I am getting the above error message with my program. The line giving the error is marked.

What I am trying to do is create a file, (new.ppm) and have it then returned onto stdout.

FILE *fp;
	fp = fopen("new.ppm", "w+"); //creates the new file
	fprintf(fp, "P3\n%d %d\n255\n", harg, warg);

	while (hcount < harg){
		while (wcount < warg){
			fprintf(fp, "%d ", cr); 
			fprintf(fp, "%d ", cb); 
			fprintf(fp, "%d ", cg); 
			wcount += 1;
		}
		fprintf(fp, "\n");
		wcount = 0;
		hcount += 1;
	}
	char buf[1024];
	int buflen;
	while((buflen = read(fp, buf, 1024)) > 0) //this line is throwing the error.
	{
		write(1, buf, buflen);
	}

	fclose(fp); //close the file

Any help you can provide is greatly appreciated,
Thanks.

I think the function you are looking for is fread (the parameters are in a slightly different order, see http://www.cplusplus.com/reference/clibrary/cstdio/fread/). AFAIK, read is an iostreams method from C++.

ptr
Pointer to a block of memory with a minimum size of (size*count) bytes.
size
Size in bytes of each element to be read.
count
Number of elements, each one with a size of size bytes.
stream
Pointer to a FILE object that specifies an input stream.

So as far as I know, ptr would be the file, (in my case fp) and stream would be stdout...

But I do not know what the size of the file will be... we are creating PPM files, and the image could be 3 pixels by 3 pixels or 100 x 1000 pixels...

So should I just make size and count 1024?

Thanks for your help!

ptr is not the file, stream is the file, ptr is whatever variable you are reading into (really, a pointer to a block of memory, which they malloced in the example at the bottom of that webpage). Size is the "sizeof" of whatever element you are reading (ints, doubles, etc).

long lSize;
	char *buffer;
	size_t result;

	// obtain file size:
  	fseek(fp , 0 , SEEK_END);
  	lSize = ftell(fp);
	rewind(fp);

	// allocate memory to contain the whole file:
	buffer = (char*) malloc (sizeof(char)*lSize);
	if (buffer == NULL)
	{//open if
		fputs ("Memory error",stderr); exit (2);
	}//close if

	// copy the file into stdout:
	result = fwrite(buffer,1,lSize,stdout); 
	if (result != lSize)
	{//open if
		fputs ("Reading error",stderr);
		exit (3);
	}//close if

That is what I have been able to piece together ... but it still does not actually write anything to stdout :(

I happened to find a good page on PPM files (http://orion.math.iastate.edu/burkardt/data/ppm/ppm.html). I think I used them a couple of times in college (in prehistory, here).

From what I remember, they are written and read as plain text files. Are you just trying to write out the numerical values of the file to the screen? If so, I would use a combination of fgets to read in the lines and sscanf to parse them into an array of the numeric values. If you mean that you want to display the actual picture, that will be much more involved.

Hopefully you've made some headway with it yourself.

This article has been dead for over six months. Start a new discussion instead.