在C编程中读取Hex文件? [英] Hex file reading in C programming?

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

问题描述

我有一个问题:

我正在写一个c程序,其功能非常像十六进制编辑器。我想取一个文件并查看它的十六进制值。例如,假设我有文本文件helloworld.txt,其中只有Hello World!这个词。在其中,我想指定程序采用该文件,读取其十六进制值(再次,文件是简单的.txt文件而不是二进制文件),并将其存储在数组中以备后用。



这是我到目前为止:

  #include< ; stdio.h中> 
#include< stdlib.h>

int main()
{
char ch,file_name [25];
FILE * fp;

printf(输入您希望看到的文件的名称\\\
);
获取(file_name);

fp = fopen(file_name,rb); //读取二进制模式

if(fp == NULL)//错误检查
{
perror(Error when open while file.\\\
);
exit(EXIT_FAILURE);


printf(%s文件的内容是:\ n,file_name); ((ch = fgetc(fp))!= EOF)

printf(%c,ch);

fclose(fp);
返回0;

$ / code>

正如你所看到的,这实际上只是一个简单的文件,读取和打印内容。然而,问题是,即使我处于读取二进制模式,它也不会给我文本文件的十六进制值。


如何构造这个程序,以便读取标准文本文件的十六进制值,并且(a)将它们放入数组中(我认为使用for循环应该足够简单)或者(b)将它们打印到屏幕上?

我试着用Google搜寻看看其他人是否正在尝试做同样的事情,但是人们已经从二进制文件读取或已经读取文件中有十六进制值。我有一个常规的.txt文件中的纯文本,但我想要下面的十六进制值(换句话说,文件的一种hexdump)。我希望我的问题和我想要做的事情是有道理的。如果您有任何问题,请随时通知我!在此先感谢您的帮助!我真的很感激它!

解决方案

不知道我是否理解正确的问题,但不是这种情况选项B)简单地使用

  printf(0X%02x,ch); 


I had a question:

I am writing a c program that functions much like a hex editor. I want to take in a file and view its hexadecimal values. For example, say I had the text file "helloworld.txt" that simply had the words "Hello World!" in it, I want to specify to the program to take that file, read its hexidecimal values (again the file is a simple .txt file and not a binary file) and store it in an array for later.

Here is what I have so far:

#include <stdio.h>
#include <stdlib.h>

int main()
{
   char ch, file_name[25];
   FILE *fp;

   printf("Enter the name of file you wish to see\n");
   gets(file_name);

   fp = fopen(file_name,"rb"); // read binary mode

   if( fp == NULL ) //error checking
   {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
   }

   printf("The contents of %s file are :\n", file_name);

   while( ( ch = fgetc(fp) ) != EOF )
      printf("%c",ch);

   fclose(fp);
   return 0;
}

As you can see this is really just a simple file in, read and print contents. However, the problem is that even though I'm in "read binary" mode, it won't give me the hexadecimal values of the text file.

How can I structure this program so that it reads the hexadecimal values of a standard text file and either (a) puts them in an array (which I think should be simple enough with a for loop) or (b) prints them out to the screen?

I tried googling to see if others were trying to do the same thing, but people were either already reading from a binary file or already had hexadecimal values in the file. I have plain text in a regular .txt file, but I want the hexadecimal values underneath (in other words a sort of hexdump of the file). I hope my question and what I'm trying to do makes sense. If you have any questions please feel free to let me know! Thanks in advance for all your help! I really do appreciate it!

解决方案

Not sure if I understand the question correctly, but wouldn't that be the case (for option B) of simply using

printf("0X%02x ", ch);

这篇关于在C编程中读取Hex文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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