Here a routine that works to reverse a string in a UTF-8 encoded file. (It doesn't take into account CRs or LFs.
Todd
Code:
#include <stdio.h>
#include <string.h>
int main (int argc, const char * argv[]) {
char line[81], newline[81] = {0 } ;
char * linep, *newlinep ;
FILE * file ;
if ( (file = fopen("/Users/toddburch/Desktop/myutf8.txt","r+")) == NULL) {
printf("Cannot open FILE\n" ) ;
return -1 ;
}
fgets( line, sizeof(line), file ) ;
// Reverse the string
linep = line ;
newlinep = newline + strlen(line)-3 ; // point to end of string
*newlinep-- = 0 ; // null term char
linep += 3 ; // skip the utf-8 encoding flag bytes
while (*linep) { // Do until we hit the null term byte
if ( (*linep & 0xC0) == 0xC0) { // if 2-byte char, move both chars in order
*(newlinep-1) = *linep++ ;
*newlinep-- = *linep++ ;
newlinep-- ;
}
else {
*newlinep-- = *linep++ ;
}
}
fputs(newline, file) ; // write reversed string
fclose(file) ;
return 0;
}
The output of which looks like this:
Code:
Here is my résumé.
.émusér ym si ereH