i'm having a problem with this code. it should read from a file the letters and write them in one other file as in the following example
in.txt:
AAAAAAAAAABCCCCC
AAAAAAAAAAAAAAAAAAAA
BBCDERFG
out.txt
10A1B5C
20A
2B1C1D1E1R1F1G
but i continue to get this...
out.txt
10A1B5C
20A
2B1C1D1E1R1F1G 1A11.
can you tell me why i can't get where i want? thank you in advance
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRING 514
int main(int argc, char *argv[])
{
if(argc!=3) {
fprintf(stderr, "Exactly 3 arguments required");
exit(EXIT_FAILURE);
}
FILE *in;
FILE *out;
int i=0, k=0;
char buffer[MAX_STRING];
char string[MAX_STRING];
in = fopen(argv[1], "r");
if(in==NULL) {
fprintf(stderr, "Couldn't open the file %s", argv[1]);
exit(EXIT_FAILURE);
}
out=fopen(argv[2], "w");
if(out==NULL) {
fprintf(stderr, "Couldn't open the file %s", argv[2]);
exit(EXIT_FAILURE);
}
while(fgets(buffer, MAX_STRING, in)!= NULL) {
i=0;
while(buffer[i]!='\n') {
if(buffer[i]==buffer[i+1]) {
k++;
i++;
}
else {
fprintf(out, "%c%d", buffer[i], k+1);
i++;
k=0;
}
}
fprintf(out, "\n");
}
return 0;
}