本文介绍了如何从文本文件中读取十六进制值数据并复制到数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 hello..i有包含十六进制值(0x00,0xAA,0x56,...等)的文本文件总共498字节数据....我已经打开并正确读取此文件但我被保存用于存储此498字节数组中的数据声明??? 文本文件数据是==> 0x04,0x2C ,0x64,0x00,0xB6,0x03,0xFA,0x84,0x87,0x6A,0x74,0x18,0x01,0x64,0x79,0x13,0x09,0x6A,0xCB,0xED,0x3D之间,为0x9c,0x79,0x88,0x49,0x4A,0x2E之间,0xE8,0x89,0x24,0xF7,0xDD,0x04,0xA9,0x09,0xE7,0xE5,0x14,0xF7,0x5B,0xC5,0x49,0x38,0x10,0xFA,0x5C,0xFA回应,0x99,0xC4,0x0D,0x74,0xE0 ,0x3A,0x45,0xE8,0x64,0xCC,为0xBB,0x72,0x57,0x31,0x63,0xF9,0x7F,0x46,0x48,0xF4,0xE8,0x91,0x5B,0xF8,0xED,0x07,0x9B,0x09,0x07,0x0D ,0x64,0xF7,0x64,0xC7,0x5A,0xB8,0x17,0xCD,0xBC,的0x6A,0xA9,0xC3,0x9D,0xEF,0x96,0x9D,0x62,0xF5,0xDD,0x83,0xD9,0x45,0x0F,0x99,0x4B ,0xF6,0x63,0x41,0x29,0x38,0xE0,0x4A,0XCD,0xF9,0x94,0xC3,0x1D,0xAE,0xB8,0x96,0x3D,0x69,0x82,0x8C,0xFF时,0x29,0xF9,0x2A,0xF6,0x85 ,0x50,0x45,0x1A,0x7E的,0xF8,0x26,0x66,0x86,0x43,0x48,0x4B,0x3E的,0xF9,0x96,0xD4,0x6A,0X 33,0xC3,0x6B,0x75,0xFD,0x29,0x95,0xAA,0x27,0xC4,0xBD,0xF7,0x0F,0x99,0x1D,将0xEB,为0x9c,0x84,0xFB,0x7B,0xF8,0x4D,0x3A,0xFA回应,0×12, 0x43,0xC9,0x43,0x0F,0x66,0x52,0xF7,0x6F,0x87,0x4A,0x36,0xE9,0xD5,0x84,0x8B,0x9A执行,0x8B,0xFF时,0xC1,0x0F,0x2D,0x25,0x8B,0x99,0xCE, 0xFE的,0xC3,0xFF,为0x0E,0xC9,0xF8,0x88,0x83,0xD9,0x83,0xD8,0x5D,0x91,0xF5,0x76,0xC7,0x3A,0xF8,0xE0,0x86,0x71,0xF7,0x7C,0x04,0x3A, 0xFA回应,0xD8,0x95,0x69,0x4B,值为0x8F,0xC5,0xFA,0xFB的才能,0xEF,0xBD,0x19,0xF6,0x05,0xC6,0xB8,0x7B,0x16,0xD5,0x71,0xF6,0x7D,0x46,0xD7,0xB7, 0xC0,0xE5,0x11,0xF7,0x75,0x04,0x4A,0x3A,0xE1,0x72,0xD9,0x4B,0x96,0x89,0xFF,0xFF时,0xDF,0x82,0xF2,0xF4,0x65,0x44,0x3B,0xB8,0xF0, 0xF9,0xF9,0x43,0x50,0xC4,0x9D,0xFE的,0x37,0x62,0xB8,0xA7,0x88,0x43,0xE9,0xC1,0xEF,0xB5,0x50,0xF7,0x87,0x85,0x17,0x40,0xE8,0x01, 0x98,0x4A,0x86,0xC7,0xF7,0xFF,为0x0F,0x31,0x38,0x4A,0x0B中,0x44,0x38,0x7E,0xF8,0x59,0xE8,0x49,0x8B,0xC3,0xD9,0xFF,0xEF,0x72,0xE0, 0x4A,0x0C,0xC7,0x29,0x3E,0xF8,0x45,0x33,0x7C,0x1B,0x02,0xD,0x3F的,0xF8,0xB1,0xF8,0x45,0x80,0x44,0x19,0xBE,0xE0,0xD6,0xC8,0x45,为0x0 4,0xC3,0xF9,0x05,0x1F,0xA1,0x6B,0x7C,0x94,0xC3,0xF7,0xFB,为0x0F,0xA3,0x2F,0x3F的,0x11,0x27,0x42,0xF7,0x44,0x1F,0x65,0x62,0x52, 0x42,0x4A,0x34,0x22,0x13,0x21,0x54,0x18,0x22,0x85,0xFF,0x43,0x32,0xB8,0xA5,0xF7,0xFF,0x12,0xF2,0x1F,0xC1,0x68,0x48,0x86,0x2F, 0x22,0xF8,0x26,0x23,0xFF,为0x0F,0xF6,0xFF,0x3F的,0x77,0x45,0x1F,0x1F的,0x17,0xF4,0x74,0x34,0x36,0x73,0x3F,0x26,0xF2,0x1F,0x6F,0x92, 0x41,0xF7,0xFF,0x77,0xA6,0xF4,0x8F,0x4F,0xF4,0xF9,0x33,0x75,0x32,0xF4,0xF1,0xF8,0xF1,0x1F,值为0x8F,0x46,0x36,0x06,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0xA2,0xE4 下面的是文件中打开和读取数据的代码==> #include < stdio.h > #include < stdlib.h > int main() { FILE * f; char c; f = fopen( 1.txt, r); while ((c = fgetc(f))!= EOF) { printf( %c,c); } fclose(f); return 0 ; }< /stdlib.h>< /stdio.h> [更新(解决方案1)] i已经尝试打开并读取文本文件...但是为了将这些数据存储到数组中我被收集了,我有这样的字符字符... #include < stdio.h > int main() { FILE * in ; char ch,cw; unsigned char str [ 508 ] = { 0 }; int j,i = 0 ; in = fopen( 1.txt , r); if ( in ) { while (!feof( in )) { ch = getc(中); str [i] = ch; i ++; printf( %c,ch); } }< /stdio.h> [更新(评论)] #include< stdio.h> #include< stdlib.h> int main() { FILE * in ; char ch,cw; unsigned char str [ 508 ] = { 0 }; int j,i = 0 ; in = fopen( 1.txt , r); if ( in ) { while (!feof( in )) { ch = getc(中); str [i] = ch; printf( %c,str [i]); i ++; } } for (j = 0 ; j< 498; j ++) { printf( %c,str [ J]); } printf( \ n); fclose( in ); return 0 ; }< /stdlib.h>< /stdio.h> 解决方案 你好,以下解决问题的每个人都是解决方案.... #include < stdio.h > int main( void ){ unsigned char tmparray [ 10 ] = {0xaa,0xbb,0xcc,0xdd,0xee,0x00,0x00,0x00,0x00,0x00}; int idx = 5 ; int 数据; FILE * fp = fopen( 1.txt, r); if (fp == NULL){ fprintf(stderr, 无法读取1.txt); return 0 ; } while (fscanf(fp, %* c%* c%x,,& data)== 1 ){ tmparray [idx ++] =( unsigned char )数据; } fclose(fp); for (idx = 0 ; idx< sizeof (tmparray)/ sizeof (tmparray [ 0 ]); idx ++){ printf( %#x,,tmparray [idx]); } printf( \ n); return 0 ; } hello..i have text file containing hex value (0x00,0xAA,0x56,...etc) total 498 bytes data....i have open and read this file properly but i am cunfused for storing this 498 bytes data in array declared???text file data is ==>0x04,0x2C,0x64,0x00,0xB6,0x03,0xFA,0x84,0x87,0x6A,0x74,0x18,0x01,0x64,0x79,0x13,0x09,0x6A,0xCB,0xED,0x3D,0x9C,0x79,0x88,0x49,0x4A,0x2E,0xE8,0x89,0x24,0xF7,0xDD,0x04,0xA9,0x09,0xE7,0xE5,0x14,0xF7,0x5B,0xC5,0x49,0x38,0x10,0xFA,0x5C,0xFA,0x99,0xC4,0x0D,0x74,0xE0,0x3A,0x45,0xE8,0x64,0xCC,0xBB,0x72,0x57,0x31,0x63,0xF9,0x7F,0x46,0x48,0xF4,0xE8,0x91,0x5B,0xF8,0xED,0x07,0x9B,0x09,0x07,0x0D,0x64,0xF7,0x64,0xC7,0x5A,0xB8,0x17,0xCD,0xBC,0x6A,0xA9,0xC3,0x9D,0xEF,0x96,0x9D,0x62,0xF5,0xDD,0x83,0xD9,0x45,0x0F,0x99,0x4B,0xF6,0x63,0x41,0x29,0x38,0xE0,0x4A,0xCD,0xF9,0x94,0xC3,0x1D,0xAE,0xB8,0x96,0x3D,0x69,0x82,0x8C,0xFF,0x29,0xF9,0x2A,0xF6,0x85,0x50,0x45,0x1A,0x7E,0xF8,0x26,0x66,0x86,0x43,0x48,0x4B,0x3E,0xF9,0x96,0xD4,0x6A,0x33,0xC3,0x6B,0x75,0xFD,0x29,0x95,0xAA,0x27,0xC4,0xBD,0xF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x22,0x85,0xFF,0x43,0x32,0xB8,0xA5,0xF7,0xFF,0x12,0xF2,0x1F,0xC1,0x68,0x48,0x86,0x2F,0x22,0xF8,0x26,0x23,0xFF,0x0F,0xF6,0xFF,0x3F,0x77,0x45,0x1F,0x1F,0x17,0xF4,0x74,0x34,0x36,0x73,0x3F,0x26,0xF2,0x1F,0x6F,0x92,0x41,0xF7,0xFF,0x77,0xA6,0xF4,0x8F,0x4F,0xF4,0xF9,0x33,0x75,0x32,0xF4,0xF1,0xF8,0xF1,0x1F,0x8F,0x46,0x36,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xA2,0xE4below is code for open and read data in file ==>#include <stdio.h>#include <stdlib.h>int main(){FILE *f;char c;f=fopen("1.txt","r");while((c=fgetc(f))!=EOF){printf("%c",c);}fclose(f);return 0;}</stdlib.h></stdio.h>[Update (was Solution 1)]i have tried for open and read the text file...but for storing this data into array i am cunfused i have charactor by charactor like this...#include<stdio.h>int main(){FILE *in;char ch,cw;unsigned char str[508]={0};int j,i=0;in=fopen("1.txt","r");if(in){ while(!feof(in)) {ch=getc(in); str[i] = ch;i++; printf("%c",ch); }}</stdio.h>[Update (was Comment)]# include <stdio.h># include <stdlib.h>int main(){FILE *in;char ch,cw;unsigned char str[508]={0};int j,i=0;in=fopen("1.txt","r");if(in){ while(!feof(in)) {ch=getc(in); str[i] = ch; printf("%c",str[i]);i++; }}for(j=0;j<498;j++){ printf("%c",str[j]);}printf("\n");fclose(in);return 0;}</stdlib.h></stdio.h> 解决方案 这篇关于如何从文本文件中读取十六进制值数据并复制到数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 20:35