在C中写入文件的问题 [英] Problem to writing to file in C

查看:87
本文介绍了在C中写入文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友

当我从图像文件中只读取64个字节时,我看到程序写的是奇怪的字节:

P5

#创建作者:Imlib

512 512

255

¢¢¢¢??£¢¢ >£ > ?? ¡¡¡¡?? ?? ?? ?? ?? ?? ?? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ >
我尝试过的事情:



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:

//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);
		
    }

推荐答案

简单:它是二进制数据,因此它不包含人类可读信息。

一些二进制数据可能是人类可读的 - 比如你的例子中的Created by ...部分 - 但其余的不是,所以当你试图显示它时它会尝试它是最好的将字节翻译为可读字符。但它们不能作为数据使用!
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!


在C ++中查看这个解决方案,看起来和你想做的一样。你可以将它改编成C语言:



解析 - 如何从C ++中的pgm文件读取数据 - Stack Overflow [ ^ ]



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[^]

#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();
}


一个原因可能是角色在图像文件本身中:)


在调试版本中,$ b $bÍÍÍ字符被分配给未初始化的变量。

你如何定义缓冲区?如果你将缓冲区定义为int(或short),你将在调试版本中获得这些字符,因为:

i)你正在读取缓冲区中的字节数组。

ii你正在写入文件中的每个第4个字节(来自缓冲区)。



如果你将缓冲区更改为char它应该有用。
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.


这篇关于在C中写入文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆