I am reading a file called "dictionary.txt" by fgets and print out, but like 10% of the head text from the "dictionary.txt" is lost when I run the program.
I suspect whether it is the size of the buffer is small, but changing MAX_INT to bigger numbers doesn't help either.
#include <stdio.h>
#include<string.h>
#define MAX_INT 50000
void main() {
FILE *fp;
char* inp = (char*)malloc(sizeof(char)*MAX_INT);
int i;
int isKorean = 0;
char* buffer[MAX_INT];
char* ptr = (char*)malloc(sizeof(char)*MAX_INT);
if (fp = fopen("C://Users//user//Desktop//dictionary.txt", "r")) {
while (fgets(buffer, sizeof(buffer), fp)) {
ptr = strtok(buffer, "/"); //a line is looking like this : Umberto/영어("English" written in Korean)
for (i = 0; i < strlen(ptr); i++) {
if ((ptr[i] & 0x80) == 0x80) isKorean = 1; //check whether it's korean
if (!isKorean) printf("%c", ptr[i]); //if it's not korean, then print one byte
else {
printf("%c%c", ptr[i], ptr[i + 1]); //if it's korean, then print two bytes
i++;
}
isKorean = 0;
printf("\n");
}
ptr = strtok(NULL, " ");
printf("tagger:%s\n", ptr); //print the POS tagger of the word(it's in dictionary)
}
fclose(fp);
}
}
The function fgets has this syncpsis:
char *
fgets(char * restrict str, int size, FILE * restrict stream);
So why make buffer as pointer array?
char buffer[MAX_INT]
is what we need.
And the following statement:
if (fp = fopen("/Users/weiyang/code/txt", "r"))
is not safe, it’s better to add parentheses after assignment.