C_CPP_Codes
114 subscribers
1 photo
About me :- alokosx.me

Books,notes and more at :- cp_resources.t.me

Discussion :- c_cpp_code.t.me
Download Telegram
11🤩3🥰2😍111
A program that replaces tabs in the input file with the proper number of
blanks to the next tab stop ( A problem from the book C by K&R):


#include <stdio.h>
#include <stdlib.h>

void tab_to_space(FILE *ifp, FILE *ofp) {
int c;
int space = 0,
count = 0;
rewind(ifp);
while((c = getc(ifp)) != EOF) {
if(c == '\t') {
space = (8-(count%8));
while(space != 0) {
putc(' ', ofp);
space--;
count++;
}
} else {
putc(c, ofp);
++count;
}
if(c == '\n')
count = 0;

}
}

int main(int argc, char *argv[]) {
FILE *ifp,
*ofp;
if(argc != 3) {
printf("Need ifp,ofp files");
exit(1);
}
ifp = fopen(argv[1], "r");
ofp = fopen(argv[2], "w");
tab_to_space(ifp, ofp);
fclose(ifp);
fclose(ofp);
return 0;
}
👍54👀1
👩‍💻🔤👩‍💻
🔤🔤🔤🔤🔤✈️
Please open Telegram to view this post
VIEW IN TELEGRAM
5
Live stream started
This media is not supported in the widget
VIEW IN TELEGRAM
👀4