@mir@ice.ch3.st
hm my program executes fine but then segfaults
hm my program executes fine but then segfaults
heres the program btw. test.txt and txt2.txt are files with keysmashes and copypasta in the working directory. im just running gcc mircat.c and it spits out a a.out that's executable
/*
my first C program ever! gonna make a copy of `cat` with maybe extra features
idk
/* */
#include
#include
char* greeting = "bonjour";
char *fnames[] = {"test.txt","tst2.txt"};
int catfile (char* file){
FILE *stream ;
int beg ;
int end ;
stream = fopen(file, "r") ; // type `FILE *`
fseek(stream, 0, SEEK_SET);
beg = ftell (stream); // should always be 0
fseek(stream, 0, SEEK_END);
end = ftell (stream); //end of file
char contents[end] ;
for (int i = beg; i <= end ; i++) {
fseek(stream, i, SEEK_SET); // go to next position
contents[i] = fgetc (stream); // get char at said pos and assign it thusly
};
printf ("file %s starts at %ld and ends at %ld\nits contents follow:\n%s\n\n",
file, beg, end,
contents
);
};
int main (){
printf ("%s\n\n",
greeting);
for (int i = 0; i <= 2; i++) {
catfile (fnames[i]);
} ;
return 0;
}
@mir int i = 0; i <= 2; i++ this actually iterates 3 times. it segfaults because it tries to read the 3rd element of the fnames array
@green ope good catch
@green now, what's with these \377 and \375 (broken characters, they render even worse in a normal terminal)
@mir here you go. i hope my voice is cute 