0

I'm writing a program that reads a file and generates an array of integers of each byte, first I prove with a .txt file and I generates the ASCII for each of the letters, but I need to make the same file with the integers of the array, this is my program I can make a new file .txt but it has only trash, or I think is it, new.txt, is the name of the new file .txt and it only has trash, I want to recover the text that is in UAM.txt, and writte in 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;

}

Can someone help me?

  • 2
    Can you please describe what output you expect to see? There's also a number of errors in the code, such as `fopen( "new.txt" , "w" )` inside the loop without an `fclose()`, and `(a & mask[i])/mask[i]` will always be either `0` or `1`. – Ken Y-N Dec 12 '16 at 05:34
  • 2
    Note that [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – Jonathan Leffler Dec 12 '16 at 05:42
  • sorry new.txt, is the name of the new file .txt it has only trash, I want to recover the text that is in UAM.txt, and writte in new.txt – Juan Marcos Jimenez Dec 12 '16 at 05:42

0 Answers0