--- id: c-files-write title: "C Write To Files" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["fprintf", "write vs append mode", "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", "fprintf"] raw_sources: ["https://www.w3schools.com/c/c_files_write.php"] applied_in: [] github_commit: "" --- # [[C Write To Files]] ## ๐ŸŽฏ ํ•œ ์ค„ ํ†ต์ฐฐ (One-line insight) Opening an EXISTING file in `"w"` mode silently DESTROYS its entire prior content the moment `fopen()` succeeds โ€” not when you write, just from opening โ€” the source explicitly warns "you might accidentally erase existing content," meaning `"w"` mode is fundamentally destructive/overwrite-only, and `"a"` (append) exists as the ONLY mode that preserves existing content while adding to it. [S1] ## ๐Ÿง  ํ•ต์‹ฌ ๊ฐœ๋… (Core concepts) - **`fprintf(fptr, text)`** โ€” writes formatted text to the file pointed to by `fptr`, the file-writing analog of `printf()`. [S1] - **`"w"` mode is destructive** โ€” opening an existing file for writing immediately discards its old content, even before any `fprintf()` call. [S1] - **`"a"` (append) mode** โ€” adds new content to the END of the file WITHOUT deleting what's already there. [S1] - **`"a"` also creates missing files** โ€” just like `"w"`, appending to a non-existent file creates it fresh with the appended content as its initial content. [S1] ## ๐Ÿ“– ์„ธ๋ถ€ ๋‚ด์šฉ (Details) - Writing (and overwriting) content: `fptr = fopen("filename.txt", "w"); fprintf(fptr, "Some text"); fclose(fptr);` โ€” then reopening with `"w"` again and writing `"Hello World!"` COMPLETELY replaces "Some text". [S1] - Appending without erasing prior content: `fptr = fopen("filename.txt", "a"); fprintf(fptr, "\nHi everybody!"); fclose(fptr);` โ€” preserves whatever was already in the file. [S1] ## โš–๏ธ ๋ชจ์ˆœ ๋ฐ ์—…๋ฐ์ดํŠธ (Contradictions & updates) - **"w" ๋ชจ๋“œ๋Š” ์—ด์ž๋งˆ์ž ๊ธฐ์กด ๋‚ด์šฉ์„ ์ง€์›€**: ์ด๋ฏธ ์กด์žฌํ•˜๋Š” ํŒŒ์ผ์„ "w" ๋ชจ๋“œ๋กœ ์—ด๋ฉด ์ƒˆ ๋‚ด์šฉ์„ ์“ฐ๊ธฐ ์ „์— ์ด๋ฏธ ๊ธฐ์กด ๋‚ด์šฉ์ด ์‚ฌ๋ผ์ง„๋‹ค๋Š” ์ ์ด ๋ช…์‹œ์ ์œผ๋กœ ๊ฒฝ๊ณ ๋˜๋ฉฐ, ์‹ค์ˆ˜๋กœ ๊ธฐ์กด ๋ฐ์ดํ„ฐ๋ฅผ ๋‚ ๋ฆด ์ˆ˜ ์žˆ๋‹ค๋Š” ์ฃผ์˜์‚ฌํ•ญ์ด ๊ฐ•์กฐ๋จ. [S1] ## ๐Ÿ› ๏ธ ์ ์šฉ ์‚ฌ๋ก€ (Applied in summary) "Some text"๋ฅผ ์“ด ๋’ค ๊ฐ™์€ ํŒŒ์ผ์„ ๋‹ค์‹œ "w" ๋ชจ๋“œ๋กœ ์—ด์–ด "Hello World!"๋กœ ๋ฎ์–ด์“ฐ๋Š” ์˜ˆ์ œ๊ฐ€ ๊ธฐ์กด ๋‚ด์šฉ์ด ์™„์ „ํžˆ ์‚ฌ๋ผ์ง€๋Š” ๊ฒƒ์„ ์ง์ ‘ ๋ณด์—ฌ์ฃผ๋Š” ์‹ค์ „ ๊ฒฝ๊ณ  ์‚ฌ๋ก€๋‹ค. [S1] ## ๐Ÿ’ป ์ฝ”๋“œ ํŒจํ„ด (Code patterns) Appending to a file without erasing its existing content (C): ```c FILE *fptr; fptr = fopen("filename.txt", "a"); // append mode preserves existing content fprintf(fptr, "\nHi everybody!"); 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 Read]], [[C Files]], [[C Type Conversion]] - **์ฐธ์กฐ ๋งฅ๋ฝ:** ํŒŒ์ผ ์„น์…˜ ๋งˆ์ง€๋ง‰ โ€” ํƒ€์ž… ๋ณ€ํ™˜ ๋ฐ ๋งคํฌ๋กœ(Type Conversion & Macros) ์„น์…˜์œผ๋กœ ์ด์–ด์ง. ## ๐Ÿ“š ์ถœ์ฒ˜ (Sources) - [S1] W3Schools โ€” C Write To Files โ€” https://www.w3schools.com/c/c_files_write.php ## ๐Ÿ“ ๋ณ€๊ฒฝ ์ด๋ ฅ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "C Write To Files" page (Astra wiki-curation, P-Reinforce v3.1 format).