Reputation: 11
I'm writing to a .ppm file, so far I'm just testing it by writing 0 and 1 to it. When I open the file in Notepad, the numbers show up as symbols. But when I open it up in WordPad or Microsoft Word the numbers appear. Surely there's nothing wrong with the code and it's Notepad's fault? I've tried to find out via Google but I can't find anything. Essentially, what I'm doing is expanding a file that contains values like, (1 1 1 1) to (1 0 0 1 0 0 1 0 0 1 0 0) which are the red pixels and then adding green and blue values in the same manner.
I get ‱‰‰‱‰‰‱‰‰‱‰‰, instead of 100100100100.
The code is:
#include <stdio.h>
int redArray[128][256 * 3];
int main(void) {
int x;
int y;
FILE *redFile = NULL;
imagePixels = fopen("image.ppm", "w");
redFile = fopen("image.red", "r");
readRed(redFile);
for (y = 0; y < 128; y++) {
for (x = 0; x < 256 * 3; x += 3) {
redArray[y][x] = 1;
}
}
for (y = 0; y < 1; y++) {
for (x = 0; x < 256 * 3; x++) {
fprintf(imagePixels, "%d ", redArray[y][x]);
}
}
fclose(redFile);
fclose(imagePixels);
return 0;
}
// This function is in a different .c file. I completely forgot to add it here but I'll leave at the '#include' business.
void readRed(FILE *colourFile) {
for (y = 0; y < 128; y++) {
for (x = 0; x < 256; x++) {
fscanf(redFile, "%d", &redArray[y][x]);
}
}
}
Upvotes: 0
Views: 1819
Reputation: 7603
Problem is related to Notepad handling of your file. Notepad look for the first 512 bytes to determine what is the encoding of a file. When no BOM is specified, it tries to guess. Your file is more than likely treated as Unicode. It is on my machine(Unicode(UTF16 LE)), look in File->Encoding->More). Here's why you get those characters:
Code point for ‰ is U2030. You are(repeatedly) writing 1 0 0
which in bytes, encoded in Ascii and represented in hexadecimal, translate to
3120302030
You can see why you get ‰ printed 2 times every 3 characters. For the 1st one, I just think that Notepad is thrown off and display the unprintable character.
While testing on my machine, it appears that if I introduce a \n
after at most 512 characters in the first line(this is important as the second line could go well over 6000 chars) I could load the file in Notepad but not after more than that.
Upvotes: 2
Reputation: 2214
Since you're getting garbage characters, not digits (and you used fprintf with %d format) my guess is, your program wrote the data out in one encoding (probably UTF-8) and Notepad interpreted it as something different (probably just ASCII).
You might want to poke around the development environment you built the program in to see what character encoding it's outputting by default.
There's a big treatise on character encodings at http://kunststube.net/encoding/
and a StackOverflow article on how Notepad interprets the encoding of characters at
How windows notepad interpret characters
with some helpful links in the very last post.
Since you say you're just testing the ability to write out to the file, the inclusion of the input file code may mislead some readers.
Upvotes: 0
Reputation: 1854
You are declaring
int redArray[128][256 * 3];
and assigning it a handle
redArray = fopen("image.red", "r");
I think you mean:
redFile = fopen("image.red", "r");
Upvotes: 0
Reputation: 4685
You need to open the file and read the data out before manipulating it. Right now you're opening the FILE * redArray
and then directly reading from it like it was an array. It's a file handle.
You'll have to read the data into an array first like: (swiped from here)
int fileSize;
int * contents;
//Seek to the end of the file to determine the file size
fseek(redArray, 0L, SEEK_END);
fileSize = ftell(redArray);
fseek(redArray, 0L, SEEK_SET);
//Allocate enough memory (add 1 for the \0, since fread won't add it)
contents = malloc(fileSize+1);
//Read the file
size_t size = fread(contents,1,fileSize,redArray);
//Close the file
fclose(redArray);
Upvotes: 1