ok, so I am writing this code, and I keep getting a seg fault. I went into gdb and it told me that it was seg faulting on the strcat() function.
here is my code
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
const char* soh="00000001";
const char* eot="00000100";
const char* esc="00011011";
const char* soh_e="0001101101000001";
const char* eot_e="0001101101000010";
const char* esc_e="0001101101000011";
const char*z="0";
const char*o="1";
unsigned char bin[]={1,2,4,8,16,32,64,128};
char bowling[4096];
char goofy[4096];
int main(int argc, char*argv[]){
if (argc < 2){
printf("You need to enter a filename");
exit(1);
}
int fd=open(argv[2], O_RDWR, 0);
read (fd, goofy, 4096);
int count;
char string[4096];
int leng=strlen(goofy);
int i;
printf("Hello");
for (i=0; i<leng; i++){//This portion of code is paraphrased from klausi's code
char c=*string; /* the character string points to at the moment */
for(count=7;count>=0;count--){
if(c>=bin[count])
{
strcat(string, o);
c-=bin[count];
}
else{
strcat(string,z);
}
}
}
int j;
for (j=0; j<strlen(string)-8; j++){
size_t ch=strncmp(&string[j],soh, 8);
size_t cha=strncmp(&string[j], eot, 8);
size_t chan=strncmp(&string[j], esc, 8);
if (ch==0){
strcat(bowling, soh_e);
j+=7;
}
else if (cha==0){
strcat(bowling, eot_e);
j+=7;
}
else if (chan==0){
strcat(bowling, esc_e);
j+=7;
}
else{
strcat(bowling, &string[j]);
}
}
int fd2=open("fdhjmk.txt", O_WRONLY, O_CREAT|O_TRUNC);
write(fd2, bowling, 4096);
close(fd);
close(fd2);
}