怎样用C语言读取txt文件中的二进制数据并转为一维数组 5
txt中的数据为用matlab将图像转为二进制存入txt文件当中,存入的时候txt中的数据为一维,怎样可以在C当中将改txt文件进行读取,然后转为一维数组,方便进行调制解...
txt中的数据为用matlab将图像转为二进制存入txt文件当中,存入的时候txt中的数据为一维,怎样可以在C当中将改txt文件进行读取,然后转为一维数组,方便进行调制解码。求大神指导
一部分数据为:
000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000010000000010000000000000000000000000000000100000010000000100000000100000001000000010000001000000011000001000000010000000100000000100000010000000100000000110000010000000010000000010000001000000010000000100000001000000010000000100000001000000010000000100000000000000000000000010000000100000000000000000000000000000001000001000000010000000101000001100000011000000111000010000000100000001000000010000000100000001000000010000000100000001001000010010000101000001011000011000000110100001101000011010000111000001110000011000000110100001101000011100001000000010001000100100001001100010100000101100001011100010110000110000001110000011110000111110010001100101000001010100010111000110011001110100100001001001001010011100101100101100001011001110110111001110011011110011000010110001010100011101001000110010000100011111000111110001111100011101000110110001100100010111000111010001100100010111001000010001101100011111000111010010001100100101001001010010011100101011001010010010011100110001001011010010111100110001001100110011100100101001001010110010011100011111000100010000111100000110111101101111010011101110111011001110001011011100110110001100110011000110110001001011100010101110101100001010010010001000011101100110011001010100001111000011000000011110000100100000100000000000000000000000011
更多0 展开
一部分数据为:
000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000001000000010000000100000010000000010000000000000000000000000000000100000010000000100000000100000001000000010000001000000011000001000000010000000100000000100000010000000100000000110000010000000010000000010000001000000010000000100000001000000010000000100000001000000010000000100000000000000000000000010000000100000000000000000000000000000001000001000000010000000101000001100000011000000111000010000000100000001000000010000000100000001000000010000000100000001001000010010000101000001011000011000000110100001101000011010000111000001110000011000000110100001101000011100001000000010001000100100001001100010100000101100001011100010110000110000001110000011110000111110010001100101000001010100010111000110011001110100100001001001001010011100101100101100001011001110110111001110011011110011000010110001010100011101001000110010000100011111000111110001111100011101000110110001100100010111000111010001100100010111001000010001101100011111000111010010001100100101001001010010011100101011001010010010011100110001001011010010111100110001001100110011100100101001001010110010011100011111000100010000111100000110111101101111010011101110111011001110001011011100110110001100110011000110110001001011100010101110101100001010010010001000011101100110011001010100001111000011000000011110000100100000100000000000000000000000011
更多0 展开
展开全部
使用read()函数以下为百度百科的介绍
函数名:read
功 能:从文件中读
函数原型 :int read(int handle, void *buf, int nbyte);
表头文件:#include <unistd.h>
函数说明:read()会把参数handle所指的文件传送nbyte个字节到buf指针所指的内存中。若参数nbyte为0,则read()不会有作用并返回0。返回值为实际读取到的字节数,如果返回0,表示已到达文件尾或无可读取的数据。
程序例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <io.h>
#include <alloc.h>
#include <fcntl.h>
#include <process.h>
#include <sys\stat.h>
int main(void)
{
void *buf;
int handle, bytes;
buf = malloc(10);
/*
Looks for a file in the current directory named TEST.$$$ and attempts
to read 10 bytes from it. To use this example you should create the
file TEST.$$$
*/
if ((handle =
open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
{
printf("Error Opening File\n");
exit(1);
}
if ((bytes = read(handle, buf, 10)) == -1) {
printf("Read Failed.\n");
exit(1);
}
else {
printf("Read: %d bytes read.\n", bytes);
}
return 0;
}
2Linux C
编辑
定义函数
ssize_t read(int fd, void *buf, size_t count);
返回值
成功返回读取的字节数,出错返回-1并设置errno,如果在调read之前已到达文件末尾,则这次read返回0。
参数
参数count是请求读取的字节数,读上来的数据保存在缓冲区buf中,同时文件的当前读写位置向后移。注意这个读写位置和使用C标准I/O库时的读写位置有可能不同,这个读写位置是记在内核中的,而使用C标准I/O库时的读写位置是用户空间I/O缓冲区中的位置。比如用fgetc读一个字节,fgetc有可能从内核中预读1024个字节到I/O缓冲区中,再返回第一个字节,这时该文件在内核中记录的读写位置是1024,而在FILE结构体中记录的读写位置是1。注意返回值类型是ssize_t,表示有符号的size_t,这样既可以返回正的字节数、0(表示到达文件末尾)也可以返回负值-1(表示出错)。
read函数返回时,返回值说明了buf中前多少个字节是刚读上来的。有些情况下,实际读到的字节数(返回值)会小于请求读的字节数count,例如:读常规文件时,在读到count个字节之前已到达文件末尾。例如,距文件末尾还有30个字节而请求读100个字节,则read返回30,下次read将返回0。
函数名:read
功 能:从文件中读
函数原型 :int read(int handle, void *buf, int nbyte);
表头文件:#include <unistd.h>
函数说明:read()会把参数handle所指的文件传送nbyte个字节到buf指针所指的内存中。若参数nbyte为0,则read()不会有作用并返回0。返回值为实际读取到的字节数,如果返回0,表示已到达文件尾或无可读取的数据。
程序例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <io.h>
#include <alloc.h>
#include <fcntl.h>
#include <process.h>
#include <sys\stat.h>
int main(void)
{
void *buf;
int handle, bytes;
buf = malloc(10);
/*
Looks for a file in the current directory named TEST.$$$ and attempts
to read 10 bytes from it. To use this example you should create the
file TEST.$$$
*/
if ((handle =
open("TEST.$$$", O_RDONLY | O_BINARY, S_IWRITE | S_IREAD)) == -1)
{
printf("Error Opening File\n");
exit(1);
}
if ((bytes = read(handle, buf, 10)) == -1) {
printf("Read Failed.\n");
exit(1);
}
else {
printf("Read: %d bytes read.\n", bytes);
}
return 0;
}
2Linux C
编辑
定义函数
ssize_t read(int fd, void *buf, size_t count);
返回值
成功返回读取的字节数,出错返回-1并设置errno,如果在调read之前已到达文件末尾,则这次read返回0。
参数
参数count是请求读取的字节数,读上来的数据保存在缓冲区buf中,同时文件的当前读写位置向后移。注意这个读写位置和使用C标准I/O库时的读写位置有可能不同,这个读写位置是记在内核中的,而使用C标准I/O库时的读写位置是用户空间I/O缓冲区中的位置。比如用fgetc读一个字节,fgetc有可能从内核中预读1024个字节到I/O缓冲区中,再返回第一个字节,这时该文件在内核中记录的读写位置是1024,而在FILE结构体中记录的读写位置是1。注意返回值类型是ssize_t,表示有符号的size_t,这样既可以返回正的字节数、0(表示到达文件末尾)也可以返回负值-1(表示出错)。
read函数返回时,返回值说明了buf中前多少个字节是刚读上来的。有些情况下,实际读到的字节数(返回值)会小于请求读的字节数count,例如:读常规文件时,在读到count个字节之前已到达文件末尾。例如,距文件末尾还有30个字节而请求读100个字节,则read返回30,下次read将返回0。
2014-04-14
展开全部
/***我不能把整个函数给你编出来,但我这有我实现的将byte->bit , bit->byte的函数,有注解,希望能帮助你。有了这个应该就差不多了***/
字节流转为位流:
原理:工作中会遇到将字节流转为位流的情况;
字节流到位流:
unsigned char a[] = "0123456789abcdef";
unsigned char 意味着 1. 任意16进制数ru 0xab , 0x45...;
2.任意小于255的十进制整数(8位),
将字节转为位时 如‘a’,a = 97 , 则97对应十六进制数0X01100001 , 每一次将0x01100001右移一位与0x01相与得到每一位的值,存取时
用书组BIT[I*MAX(8) + MAX - 1 - J]存储;代码实现如下:
void Byte_to_bit(const unsigned *Byte , int& ByteNum , unsigned *bit , int *bitNum)
{
int i , j;
int counter = 0;
for(i = 0 ; i < ByteNum ; i++)
{
for(j = 0 ; j < MAX(8) ; j++)
{
bit[i * MAX + MAX - 1 - j] = ((Byte[i] >> j) & VALUE(0X01));//每个字节对应的位倒着存到数组,正着输出;
counter++;
}
}
*bitNum = counter;
}
位流到字节流:
原理0xff对应的十进制数值为0x11111111与 int jishu = {128 , 64 , 32 , 16 , 8 , 4 , 2 , 1}对应位相乘相加变得到其值, 或
代码实现(1):
void bit_to_Byte(const unsigned char * bit , int bitNum , unsigned char *Byte , int *ByteNum)
{
int iByteNum = 0;//转换后的字节号
int ibitNum = 0;//在位流中的相对位号
int i;
for(i = 0 ; i < bitNum ; i++)
{
iByteNum = i / MAX;
ibitNum = i % MAX;
Byte[iByteNum] += bit[i] * jishu[ibitNum];
}
*ByteNum = bitNum / MAX;B
}
代码实现(二):
void bit_to_Byte(const unsigned char * bit , int bitNum , unsigned char *Byte , int *ByteNum)
{
int i , j;
int Bnum = 0;
unsigned char uchTmp = 0;
for(i = 0 ; i < bitNum ; i += MAX)
{
for(j = 0 ; j < MAX ; j++)
{
uchTmp |= (bit[i + j] << (MAX - 1 - j));//等号右边的意思和上边的差不多;
}
Byte[Bnum] = uchTmp;
Bnum++;
uchTmp = 0;
}
*ByteNum = Bnum
}
函数测试代码:
void test_Byte_bit()
{
unsigned char Byte[] = "dfasdfasgdsfsdafsdfsd";
int size = sizeof(Byte) / sizeof(unsigned char) -1//求字符串的大小,若不减一则求得是字符串的实际内存大小,
int bitNum = MAX * Byte;
unsigned char bit[bitNum]//用来盛放 bit的数组;
}
字节流转为位流:
原理:工作中会遇到将字节流转为位流的情况;
字节流到位流:
unsigned char a[] = "0123456789abcdef";
unsigned char 意味着 1. 任意16进制数ru 0xab , 0x45...;
2.任意小于255的十进制整数(8位),
将字节转为位时 如‘a’,a = 97 , 则97对应十六进制数0X01100001 , 每一次将0x01100001右移一位与0x01相与得到每一位的值,存取时
用书组BIT[I*MAX(8) + MAX - 1 - J]存储;代码实现如下:
void Byte_to_bit(const unsigned *Byte , int& ByteNum , unsigned *bit , int *bitNum)
{
int i , j;
int counter = 0;
for(i = 0 ; i < ByteNum ; i++)
{
for(j = 0 ; j < MAX(8) ; j++)
{
bit[i * MAX + MAX - 1 - j] = ((Byte[i] >> j) & VALUE(0X01));//每个字节对应的位倒着存到数组,正着输出;
counter++;
}
}
*bitNum = counter;
}
位流到字节流:
原理0xff对应的十进制数值为0x11111111与 int jishu = {128 , 64 , 32 , 16 , 8 , 4 , 2 , 1}对应位相乘相加变得到其值, 或
代码实现(1):
void bit_to_Byte(const unsigned char * bit , int bitNum , unsigned char *Byte , int *ByteNum)
{
int iByteNum = 0;//转换后的字节号
int ibitNum = 0;//在位流中的相对位号
int i;
for(i = 0 ; i < bitNum ; i++)
{
iByteNum = i / MAX;
ibitNum = i % MAX;
Byte[iByteNum] += bit[i] * jishu[ibitNum];
}
*ByteNum = bitNum / MAX;B
}
代码实现(二):
void bit_to_Byte(const unsigned char * bit , int bitNum , unsigned char *Byte , int *ByteNum)
{
int i , j;
int Bnum = 0;
unsigned char uchTmp = 0;
for(i = 0 ; i < bitNum ; i += MAX)
{
for(j = 0 ; j < MAX ; j++)
{
uchTmp |= (bit[i + j] << (MAX - 1 - j));//等号右边的意思和上边的差不多;
}
Byte[Bnum] = uchTmp;
Bnum++;
uchTmp = 0;
}
*ByteNum = Bnum
}
函数测试代码:
void test_Byte_bit()
{
unsigned char Byte[] = "dfasdfasgdsfsdafsdfsd";
int size = sizeof(Byte) / sizeof(unsigned char) -1//求字符串的大小,若不减一则求得是字符串的实际内存大小,
int bitNum = MAX * Byte;
unsigned char bit[bitNum]//用来盛放 bit的数组;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp=fopen("test.txt","r"), //要读取的txt
*fp1=fopen("new.txt","w+");
int i,j=0,n,a[8],*b;
while(!feof(fp))
{
for(i=0;i<8;i++)//八位二进制
fscanf(fp,"%1d",&a[i]);
n=a[0]*128+a[1]*64+a[2]*32+a[3]*16+a[4]*8+a[5]*4+a[6]*2+a[7]*1;
//转换成十进制
fprintf(fp1,"%d\n",n);//写进新文件保存
j++;
}
b=(int*)calloc(j,sizeof(int));//分配一维数组保存
rewind(fp1);
for(i=0;i<j;i++)
{
fscanf(fp1,"%d",&b[i]);
printf("%3d ",b[i]);
if(i%10==9)printf("\n");
}
free(b);
fcloseall();//关闭文件
remove("new.txt");//删除
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1.fopen函数打开需要读取的文本,获取该文件的文件描述符
2.使用fscan()函数对文件进行读取。
3.放到二维数组其实就是读取相应格式的数据,然后对应数组的每个位置
4.文件中的二进制数据跟正常的数据是没有区别的。区别在于如何去输出他们。
比如在文件中二进制数据是 0x0A.你读到内存中其实还是以0x0A 存储的。如果直接输出
其值那就是0x0A。如果输出其ASCII码就是一个换行符了。
例如
#include "stdio.h"
#define M 300
#define N 50
void main()
{
int i,j;
char c;
FILE *fp;
if((fp=fopen("test.txt","rt"))==NULL)
{
printf("cannot open file\n");
return;
}
fscanf(fp,"%c",&c);
printf("%X,%c",c,c);
fclose(fp);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询