I'm working on a replication of the Unix/Linux Cat utility, and I'm having some difficulty with its implementation. Right now, I'm using getopts to parse out the command line entry and then handling each argument one at a time. I'm reading it into a char array with fread, and I'm not sure if that's even the best way (looping with fgets, potentially?).
Right now I'm trying to append $ to the end of each line, but instead it's replacing the \n with $, and I can't seem to figure out how to place a \n at the end. Any guidance on this and general suggestions would be much obliged.
the main:Code:char case_A( char buf[], int cap ) { char *ptr; int i; for (i=0; i<cap; ++i) { if((ptr = strchr(buf, '\n'))) { *ptr = '$'; } case_T( buf , cap); } return *buf; }
Code:int main(int argc, char * argv[]) { int cap = 0; char choice; char *buf = malloc( cap * sizeof(char)); FILE * fptr; fptr = fopen(argv[2], "r"); do { cap+=10; buf = realloc(buf, sizeof(char) * cap); fread ( buf+cap-10, sizeof( char ), 10, fptr ); } while(!feof(fptr)); while (( choice = getopt (argc, argv, "AbeEnstTuv")) != -1) { <switch statement for choice>



LinkBack URL
About LinkBacks



