--- id: c-files-read title: "C Read Files" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["fgets file reading", "read multiple lines", "C 파일 읽기"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.86 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["c", "programming-language", "w3schools", "files", "fgets"] raw_sources: ["https://www.w3schools.com/c/c_files_read.php"] applied_in: [] github_commit: "" --- # [[C Read Files]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A single `fgets()` call only reads ONE LINE, not the whole file β€” the source's own example proves it by showing "filename.txt" has two lines but only "Hello World!" prints from one call, and the fix (wrapping `fgets()` in a `while` loop) works because `fgets()` naturally returns falsy/NULL once it hits end-of-file, making the loop condition double as the "keep reading until there's nothing left" check with no separate end-of-file test needed. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`fopen(filename, "r")`** β€” opens a file specifically for reading. [S1] - **`fgets(buffer, maxSize, fptr)`** β€” reads UP TO `maxSize` characters (or until a newline) from `fptr` into `buffer`; reads only ONE line per call. [S1] - **`while (fgets(...))` pattern** β€” repeatedly calling `fgets()` inside a `while` loop reads the ENTIRE file line by line; the loop naturally stops once `fgets()` reaches end-of-file (returns a falsy value). [S1] - **NULL-check for missing files** β€” `fopen()` returns `NULL` if the file doesn't exist for reading; checking `if (fptr == NULL)` before use prevents crashes. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Reading only the FIRST line (a common early mistake): `FILE *fptr = fopen("filename.txt", "r"); char myString[100]; fgets(myString, 100, fptr); printf("%s", myString); // only "Hello World!", missing the second line`. [S1] - Reading the ENTIRE file with a while loop: `while(fgets(myString, 100, fptr)) { printf("%s", myString); } // prints both "Hello World!" and "Hi everybody!"`. [S1] - Combining NULL-checking with the full-file read pattern: `if(fptr != NULL) { while(fgets(myString, 100, fptr)) { printf("%s", myString); } } else { printf("Not able to open the file."); }`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **fgets ν•œ 번 ν˜ΈμΆœλ‘œλŠ” 첫 μ€„λ§Œ 읽힘**: νŒŒμΌμ— μ—¬λŸ¬ 쀄이 μžˆμ–΄λ„ fgets()λ₯Ό ν•œ 번만 ν˜ΈμΆœν•˜λ©΄ 첫 μ€„λ§Œ μ½νžŒλ‹€λŠ” 점이 직접 비ꡐ 예제둜 증λͺ…λ˜λ©°, 전체 νŒŒμΌμ„ 읽으렀면 while λ£¨ν”„λ‘œ 감싸야 ν•œλ‹€λŠ” 해법이 μ œμ‹œλ¨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) 파일이 μ‘΄μž¬ν•˜μ§€ μ•Šμ„ 경우 NULL을 체크해 "νŒŒμΌμ„ μ—΄ 수 μ—†μŠ΅λ‹ˆλ‹€" λ©”μ‹œμ§€λ₯Ό 좜λ ₯ν•˜λŠ” μ•ˆμ „ν•œ 파일 읽기 νŒ¨ν„΄μ΄ μ›λ¬Έμ—μ„œ 직접 μ‹€μ „ ν™œμš© μ‚¬λ‘€λ‘œ μ œμ‹œλ¨. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Safely reading an entire file line by line, with a NULL check for missing files (C): ```c FILE *fptr; fptr = fopen("filename.txt", "r"); char myString[100]; if (fptr != NULL) { while (fgets(myString, 100, fptr)) { printf("%s", myString); } } else { printf("Not able to open the file."); } fclose(fptr); ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[C Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[C Files]], [[C User Input]], [[C Files Write]] - **μ°Έμ‘° λ§₯락:** 파일 읽기 β€” 파일 μ“°κΈ°(Files Write) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” C Read Files β€” https://www.w3schools.com/c/c_files_read.php ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C Read Files" page (Astra wiki-curation, P-Reinforce v3.1 format).