Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi friends
when i read only 64 bytes from image file i see that the program write strange byte :
P5
# Created by Imlib
512 512
255
¢¢¢¡¢£¡¦¢¢ ›£ ›œ¡¡šœš™š˜œšÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ..
why the program write the """Í""" bytess????

What I have tried:

C#
//while (!feof(file))  // repeat until reached end of file

{	
	char *filename="temp_file()";

	pFile=fopen(filename,"wb");

    int index;	// index into the buffer

    numread = fread(buffer, sizeof(unsigned char), 64, fpIn);

    if (numread == 0)

        break;  // no data left to read

    for (index = 0; index < numread; ++index)

    {
        // process each byte of the buffer

        char b = buffer[index];	// b contains the value of the current byte

        //printf("byte %d: %c\", index, b);

		fprintf(pFile,"%c",b);
		
    }
Posted
Updated 26-Oct-16 17:39pm

Simple: it's binary data, so it doesn't contain human readable information.
Some of the binary data may be human readable - like the "Created by ..." part in your example - but the rest isn't, so so when you try to display it it tries it's best to "translate" the bytes to "readable characters". They aren't usable as data however!
 
Share this answer
 
Comments
yagami_md 25-Oct-16 8:00am    
what i must to do for not write the "ÍÍÍÍÍÍÍ.." bytes????
Leo Chapiro 25-Oct-16 8:02am    
You need to read a text file instead of a picture!
yagami_md 25-Oct-16 8:04am    
i think "fprintf" do that !!! write 64 bytes to the file "temp_file()"
Leo Chapiro 25-Oct-16 8:09am    
Didn't you said "when i read ... from image file"? What kind of file is that?
yagami_md 25-Oct-16 8:10am    
yes image.pgm
Take a look at this solution in C++, looks like the same you want to do. You can adapt it to C-Language:

parsing - How to read in data from a pgm file in C++ - Stack Overflow[^]

C#
#include <iostream> // cout, cerr
#include <fstream> // ifstream
#include <sstream> // stringstream
using namespace std;

int main() {
  int row = 0, col = 0, numrows = 0, numcols = 0;
  ifstream infile("file.pgm");
  stringstream ss;
  string inputLine = "";

  // First line : version
  getline(infile,inputLine);
  if(inputLine.compare("P2") != 0) cerr << "Version error" << endl;
  else cout << "Version : " << inputLine << endl;

  // Second line : comment
  getline(infile,inputLine);
  cout << "Comment : " << inputLine << endl;

  // Continue with a stringstream
  ss << infile.rdbuf();
  // Third line : size
  ss >> numcols >> numrows;
  cout << numcols << " columns and " << numrows << " rows" << endl;

  int array[numrows][numcols];

  // Following lines : data
  for(row = 0; row < numrows; ++row)
    for (col = 0; col < numcols; ++col) ss >> array[row][col];

  // Now print the array to see the result
  for(row = 0; row < numrows; ++row) {
    for(col = 0; col < numcols; ++col) {
      cout << array[row][col] << " ";
    }
    cout << endl;
  }
  infile.close();
}
 
Share this answer
 
v2
One reason could be that the characters are in the image file itself :)

ÍÍÍ characters are assigned to uninitialized variables in debug build.
How have you defined buffer? If you defined buffer as an int (or short), you will get these characters in debug build because:
i) you are reading an array of bytes in buffer.
ii) you are writing every 4th byte (from buffer) in file.

It should work if you change buffer to char.
 
Share this answer
 
v2
Comments
yagami_md 27-Oct-16 5:25am    
sorry brother i changed to char but not work !!! what i will should do
manoranjan 31-Oct-16 4:48am    
Please share the updated code and the new output.
yagami_md 31-Oct-16 10:30am    
It's error of the position of the fclose file

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900