G
Guest
Thanks for reading.
I'm taking a class where I need to read in PGM files character by character.
We're given code in C but I'd much rather use Java, so I tried rewriting it.
I'm hitting a snag though: using BufferedReader's read() method is returning
different values then what it should.
For example, where the c version...
returns "150" for some point in the file. The Java version...
returns "8211".
If I convert the input from both the C and Java versions back to chars and output
to files, the C versions matches the original image exactly (which it should), but
the Java version is screwy.
I assume it has to do with Java VM converting the character encodings of the input,
but I have no clue as to how to get the correct input.
Here a simple example code that just reads in the pgm (without the header info) and
outputs it to another file character by character...
import java.io.*;
public class copy {
public static void main (String[] args) throws IOException {
int i,j, input;
BufferedReader fin = new BufferedReader(new FileReader(args[0]));
BufferedWriter fout = new BufferedWriter(new FileWriter(args[1]));
// .pgm header info for a 256x256 image
fout.write("P5\n256 256\n255\n");
for (i=0; i<256; i++) {
for (j=0;j<256;j++) {
input = fin.read();
fout.write(input);
}
}
fin.close();
fout.close();
}
}
Thanks for any help anyone can provide.
- Drew G.
I'm taking a class where I need to read in PGM files character by character.
We're given code in C but I'd much rather use Java, so I tried rewriting it.
I'm hitting a snag though: using BufferedReader's read() method is returning
different values then what it should.
For example, where the c version...
int ci2;
FILE *fp2;
fp2=fopen("inputimage","rb");
ci2 = getc(fp2);
printf("%d", ci2);
returns "150" for some point in the file. The Java version...
int ci2;
BufferedReader ci2 = new BufferedReader(new FileReader("inputimage"));
ci2 = in2.read(t2);
System.out.print(ci2);
returns "8211".
If I convert the input from both the C and Java versions back to chars and output
to files, the C versions matches the original image exactly (which it should), but
the Java version is screwy.
I assume it has to do with Java VM converting the character encodings of the input,
but I have no clue as to how to get the correct input.
Here a simple example code that just reads in the pgm (without the header info) and
outputs it to another file character by character...
import java.io.*;
public class copy {
public static void main (String[] args) throws IOException {
int i,j, input;
BufferedReader fin = new BufferedReader(new FileReader(args[0]));
BufferedWriter fout = new BufferedWriter(new FileWriter(args[1]));
// .pgm header info for a 256x256 image
fout.write("P5\n256 256\n255\n");
for (i=0; i<256; i++) {
for (j=0;j<256;j++) {
input = fin.read();
fout.write(input);
}
}
fin.close();
fout.close();
}
}
Thanks for any help anyone can provide.
- Drew G.