用fwrite()用整数数组的c写一个文件

时间:2016-12-12 05:24:55

标签: c arrays

我正在编写一个程序来读取文件并生成每个字节的整数数组,首先我用.txt文件证明我为每个字母生成ASCII,但是我需要制作相同的文件使用数组的整数,这是我的程序,我可以创建一个新文件.txt,但它只有垃圾,或者我认为是,new.txt,是新文件.txt的名称,它只有垃圾,我想恢复UAM.txt中的文本,并在new.txt中写入

#include<stdio.h>

int main(){

int b1, b2, b3, b4, b5, b6, b7, b8;
int x,i;
char a,b;

FILE *f1, *bin, *fp;

b1=0x01; // = 0000 0001
b2=0x02; // = 0000 0010
b3=0x04; // = 0000 0100
b4=0x08; // = 0000 1000
b5=0x10; // = 0001 0000
b6=0x20; // = 0010 0000
b7=0x40; // = 0100 0000
b8=0x80; // = 1000 0000

int mask[8] = {b8, b7, b6, b5, b4, b3, b2, b1};
int rest[8];

f1 = fopen("UAM.txt","rb");


while(!feof(f1)){
    a=getc(f1);
    printf("%d\n",a);

    for(i=0;i<=7;i++){
        rest[i] = a & mask[i];
    }

    for(i=0;i<=7;i++){
        rest[i]=rest[i]/mask[i];
    }

    for(x=0;x<=7;x++){
        printf("%i",rest[x]);
        fp = fopen( "new.txt" , "w" );
        fwrite(rest , 8 , sizeof(rest) , fp );
     }
        printf("\n");
       fclose(fp);
    }
 return 0;

}

有人可以帮助我吗?

0 个答案:

没有答案