So I have the following code that I am toying around with:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main (int argc, char* argv[]) {
FILE* fp;
char c, currentchar;
int flag = 0, count = 1;
if (argc < 2) {
printf("wzip:insufficient args\n");
exit(1);
}
fp = fopen(argv[1], "r");
while ( fread(&c, sizeof(char), 1, fp) == 1) {
if (flag == 1) {
if (c == currentchar) {
++count;
}
else {
fwrite(¤tchar, sizeof(char), 1, stdout);
//printf("%d", count);
fwrite(&count, sizeof(int), 1, stdout);
currentchar = c;
count = 1;
}
}
if (flag == 0) {
currentchar = c;
flag = 1;
}
}
printf("\n");
return 0;
}
The above code doesn't provide me the output from the second fwrite() function(that uses the count variable). I assume its a problem regarding stream flushing but I can't resolve it. Any help would be great!
E: for a sample input/output, suppose i have a file test.1 that has the letters sssaa in it.
If I run ./wzip test.1 the output is:
sa
while ideally it should be:
s3a2