파일에서 한 줄을 지우고 싶습니다. 현재 각 줄에 대해 oldTodoFile
으로 반복하고, lineNumber
이 todoNumber
과 같지 않으면 새 파일 (todoFile
)에 추가하십시오. 선을 삭제하는 좋은 방법이 아닌 것 같은데 선을 제거하는 더 좋은 방법이 있습니까?파일에서 행을 제거하는 더 나은 방법은?
FILE *oldTodoFile;
oldTodoFile = fopen("./todo.txt", "r");
FILE *todoFile;
todoFile = fopen("./todo2.txt", "w");
int lineNumber = 0;
int len;
char line[4096];
if (oldTodoFile != NULL) {
while (fgets(line, sizeof line, oldTodoFile)) {
len = strlen(line);
if (len && (line[len - 1] != '\n')) {} else {
lineNumber++;
if (lineNumber == todoNumber) {
// Do nothing
} else {
fputs(line, todoFile);
}
}
}
} else {
printf("ERROR");
}
remove("./todo.txt");
rename("./todo2.txt", "./todo.txt");
fclose(oldTodoFile);
fclose(todoFile);
매우 유사 : http://stackoverflow.com/questions/1151713/delete-a-line-from-a-file-in-c-language –
검색에 나오지 않았습니다. http : // stackoverflow. co.kr/search? q = 더 나은 방법 + + + 제거 + a + 라인 + in + a + 파일 % 3F – errorhandler