forked from bangoc/cgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minh họa cgetline có theo dõi kích thước vùng nhớ đã cấp phát
- Loading branch information
Showing
3 changed files
with
38 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
add_executable(cgetline_demo cgetline_demo.c) | ||
add_executable(cstrdup_demo cstrdup_demo.c) | ||
add_executable(cstrdup_demo cstrdup_demo.c) | ||
add_executable(cgetline_buffsize cgetline_buffsize.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
(C) Nguyen Ba Ngoc | ||
Minh họa sử dụng cgetline | ||
*/ | ||
|
||
#include "ext/io.h" | ||
|
||
#include <string.h> | ||
|
||
int main() { | ||
char *line = NULL; | ||
long n = 0; | ||
printf("Nhập vào 1 dòng (nhập STOP để kết thúc chương trình): "); | ||
for (;;) { | ||
// Có thể sử dụng NULL hoặc 0 thay cho &n nếu không quan tâm đến | ||
// kích thước vùng nhớ được cập phát | ||
cgetline(&line, &n, stdin); | ||
if (strcmp(line, "STOP\n") == 0) { | ||
break; | ||
} | ||
printf("Dòng đã nhập: %s", line); | ||
printf("Độ dài chuỗi ký tự: %zu\n", strlen(line)); | ||
printf("Kích thước vùng nhớ: %ld\n", n); | ||
} | ||
free(line); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters