Wednesday, 5 July 2017

C ไฟล์ จัดการ ไบนารี ตัวเลือก


C การจัดการแฟ้มตัวเลือกไบนารีคอนโทรลเลอร์สำหรับการส่งค่าคู่ค่าที่เรียกว่าการควบคุมที่ประสบความสำเร็จเมื่อมีการส่งแบบฟอร์มเฉพาะในการควบคุมช่องทำเครื่องหมายจะกลายเป็นที่ประสบความสำเร็จ C การจัดการไฟล์ไบนารีตัวเลือกตัวเลือกไบนารีการค้าสำหรับบล็อกที่ใช้งานได้เริ่มโปรแกรมอาหารในที่แยกจากกัน ประมวลผลและส่งคืนที่จับไฟล์ที่คุณสามารถใช้เพื่ออ่านข้อมูลจากโปรแกรมนี้ได้หลังจากจัดการตัวเลือกแล้ว lua หลายช่องทำเครื่องหมายในแบบฟอร์มอาจใช้ชื่อการควบคุมเดียวกันผู้ใช้โดยทั่วไปกรอกแบบฟอร์มโดยการปรับเปลี่ยนตัวควบคุมป้อนข้อความเลือกรายการเมนู ฯลฯ ก่อนที่จะส่งแบบฟอร์มไปยังตัวแทนสำหรับการประมวลผลเช่นไปยังเว็บเซิร์ฟเวอร์ไปยังเซิร์ฟเวอร์จดหมาย ฯลฯ องค์ประกอบเมื่อมีการส่งแบบฟอร์มสำหรับการประมวลผลตัวควบคุมบางตัวจะมีชื่อคู่กับค่าปัจจุบันและส่งคู่นี้พร้อมกับแบบฟอร์ม โปรดดูคำจำกัดความของแต่ละตัวควบคุมสำหรับข้อมูลเกี่ยวกับค่าเริ่มต้นและข้อ จำกัด ที่เป็นไปได้เกี่ยวกับค่าที่กำหนดโดยการควบคุมตัวแปรระบบสามารถตั้งค่าได้ที่ ser เริ่มต้น ver โดยใช้ตัวเลือกในบรรทัดคำสั่งหรือในแฟ้มตัวเลือกค่าดีฟอลต์คือไบนารีซึ่งหมายความว่าไม่มี C การจัดการไฟล์ไบนารีอ็อพชัน Unbolting Binary Trading Binary ผูกนิพจน์สเกลาให้เป็นรูปแบบที่ตรงกับตัวเลือกที่ไม่ทำลายซึ่งทำให้ค่าส่งคืนเป็นผลมาจากการทดแทนการจัดการข้อมูลไบนารีหมายเหตุว่าแม้ว่าในตัวอย่างนี้จะมีฟิลด์ป้อนข้อมูลบางไฟล์เมื่อคุณส่งแบบฟอร์มผ่าน FormData API ดูส่วนการส่งแบบฟอร์มสำหรับข้อมูลการใช้งานเริ่มโปรแกรม prog ในกระบวนการที่แยกจากกันและส่งคืนหมายเลขไฟล์ที่คุณสามารถใช้เพื่ออ่านข้อมูลจากโปรแกรมนี้ได้หลังจากจัดการตัวเลือกแล้ว lua HTML กำหนดชนิดของตัวควบคุมต่อไปนี้ไว้ดังนี้ ตัวอย่างเช่นช่องทำเครื่องหมายอนุญาตให้ผู้ใช้เลือกค่าต่างๆสำหรับคุณสมบัติเดียวกัน C การจัดการไฟล์ไบนารีตัวเลือกสำหรับข้อมูลอินพุตที่ได้รับการยอมรับจากเซิร์ฟเวอร์การประมวลผลไบนารีตัวเลือกผู้เชี่ยวชาญด้านการทบทวนวิธีการ 0809 ไบนารี ผูกนิพจน์สเกลาให้เป็นรูปแบบที่ตรงกับตัวเลือกที่ไม่ทำลายซึ่งทำให้ค่าที่ส่งคืนเป็นผลมาจากการทดแทนนอกจากนี้ยังจะทำงานกับไฟล์ MS Word DOCX ที่จะทำงานได้ดีเช่นเดียวกับ LibreOffice สามารถจัดการกับ ODT โดยเฉพาะได้และ ทั่วไปไฟล์ทั้งหมดตัวเลือก Norge Kontor Instaforex เริ่มต้นโปรแกรม prog ในกระบวนการแยกและส่งคืนหมายเลขแฟ้มที่คุณสามารถใช้เพื่ออ่านข้อมูลจากโปรแกรมนี้หลังจากจัดการตัวเลือกของตน lua ค่าที่เป็นไปได้กรณีที่ไม่ดีจะได้ค่าเริ่มต้นและ post. Each การควบคุมมีทั้งค่าเริ่มต้นและค่าปัจจุบันซึ่งทั้งสองอย่างนี้เป็นสตริงอักขระโดยทั่วไปการควบคุมดังนั้นเมื่อมีการรีเซ็ตรูปแบบค่าปัจจุบันของตัวควบคุมแต่ละตัวจะถูกรีเซ็ตเป็นค่าเริ่มต้น C การจัดการไฟล์ค่าไบนารีตัวเลือกการเรียกเก็บเงินสำหรับ กลยุทธ์การซื้อขายภายในวันถ้าการควบคุมไม่ได้มีค่าเริ่มต้นผลกระทบของการรีเซ็ตแบบฟอร์มในตัวควบคุมนั้นจะไม่ได้กำหนด C การจัดการไฟล์ไบนารีตัวเลือกถ้าไม่มีปุ่มวิทยุในชุดที่มีชื่อควบคุมเดียวกัน ในตอนแรกลักษณะการทำงานของเอเจนต์ผู้ใช้สำหรับการเลือกการควบคุมที่เริ่มต้นยังไม่ได้กำหนดเนื่องจากการใช้งานที่มีอยู่จัดการกับกรณีนี้แตกต่างออกไปข้อกำหนดทางเทคนิคปัจจุบันแตกต่างจาก RFC 1866 RFC1866 ส่วน 8 1 2 4 ซึ่งระบุว่าผู้สร้างอาจสร้างตัวควบคุมที่ไม่ได้แสดงผล แต่มี ค่าจะถูกส่งด้วยแบบฟอร์มด้านบนของทั้งสองมีคำสั่ง PageEx ซึ่งจะช่วยให้คุณสามารถเพิ่มหน้าเว็บให้กับเพจใดก็ได้และมีจำนวนตัวเลือกมากขึ้นที่เปิดไฟล์เปิดใช้งานแอ็ตทริบิวต์นี้ระบุว่าจะใช้วิธีใดในการส่งข้อมูลฟอร์ม ชุด ช่องทำเครื่องหมายเช็คบ็อกซ์ปุ่มเมนู ฯลฯ และป้ายกำกับในตัวควบคุมเหล่านี้ C การจัดการไฟล์ตัวเลือกไบนารีค่าเป็นรายการที่มีการเว้นวรรคเป็นตัวคั่นด้วยเครื่องหมายจุลภาคและใช้ตัวควบคุมประเภทนี้เพื่อเก็บข้อมูลระหว่างการแลกเปลี่ยนเซิร์ฟเวอร์ไคลเอ็นต์ ลูกค้าจะต้องตีความรายการนี้เป็นรายการพิเศษหรือเช่นเซิร์ฟเวอร์สามารถรับการเข้ารหัสอักขระหนึ่งตัวต่อหนึ่งเอนทิตีที่ได้รับเท่านั้น [19] 2013 1 - - -, 15 2009 IDM. ไฟล์ IO และไฟล์ไบนารีฉัน O. By Alex Allain ในบทแนะนำนี้คุณจะได้เรียนรู้วิธีการทำไฟล์ IO, ข้อความและไบนารีใน C โดยใช้ fpen fwrite และ freadf fsfff fgetc และ fputc. For C File IO คุณจำเป็นต้องใช้ FILE pointer ซึ่งจะทำให้โปรแกรมสามารถติดตามได้ ของไฟล์ที่มีการเข้าถึงคุณสามารถคิดว่าเป็นที่อยู่หน่วยความจำของไฟล์หรือตำแหน่งของไฟล์เพื่อเปิดไฟล์ที่คุณต้องใช้ฟังก์ชัน fopen ซึ่งจะส่งกลับตัวชี้ FILE เมื่อคุณเปิดไฟล์แล้วคุณสามารถ ใช้ตัวชี้แฟ้มเพื่อให้คอมไพเลอร์ดำเนินการฟังก์ชันอินพุตและเอาต์พุตในไฟล์ในชื่อไฟล์ถ้าคุณใช้อักษรแบบสตริงเป็นอาร์กิวเมนต์คุณต้องจำใช้เครื่องหมายทับขวาคู่แทนเครื่องหมายแบ็กสแลชเดียวเนื่องจากคุณเสี่ยงตัวหนี เช่น t ใช้เครื่องหมายทับขวาสองครั้งหนีคีย์ดังนั้นสตริงทำงานตามที่คาดหวังผู้ใช้ของคุณแน่นอนไม่จำเป็นต้องทำเช่นนี้มันเป็นเพียงวิธีการยกสตริงที่มีการจัดการในโหมด C และ C. fopen อนุญาตโหมด สำหรับ fopen มีดังต่อไปนี้โปรดทราบว่าเป็นไปได้สำหรับ fopen ล้มเหลวแม้ว่าโปรแกรมของคุณถูกต้องสมบูรณ์คุณอาจพยายามเปิดไฟล์ที่ระบุโดยผู้ใช้และไฟล์ที่อาจไม่อยู่หรืออาจจะมีการป้องกันการเขียนในกรณีเหล่านี้, fopen จะกลับ 0 ตัวชี้เป็น NULL นี่คือ ตัวอย่างง่ายๆในการใช้ fopen รหัสนี้จะเปิดขึ้นสำหรับการอ่านในโหมดข้อความในการเปิดไฟล์ในโหมดไบนารีคุณต้องเพิ่ม ab ลงที่ส่วนท้ายของสตริงโหมดตัวอย่างเช่น rb สำหรับโหมดการอ่านและการเขียนคุณสามารถเพิ่ม b ได้เช่นกัน หลังจากเสร็จสิ้นการทำงานกับไฟล์แล้วคุณควรปิดไฟล์โดยใช้ฟังก์ชัน fclose ส่งค่าเป็นศูนย์ถ้าไฟล์ปิดสำเร็จตัวอย่างเช่น fclose is. Reading และการเขียนด้วย fprintf, fscanf fputc และ fgetc. To ทำงานกับ text input และ output คุณใช้ fprintf และ fscanf ซึ่งทั้งสองอย่างนี้คล้ายคลึงกับ printf และ scanf ของเพื่อนยกเว้นว่าคุณต้องผ่าน FILE pointer เป็นอาร์กิวเมนต์แรกตัวอย่างเช่นสามารถอ่านได้ หรือเขียนตัวอักษรทีละตัวซึ่งจะมีประโยชน์หากคุณต้องการป้อนข้อมูลอักขระตามตัวอักษรตัวอย่างเช่นถ้าคุณต้องการติดตามส่วนเครื่องหมายวรรคตอนทุกชิ้นในไฟล์จะทำให้รู้สึกอ่านได้ดีขึ้น อักขระตัวเดียวมากกว่าการอ่านในสตริงทีละเฟรม tc ซึ่งใช้ตัวชี้แฟ้มและส่งกลับค่า int จะช่วยให้คุณสามารถอ่านอักขระตัวเดียวจากไฟล์ได้สังเกตเห็นว่า fgetc ส่งกลับค่า int สิ่งที่จริงหมายถึงนี้คือเมื่ออ่านอักขระปกติในไฟล์ ค่าที่เหมาะสมสำหรับการเก็บข้อมูลใน unsigned char โดยทั่วไปคือตัวเลขที่อยู่ในช่วง 0 ถึง 255 ในอีกทางหนึ่งเมื่อคุณกลับมาที่ท้ายสุดของไฟล์คุณจะไม่สามารถรับค่าตัวอักษรได้ในกรณีนี้ fgetc จะกลับมา EOF ซึ่งเป็นค่าคงที่ซึ่งระบุว่าคุณได้มาถึงจุดสิ้นสุดของไฟล์แล้วเมื่อต้องการดูตัวอย่างเต็มรูปแบบโดยใช้ fgetc ในทางปฏิบัติลองดูตัวอย่างที่นี่ฟังก์ชั่น fputc ช่วยให้คุณสามารถเขียนอักขระได้ตลอดเวลา - คุณ อาจพบว่ามีประโยชน์ถ้าคุณต้องการคัดลอกอักขระไฟล์ตามอักขระลักษณะนี้หมายเหตุว่าอาร์กิวเมนต์แรกควรอยู่ในช่วงของอักขระที่ไม่ได้ลงชื่อเพื่อให้เป็นอักขระที่ถูกต้องอาร์กิวเมนต์ที่สองคือไฟล์ที่จะเขียนถึงบนความสำเร็จ , fputc จะคืนค่า c และเมื่อความล้มเหลวจะส่งกลับค่า EOF. B ไฟล์ inary IO - fread และ fwrite. For ไฟล์ไบนารี IO ที่คุณใช้ fread และ fwrite. declarations สำหรับแต่ละไฟล์มีความคล้ายกันทั้งสองฟังก์ชั่นเหล่านี้จัดการกับ blocks of memories - มักเป็น arrays เนื่องจากยอมรับคำแนะนำคุณสามารถใช้ฟังก์ชันเหล่านี้กับข้อมูลอื่น ๆ โครงสร้างคุณสามารถเขียน structs ไฟล์หรืออ่าน struct ลง memory. Let s ดูที่ฟังก์ชันหนึ่งเพื่อดูว่า notation works. fread ใช้เวลาสี่อาร์กิวเมนต์ Don t สับสนโดยการประกาศของ void ptr โมฆะหมายความว่าเป็น ตัวชี้ที่สามารถใช้สำหรับตัวแปรชนิดใดก็ได้อาร์กิวเมนต์แรกคือชื่ออาร์เรย์หรือที่อยู่ของโครงสร้างที่คุณต้องการจะเขียนลงในไฟล์อาร์กิวเมนต์ที่สองคือขนาดของแต่ละองค์ประกอบของอาร์เรย์เป็นไบต์ ถ้าคุณมีอาร์เรย์ของตัวอักษรคุณจะต้องการอ่านในชิ้นหนึ่งไบต์เพื่อ sizeofelements หนึ่งคุณสามารถใช้ตัวดำเนินการ sizeof เพื่อให้ได้ขนาดของประเภทข้อมูลต่างๆตัวอย่างเช่นถ้าคุณมีตัวแปร x int คุณจะได้รับ ขนาดของ x กับ siz eof x การใช้งานนี้แม้กระทั่งสำหรับโครงสร้างหรืออาร์เรย์ E g ถ้าคุณมีตัวแปรประเภท struct ที่มีชื่อ astruct คุณสามารถใช้ sizeofstructor เพื่อหาจำนวนหน่วยความจำที่ใช้ขึ้นอาร์กิวเมนต์ที่สามเป็นเพียงจำนวนองค์ประกอบ คุณต้องการอ่านหรือเขียนตัวอย่างเช่นถ้าคุณผ่านอาร์เรย์องค์ประกอบ 100 คุณต้องการอ่านไม่เกิน 100 องค์ประกอบเพื่อให้คุณผ่าน 100 อาร์กิวเมนต์สุดท้ายเป็นเพียงตัวชี้แฟ้มที่เราเคยใช้เมื่อ fread ใช้, หลังจากที่ถูกส่งผ่านอาร์เรย์ fread จะอ่านจากไฟล์จนเต็มอาร์เรย์และจะแสดงจำนวนองค์ประกอบที่อ่านจริงถ้าไฟล์ตัวอย่างเช่นมีเพียง 30 ไบต์ แต่คุณพยายามอ่านไบต์ 100 ไฟล์ จะกลับมาว่าอ่าน 30 bytes ตรวจสอบเพื่อให้แน่ใจว่ามีการสิ้นสุดไฟล์แล้วใช้ feof function ซึ่งยอมรับตัวชี้ FILE และส่งคืนค่าจริงถ้าไฟล์ถึงจุดสิ้นสุดของไฟล์ fwrite มีลักษณะคล้ายกันในการใช้งานยกเว้นแทน อ่านลงในหน่วยความจำที่คุณเขียนจากหน่วยความจำลงในไฟล์ฉันกำลังมองหาซิมมาก ple คำอธิบายเกี่ยวกับสิ่งที่ธงฉันเข้าใจว่าการทำงานธงระบุคำสั่งให้ทำเช่น. ฉันรู้ว่าคำสั่ง rm จะลบโฟลเดอร์ทดสอบและที่ธง Rf จะบังคับคำสั่งเพื่อลบไม่เพียง แต่โฟลเดอร์ แต่ ไฟล์ใน it. But ที่ธงอ่านเรียบเรียงอะไรธงจัดการสิ่งที่ฉันสามารถเช่นเขียนโปรแกรม CC ของฉันเองและกำหนดธงที่แตกต่างกันเพื่อให้โปรแกรมไม่สิ่งที่แตกต่างฉันหวังว่าฉันถามคำถามที่ถูกต้องถ้าไม่โปรด แจ้งให้ฉันทราบ 6 กุมภาพันธ์ 13 ที่ 20 16.At ระดับ C อาร์กิวเมนต์บรรทัดคำสั่งให้โปรแกรมปรากฏในพารามิเตอร์ไปยังฟังก์ชันหลักตัวอย่างเช่นถ้าคุณคอมไพล์โปรแกรมนี้และเรียกมันด้วยอาร์กิวเมนต์เดียวกับตัวอย่างของคุณ rm คำสั่งคุณจะได้รับนี้คุณสามารถดูรายการแรกใน argv เป็นชื่อของโปรแกรมเองและส่วนที่เหลือของรายการอาร์เรย์เป็นอาร์กิวเมนต์บรรทัดคำสั่งระบบปฏิบัติการไม่สนใจที่ทุกสิ่งที่ขัดแย้งเป็น มันขึ้นอยู่กับโปรแกรมของคุณเพื่อตีความพวกเขา อย่างไรก็ตามมีการประชุมสำหรับวิธีที่พวกเขาทำงานซึ่งต่อไปนี้เป็นสิ่งที่สำคัญที่สุดอาร์กิวเมนต์จะแบ่งออกเป็นตัวเลือกและตัวเลือกที่ไม่ใช่ตัวเลือกเริ่มต้นด้วยเส้นประไม่ใช่ตัวเลือก don t. Options เป็นชื่อหมายถึงควรจะ เป็นตัวเลือกถ้าโปรแกรมของคุณต้องการอาร์กิวเมนต์บรรทัดคำสั่งบางอย่างเพื่อทำทุกอย่างที่เป็นประโยชน์อาร์กิวเมนต์เหล่านั้นควรเป็นตัวเลือกที่ไม่ใช่ตัวเลือกนั่นคือไม่ควรเริ่มต้นด้วยเครื่องหมายขีดกลางตัวเลือกสามารถแบ่งออกเป็นตัวเลือกสั้น ๆ ซึ่งเป็นรีบเดียวตามด้วย ตัวอักษรตัวเดียว - r - f และตัวเลือกที่ยาวซึ่งเป็นเครื่องหมายขีดกลางสองขีดที่ตามด้วยคำที่แยกออกจากกันตั้งแต่หนึ่งคำขึ้นไปโดยใช้คำที่แยกออกจากกันเป็นคำ ๆ หนึ่งคำหรือคำที่ใช้เรียกกันทั่วไป - ตัวเลือกสั้น ๆ สามารถรวมเข้าด้วยกันเป็นอาร์กิวเมนต์หนึ่งตัวได้ตราบเท่าที่ไม่มี ของอาร์กิวเมนต์ใช้อาร์กิวเมนต์ดูด้านล่างตัวเองอาจใช้อาร์กิวเมนต์อาร์กิวเมนต์เป็นตัวเลือกสั้น x คือส่วนที่เหลือของรายการ argv หรือหากไม่มีข้อความใด ๆ เพิ่มเติมในรายการนั้นรายการ argv ถัดไปไม่ว่าจะเป็นหรือไม่ เริ่มต้นด้วย dash อาร์กิวเมนต์เพื่อ opti ยาว on ถูกกำหนดด้วยเครื่องหมายเท่ากับหากเป็นไปได้ทั้งหมดการสั่งซื้อญาติของตัวเลือกที่แตกต่างกันกับอาร์กิวเมนต์ของพวกเขาควรจะมีผลที่ไม่สามารถสังเกตได้ตัวเลือกพิเศษ - หมายถึงไม่รักษาอะไรหลังจากจุดบนบรรทัดคำสั่งนี้เป็นตัวเลือก, แม้ว่าจะมีลักษณะคล้าย ๆ กันก็ตามตัวอย่างเช่นคุณสามารถลบไฟล์ที่ชื่อว่า - f โดยการพิมพ์ rm - f ตัวเลือกพิเศษหมายถึงอ่านข้อมูลมาตรฐานโดยมีตัวอักษรตัวเล็ก ๆ ที่สงวนไว้สำหรับอนุสัญญา ที่สำคัญที่สุดคือ - v be verbose. - h พิมพ์ไฟล์ help-text ส่งออกไปยังแฟ้ม - f force don t prompt เพื่อยืนยันการกระทำที่เป็นอันตรายเพียงทำพวกเขามีพวงของไลบรารีที่ช่วยให้คุณแยกคำสั่ง line อาร์กิวเมนต์แบบพกพามากที่สุด แต่ยัง จำกัด มากที่สุดของเหล่านี้เป็น getopt ซึ่งถูกสร้างขึ้นในห้องสมุด C ในระบบส่วนใหญ่ในปัจจุบันผมขอแนะนำให้คุณอ่านเอกสารทั้งหมดสำหรับ GNU argp แม้ว่าคุณ don t ต้องการใช้ที่เฉพาะอย่างใดอย่างหนึ่ง, เพราะมันจะให้ความรู้แก่คุณต่อไปในอนุสัญญา th กล่าวว่าการขยายตัว wildcard rm-rf จะกระทำก่อนที่โปรแกรมของคุณจะถูก invoked ถ้าคุณรันโปรแกรมตัวอย่างด้านบนเช่นเดียวกับในไดเร็กทอรีที่มีเฉพาะไบนารีและซอร์สโค้ดที่คุณจะได้รับตอบ 6 กุมภาพันธ์ที่ 20 34.flags เป็นอาร์กิวเมนต์ ผ่านเข้าไปในจุดเริ่มต้นของโปรแกรมตัวอย่างเช่นในโปรแกรม C คุณสามารถมีส่วนโค้งของคุณเป็นอาร์กิวเมนต์ที่ผ่านเข้าและตัวชี้ให้ u รายการอาร์กิวเมนต์ที่แท้จริงดังนั้น for. argc จะเป็น 3 และ argv array จะมีอาร์กิวเมนต์ของคุณหมายเหตุ argc 1 เนื่องจากชื่อโปรแกรมเองนับ rm-RF เป็นพารามิเตอร์ที่ 2 ของคุณและการทดสอบของคุณคือ third. So ของคุณเมื่อใดก็ตามที่คุณกำลังพิมพ์คำสั่งใน unix คุณเป็นหลักรันโปรแกรมและผ่านพารามิเตอร์ที่พวกเขาทำงานอยู่ หากคุณสนใจจริงๆในระบบยูนิกซ์โอเอสคุณควรมองหาส้อมและวิธีการทำงานสิ่งนี้อาจสร้างความสับสนแก่ผู้ใช้ใหม่ ๆ ได้ดังนั้นหากคุณสนใจจริงๆในระบบปฏิบัติการและวิธีที่โปรแกรมถูกเรียกใช้งาน 20 22.GNU libc, ซึ่งมีความเป็นไปได้มากที่จะมีอยู่ในระบบของคุณมีไลบรารีสำหรับ getopt นี้ซึ่งสามารถใช้เพื่อแยกวิเคราะห์ตัวเลือกต่างๆในแบบที่สมเหตุสมผลมีตัวอย่างเพื่อช่วยคุณเริ่มต้นในเอกสารที่เชื่อมโยงด้านล่างได้ตอบเมื่อวันที่ 20 มีนาคม 16 เวลา 14.30 น. Answer.2017 Stack Exchange, Inc วิธีการเปิดไฟล์วิธีการอ่านและเขียนไฟล์ที่เปิดอยู่วิธีการปิดไฟล์สร้างหนังสือที่อยู่การจัดการข้อมูลไบนารีไฟล์การเข้าถึงแบบสุ่มเพื่อไฟล์ข้อมูลการจัดการไฟล์มักจะโพสท่า ปัญหาสำหรับผู้เริ่มต้นแม้ว่าเหตุผลนี้ปริศนาฉันเล็กน้อยไฟล์ในความรู้สึกโปรแกรมไม่มากแตกต่างจากไฟล์ที่คุณใช้ในโปรแกรมประมวลผลคำหรือโปรแกรมอื่น ๆ ที่คุณเปิดพวกเขาทำงานบางอย่างแล้วปิดพวกเขาอีกครั้งความแตกต่างที่ใหญ่ที่สุดคือ ว่าในโปรแกรมที่คุณเข้าถึงไฟล์ตามลำดับคือคุณอ่านหนึ่งบรรทัดในเวลาเริ่มต้นที่จุดเริ่มต้นในทางปฏิบัติประมวลผลคำมักจะเหมือนกันก็แค่เก็บไฟล์ทั้งหมดในหน่วยความจำในขณะที่คุณทำงานกับมันแล้วเขียนมัน ออกทั้งหมด เมื่อคุณปิดมันความแตกต่างอื่น ๆ ก็คือเมื่อเขียนโปรแกรมคุณปกติจะเปิดไฟล์เป็นแบบอ่านอย่างเดียวหรือเขียนเท่านั้นคุณสามารถเขียนโดยการสร้างไฟล์ใหม่ตั้งแต่เริ่มต้นหรือเขียนทับไฟล์ที่มีอยู่หรือโดยการผนวกกับสิ่งที่มีอยู่หนึ่งอย่างอื่น คุณสามารถทำในขณะที่การประมวลผลไฟล์คือคุณสามารถกลับไปที่จุดเริ่มต้นแฟ้ม - Input และ Output. Let s เห็นว่าในทางปฏิบัติเราจะสมมติว่ามีไฟล์ที่เรียกว่าและที่เก็บรายชื่อของอาหารตอนนี้เราจะเขียน โปรแกรมอ่านไฟล์และแสดงผลลัพธ์ - เช่นคำสั่ง cat ใน Unix หรือคำสั่ง type ใน DOS ไฟล์คำอธิบาย 1 ใช้อาร์กิวเมนต์สองตัวแรกคือชื่อไฟล์ที่สามารถส่งผ่านเป็นตัวแปรหรือสตริงที่แท้จริงได้เช่นเดียวกับที่เราทำ ที่นี่โหมดที่สองโหมดกำหนดว่าเราจะเปิดไฟล์สำหรับอ่าน r หรือเขียน w และยังไม่ว่าจะเป็นสำหรับข้อความ ASCII หรือการใช้ไบนารีโดยการเพิ่ม ab ไปที่ r หรือ w เช่นเดียวกับ open fn, rb หมายเหตุ 2 เราใช้ฟังก์ชั่นไฟล์เพื่อเปิดไฟล์รุ่นเก่าของ Python ใช้ th แต่เนื่องจากเปิดยังคงเป็นกลไกที่เรามักจะใช้เปิด แต่ถ้าคุณพบไฟล์ตรรกะมากขึ้นแล้วรู้สึกฟรีเพื่อใช้แทนหมายเหตุ 3 เราอ่านและปิดไฟล์โดยใช้ฟังก์ชันที่นำหน้า โดยตัวแปรของไฟล์สัญกรณ์นี้เรียกว่าเมธอดการอุทธรณ์และเป็นตัวอย่างของการปฐมนิเทศอ็อบเจ็กต์อย่ากังวลเกี่ยวกับเรื่องนี้ในตอนนี้เว้นแต่จะตระหนักว่ามันเกี่ยวข้องกับบางส่วนของโมดูลคุณสามารถคิดว่าตัวแปรของไฟล์เป็นข้อมูลอ้างอิง โมดูลที่มีฟังก์ชันที่ทำงานบนไฟล์และที่เรานำเข้าโดยอัตโนมัติทุกครั้งที่เราสร้างตัวแปรชนิดของไฟล์หมายเหตุ 4 เราปิดไฟล์ที่ท้ายด้วยวิธีปิดใน Python ไฟล์จะถูกปิดโดยอัตโนมัติเมื่อสิ้นสุดโปรแกรม แต่ เป็นวิธีปฏิบัติที่ดีที่จะเข้าสู่นิสัยของการปิดไฟล์ของคุณอย่างชัดเจนทำไมดีระบบปฏิบัติการอาจไม่เขียนข้อมูลออกไปยังแฟ้มจนกว่าจะปิดนี้สามารถเพิ่มประสิทธิภาพสิ่งที่หมายถึงนี้ก็คือถ้า โปรแกรมออกมาโดยไม่คาดคิดมีอันตรายที่ข้อมูลอันมีค่าของคุณอาจไม่ได้ถูกเขียนลงในแฟ้มดังนั้นจริยธรรมเป็นเมื่อคุณเขียนเสร็จแล้วไปที่ไฟล์ปิดมันหมายเหตุ 5 เราไม่ได้ระบุเส้นทางแบบเต็มไปยังแฟ้มในรหัส ข้างต้นดังนั้นไฟล์จะถือว่าอยู่ในโฟลเดอร์ปัจจุบันอย่างไรก็ตามเราสามารถส่งชื่อเส้นทางแบบเต็มไปยังไฟล์หรือเปิดฟังก์ชันแทนที่จะเป็นเพียงชื่อไฟล์มีริ้วรอยเมื่อใช้ Windows อย่างไรก็ตามเนื่องจากอักขระที่ใช้ในการแยกโฟลเดอร์ใน เส้นทางของ Windows มีความหมายพิเศษภายในสตริง Python ดังนั้นเมื่อระบุเส้นทางใน Python จะเป็นการดีที่สุดในการใช้อักขระแทนและจะทำงานบนระบบปฏิบัติการใดก็ได้รวมถึง Windows. Now ให้พิจารณาว่าคุณจะจัดการกับไฟล์ที่มีความยาวได้อย่างไรก่อนอื่นคุณ จะต้องอ่านไฟล์ทีละบรรทัดใน Python โดยใช้ readline และ while loop แทน readlines และ for loop จากนั้นคุณอาจใช้ตัวแปร linecount ซึ่งเพิ่มขึ้นสำหรับแต่ละบรรทัดแล้วทดสอบเพื่อดูว่ามีอยู่หรือไม่ เท่ากับ 25 สำหรับหน้าจอ 25 บรรทัดถ้าใช่คุณขอให้ผู้ใช้กดคีย์ป้อนพูดก่อนที่จะรีเซ็ต linecount เป็นศูนย์และดำเนินการต่อคุณอาจต้องการลองเป็นแบบฝึกหัดตั้งแต่ Python version 2 2 ก็ยังเป็นไปได้ รักษาไฟล์เป็นรายการเพื่อให้คุณ don t ต้องใช้ readlines ภายในวงสำหรับคุณเพียงย้ำกว่าไฟล์ Let s เขียนตัวอย่างก่อนหน้านี้เพื่อดูคุณลักษณะนี้ในการดำเนินการลักษณะนี้ยังมีข้อได้เปรียบที่ไม่มีข้อ จำกัด ของ หน่วยความจำเช่นเดียวกับ readlines ดังนั้นจึงรวมข้อดีของการห่วงและในขณะที่วิธีการ readline ดังกล่าวข้างต้นจริงๆที่มีทั้งหมดที่มีให้คุณเปิดไฟล์อ่านและจัดการกับมันในแบบที่คุณต้องการใด ๆ เมื่อคุณเสร็จสิ้นคุณ ปิดไฟล์ แต่มีหนึ่ง niggle เล็กน้อยที่คุณอาจสังเกตเห็นในตัวอย่างก่อนหน้าบรรทัดอ่านจากแฟ้มมีอักขระ newline ที่สิ้นสุดเพื่อให้คุณลมกับบรรทัดว่างโดยใช้พิมพ์ที่เพิ่มบรรทัดใหม่ของตัวเองเพื่อหลีกเลี่ยง Python ให้ วิธีสตริงที่เรียกว่า s trip ซึ่งจะลบ whitespace หรือตัวอักษรที่ไม่สามารถพิมพ์ได้จากปลายทั้งสองของสตริงมีญาติที่สามารถตัดปลายด้านหนึ่งเรียกว่า rstrip และ lstrip เกินไปถ้าเราเปลี่ยนบรรทัดพิมพ์ข้างต้นด้วยทุกอย่างตอนนี้ควรทำงานได้ดีเพื่อสร้าง คัดลอกคำสั่งใน Python เราเพียงแค่เปิดไฟล์ใหม่ในโหมดเขียนและเขียนบรรทัดไปยังแฟ้มนั้นแทนการพิมพ์พวกเขาเช่นนี้คุณสังเกตเห็นว่าฉันเพิ่มคำสั่งพิมพ์ที่ท้ายเพียงเพื่อสร้างความมั่นใจให้กับผู้ใช้ว่าเกิดอะไรขึ้นจริง ชนิดของข้อเสนอแนะของผู้ใช้นี้มักจะเป็นความคิดที่ดีเพราะเราเขียนออกบรรทัดเดียวกับที่เราอ่านในมีปัญหากับตัวใหม่ที่นี่ แต่ถ้าเราได้เขียนออกสตริงที่เราสร้างขึ้นหรือที่เรามีแถบก่อนหน้านี้เรา จะต้องเพิ่มบรรทัดใหม่ในตอนท้ายของสตริงเอาต์พุตเช่นนี้ลองดูที่วิธีการที่เราอาจรวมไว้ในโปรแกรมสำเนาของเราแทนการคัดลอกเมนูเราจะเพิ่มวันที่วันนี้ไปที่ด้านบนด้วยวิธีนี้เราสามารถได้อย่างง่ายดาย สร้างดา เมนู ily จากแฟ้มข้อความที่แก้ไขได้ง่ายของมื้ออาหารทั้งหมดที่เราต้องทำคือเขียนออกคู่ของเส้นที่ด้านบนของไฟล์ใหม่ก่อนที่จะคัดลอกไฟล์เช่นนี้โปรดทราบว่าเราใช้โมดูลเวลาเพื่อให้ได้วันนี้และแปลง มันเป็น tuple ของค่าที่ใช้แล้วโดยการผลิตสตริงที่เมื่อแทรกลงในข้อความชื่อโดยใช้การจัดรูปแบบสตริงดูเหมือนว่าแม้ว่าเราจะเพิ่มสอง n ตัวอักษรที่ท้ายมีเพียงหนึ่งบรรทัดว่างพิมพ์เป็นเพราะ หนึ่งในนั้นเป็นบรรทัดใหม่ในตอนท้ายของชื่อตัวเองการจัดการการสร้างและการลบอักขระ newline เป็นหนึ่งในลักษณะที่น่ารำคาญมากขึ้นในการจัดการไฟล์ข้อความบางระบบปฏิบัติการ Gotchas ระบบปฏิบัติการจะจัดการไฟล์ด้วยวิธีที่ต่างกัน โปรแกรมของเราถ้าเราต้องการให้ทำงานในหลายระบบปฏิบัติการมีสอง niggles โดยเฉพาะที่สามารถจับคนออกและเราจะมองไปที่พวกเขาที่นี่เรื่องทั้งหมดของ newlines และไฟล์ข้อความเป็นพื้นที่มืดของ im มาตรฐานไม่ ความแตกต่างเหล่านี้มีรากมาจากวันแรกของการสื่อสารข้อมูลและการควบคุมเครื่องโทรสารเชิงกลโดยพื้นฐานแล้วมี 3 วิธีที่แตกต่างกันในการระบุบรรทัดใหม่ Carriage Return อักขระ CR rA Line Feed ตัวอักษร LF nA CR LF คู่ r n. All สามเทคนิคที่ใช้ในระบบปฏิบัติการที่แตกต่างกัน MS DOS และดังนั้น Windows ใช้วิธีที่ 3 Unix รวมถึง Linux ใช้วิธีที่ 2 Apple ใน MacOS เดิมใช้วิธีที่ 1 แต่ตอนนี้ใช้วิธีที่ 2 เนื่องจาก MacOS X เป็นตัวแปรของ Unix ดังนั้นวิธี โปรแกรมเมอร์ที่ยากจนสามารถรับมือกับความหลากหลายของเส้นตอนท้ายได้ในหลาย ๆ ภาษาเธอต้องทำแบบทดสอบจำนวนมากและใช้การดำเนินการที่แตกต่างกันต่อ OS ในภาษาที่ทันสมัยมากขึ้นรวมถึง Python ภาษานี้มีสิ่งอำนวยความสะดวกในการจัดการกับระเบียบสำหรับคุณในกรณีนี้ ของ Python ความช่วยเหลือมาในรูปแบบของ os โมดูลที่กำหนดตัวแปรที่เรียกว่า linesep ซึ่งตั้งค่าให้สิ่งที่ตัวละครใหม่อยู่ใน operatin ปัจจุบัน g ทำให้การเพิ่มบรรทัดใหม่ง่ายขึ้นและ rstrip ใช้บัญชีของระบบปฏิบัติการเมื่อไม่ทำงานของการลบพวกเขาดังนั้นจริงๆวิธีง่ายๆในการรักษาสติตราบใดที่ newlines มีความกังวลอยู่เสมอใช้ rstrip เพื่อลบ newlines จากบรรทัดอ่านจาก ไฟล์และมักจะเพิ่มสตริงที่ถูกเขียนลงในแฟ้มซึ่งยังคงออกจากสถานการณ์ที่น่าอึดอัดใจที่ไฟล์ถูกสร้างขึ้นในระบบปฏิบัติการหนึ่งแล้วประมวลผลในอื่นไม่เข้ากันได้ OS และเศร้ามีไม่มากที่เราสามารถทำได้เกี่ยวกับที่ยกเว้น เปรียบเทียบจุดสิ้นสุดของบรรทัดด้วยเพื่อกำหนดว่าข้อแตกต่างคืออะไรการระบุเส้นทางเป็นปัญหาสำหรับผู้ใช้ Windows มากกว่าคนอื่น ๆ แม้ว่าผู้ใช้ MacOS 9 อาจชนกันเป็นครั้งคราวด้วยเช่นกันแต่ละระบบปฏิบัติการระบุเส้นทางไปยังไฟล์โดยใช้อักขระที่ต่างกันไป แยกไดรฟ์โฟลเดอร์และไฟล์วิธีแก้ปัญหาโดยทั่วไปสำหรับเรื่องนี้คือการใช้โมดูล os ซึ่งมีตัวแปรเพื่อกำหนดเส้นทางของตัวคั่นระหว่างแพลตฟอร์มปัจจุบันในทางปฏิบัติคุณไม่จำเป็นต้องใช้ตัวเลือกนี้บ่อยนักตั้งแต่ th เส้นทาง e อาจจะแตกต่างกันสำหรับเครื่องทุก ๆ ต่อไปดังนั้นแทนที่จะคุณเพียงแค่จะใส่เส้นทางแบบเต็มโดยตรงในสตริงอาจจะเป็นครั้งเดียวสำหรับแต่ละระบบปฏิบัติการที่คุณกำลังทำงานอยู่ แต่มีหนึ่งซ่อน Gotcha รอผู้ใช้ Windows คุณเห็นใน ส่วนก่อนหน้านี้ที่ python ถือว่าสตริง n เป็นอักขระ newline ซึ่งใช้เวลาสองตัวอักษรและถือว่าเป็นหนึ่งในความเป็นจริงมีทั้งช่วงของลำดับพิเศษเหล่านี้เริ่มต้นด้วยเครื่องหมายสแลชรวมทั้ง r - การกลับรถ t - แท็บแนวนอน v - แท็บแนวตั้ง b - แบ็กอัพ 0nn - อักขระรหัสฐานแปดตัวใด ๆ เช่นรหัส 033 คืออักขระ Escape ESC ซึ่งหมายความว่าถ้าเรามีไฟล์ข้อมูลที่เรียกและต้องการเปิดใน Python โดยระบุเส้นทาง Windows แบบเต็มเราจะคาดหวังให้ทำงานได้ แต่ Python จะ ดูคู่ t เป็นตัวอักษรแท็บและบ่นไม่สามารถหาไฟล์ที่เรียกว่า C ดังนั้นเราจะได้รับรอบความไม่สะดวกนี้มีสามโซลูชั่น. put r ในด้านหน้าของสตริงนี้จะบอก Python ที่จะละเว้น slashes กลับใด ๆ และรักษามันเป็น sting. Use ดิบไปข้างหน้า slashes แทน backslashes, Python และ Windows จะระหว่างพวกเขาจัดเรียงออกเส้นทางสำหรับคุณนี้มีประโยชน์เพิ่มเติมของการทำให้โค้ดของคุณแบบพกพาไปยังระบบปฏิบัติการอื่น ๆ เกินไปใช้เครื่องหมายทับขวาคู่ตั้งแต่อักขระเครื่องหมายทับขวาสองครั้งจะเห็น โดย Python เป็นเครื่องหมายทับแบ็กเดียวหากมีข้อใดต่อไปนี้จะเปิดไฟล์ข้อมูลของเราอย่างถูกต้องขั้นตอนสุดท้ายในการประมวลผลไฟล์คือคุณอาจต้องเพิ่มข้อมูลลงในตอนท้ายของไฟล์ที่มีอยู่วิธีหนึ่งที่จะทำคือเปิด ไฟล์สำหรับ i nput อ่านข้อมูลลงในรายการผนวกข้อมูลลงในรายการแล้วเขียนรายการทั้งหมดออกไปเป็นเวอร์ชันใหม่ของไฟล์เก่าถ้าไฟล์สั้นที่ไม่เป็นปัญหา แต่ถ้าไฟล์มีขนาดใหญ่มากอาจมากกว่า 100Mb แล้วคุณก็จะทำงานออกจากหน่วยความจำที่จะถือรายการโชคดีที่มีโหมดอื่นที่เราสามารถผ่านการเปิดซึ่งจะช่วยให้เราสามารถผนวกโดยตรงไปยังไฟล์ที่มีอยู่เพียงแค่เขียนดียิ่งขึ้นหากไฟล์ doesn t อยู่มันจะ เปิดไฟล์ใหม่เช่นเดียวกับถ้าคุณระบุ w ตัวอย่างเช่นสมมุติว่าเรามีไฟล์บันทึกที่เราใช้เพื่อจับข้อผิดพลาดเราไม่ต้องการลบข้อความที่มีอยู่เพื่อให้เราเลือกที่จะเพิ่มข้อผิดพลาดเช่นนี้ ในโลกแห่งความเป็นจริงเราอาจต้องการ จำกัด ขนาดของไฟล์ในบางวิธีเทคนิคทั่วไปคือการสร้างชื่อไฟล์ตามวันที่ดังนั้นเมื่อวันที่เปลี่ยนแปลงเราจะสร้างไฟล์ใหม่โดยอัตโนมัติและเป็นเรื่องง่ายสำหรับผู้ดูแล ของระบบเพื่อหาข้อผิดพลาดสำหรับวันใดวันหนึ่งและเพื่อเก็บถาวรข้อมูลเก่า r ไฟล์ถ้าไม่จำเป็นจำจากตัวอย่างเมนูด้านบนที่โมดูลเวลาสามารถใช้เพื่อหาวันที่ปัจจุบันสมุดรายชื่อ Revisited. You จำโปรแกรมสมุดที่อยู่ที่เราแนะนำในหัวข้อวัตถุดิบและขยายแล้ว ในการพูดคุยกับหัวข้อผู้ใช้ Let s เริ่มต้นเพื่อให้มีประโยชน์จริงๆโดยการบันทึกลงในไฟล์และแน่นอนอ่านไฟล์เมื่อเริ่มต้นเราจะทำเช่นนี้โดยการเขียนบางฟังก์ชันดังนั้นในตัวอย่างนี้เราดึงกันหลายเส้น ที่เราได้ครอบคลุมในช่วงไม่กี่หัวข้อการออกแบบขั้นพื้นฐานจะต้องมีฟังก์ชั่นการอ่านไฟล์เมื่อเริ่มต้นอีกหนึ่งไฟล์ที่จะเขียนในตอนท้ายของโปรแกรมนอกจากนี้เรายังจะสร้างฟังก์ชันเพื่อนำเสนอผู้ใช้ด้วยเมนูตัวเลือก และฟังก์ชั่นแยกต่างหากสำหรับการเลือกเมนูแต่ละเมนูจะช่วยให้ผู้ใช้เพิ่มรายการไปยังสมุดที่อยู่ลบรายการจาก book. Find และแสดงรายการที่มีอยู่ให้คำแนะนำการใช้งานที่อยู่ใน Book. Notice การใช้งาน ของ rstrip เพื่อเอาเส้นใหม่ e จากท้ายบรรทัดนอกจากนี้ยังสังเกตเห็นการดำเนินงานต่อไปเพื่อดึงข้อมูลบรรทัดถัดไปจากไฟล์ภายในลูปในที่สุดสังเกตว่าเรากำหนดชื่อไฟล์เป็นตัวแปรระดับโมดูลเพื่อให้เราสามารถใช้งานได้ทั้งในการโหลดและบันทึกข้อมูล Address Book. Notice เราต้องเพิ่มอักขระ newline n เมื่อเราเขียนข้อมูลการเรียกผู้ใช้ Input. Adding Entry. Removing entry. Finding entry. Quitting program. Actually ฉันได้รับรางวัล t เขียนฟังก์ชันแยกต่างหากสำหรับนี้, แทนฉันจะทำให้ตัวเลือกออกจากการทดสอบในเมนูของฉันในขณะที่ห่วงดังนั้นโปรแกรมหลักจะมีลักษณะเช่นนี้ตอนนี้สิ่งเดียวที่เหลือจะทำคือการเรียกใช้ฟังก์ชันหลักเมื่อโปรแกรมทำงานและการทำเช่นนั้นเราใช้บิตของ เวทมนตร์ Python เช่นนี้บิตลึกลับของรหัสช่วยให้เราสามารถใช้ไฟล์หลามใด ๆ เป็นโมดูลโดยนำเข้าไอเอ็นจีหรือเป็นโปรแกรมโดยใช้มันความแตกต่างก็คือเมื่อโปรแกรมถูกนำเข้าชื่อตัวแปรภายในถูกตั้งค่าเป็น ชื่อโมดูล แต่เมื่อไฟล์ถูกเรียกใช้ค่าของชื่อถูกตั้งไว้ที่ หลักส่อเสียด eh. Now ถ้าคุณพิมพ์รหัสทั้งหมดลงในแฟ้มข้อความใหม่และบันทึกเป็นคุณควรจะสามารถเรียกใช้จากพรอมต์ระบบปฏิบัติการโดยการพิมพ์หรือเพียงแค่ดับเบิลคลิกที่ไฟล์ใน Windows Explorer และควรเริ่มต้นใน หน้าต่าง DOS ของตัวเองและหน้าต่างจะปิดเมื่อคุณเลือกตัวเลือกออกจากการทดสอบโค้ดดูว่าคุณสามารถหาข้อผิดพลาดที่ฉันได้ทิ้งไว้อย่างน้อยสองข้อผิดพลาดเล็กน้อยสำหรับคุณที่จะหาอาจมีมากขึ้นและพยายามที่จะ แก้ไขโปรแกรมนี้ 60 สายแปลกเป็นปกติของการเรียงลำดับของสิ่งที่คุณสามารถเริ่มต้นเขียนด้วยตัวคุณเองมีสองสิ่งที่เราสามารถทำได้เพื่อปรับปรุงที่ฉันจะครอบคลุมในส่วนถัดไป แต่แม้ในขณะที่มันยืนมันเป็นประโยชน์อย่างสมเหตุสมผล little. VBScript และ JavaScript. No ไม่ว่า VBScript หรือ JavaScript จะมีความสามารถในการจัดการไฟล์แบบเนทีฟนี่คือคุณลักษณะด้านการรักษาความปลอดภัยเพื่อให้แน่ใจว่าไม่มีใครสามารถอ่านไฟล์ของคุณได้เมื่อคุณโหลดหน้าเว็บอย่างอ่อนโยน แต่จะ จำกัด ประโยชน์ แต่อย่างใดอย่างไรก็ตามเราสามารถนำมาใช้ซ้ำได้ โมดูลมีวิธีการทำโดยใช้ Windows Scr ipt Host WSH ให้ FileSystem object ซึ่งอนุญาตให้ใช้ภาษา WSH อ่านไฟล์ได้เราจะดูตัวอย่าง JavaScript ในรายละเอียดแล้วแสดงโค้ดที่คล้ายกันใน VBScript เพื่อเปรียบเทียบ แต่ก่อนที่องค์ประกอบสำคัญจะเรียกใช้วัตถุ WScript ก่อนหน้าเรา สามารถดูรหัสในรายละเอียดได้โดยใช้เวลาในการอธิบาย FileSystem Object Model Model Object คือชุดของอ็อบเจ็กต์ที่เกี่ยวข้องซึ่งสามารถใช้โดยโปรแกรมเมอร์รูปแบบวัตถุ WSH FileSystem ประกอบด้วยอ็อบเจ็กต์ FSO จำนวนออบเจ็กต์ไฟล์ รวมถึงวัตถุ TextFile ที่เราจะใช้นอกจากนี้ยังมีวัตถุผู้ช่วยเหลือบางส่วนที่โดดเด่นที่สุดคือเพื่อวัตถุประสงค์ของเรา TextStream object โดยทั่วไปเราจะสร้างอินสแตนซ์ของอ็อบเจ็กต์ FSO จากนั้นใช้เพื่อสร้างวัตถุ TextFile และจาก เหล่านี้ในการเปิดสร้างวัตถุ TextStream ที่เราสามารถอ่านหรือเขียนข้อความวัตถุ TextStream ตัวเองเป็นสิ่งที่เราจริงอ่านเขียนจากไฟล์พิมพ์รหัสต่อไปนี้ลงในไฟล์ที่เรียกว่าและเรียกใช้ usi cscript ดังที่อธิบายไว้ในบทนำก่อนหน้า WSH การเปิดไฟล์หากต้องการเปิดไฟล์ใน WSH เราจะสร้างวัตถุ FSO จากนั้นสร้างวัตถุ TextFile จากที่อ่านและเขียนไฟล์การเก็บไฟล์และใน VBScript บันทึกต่อไปนี้ as แล้วเรียกใช้โดยใช้หรืออีกวิธีหนึ่งคือใส่บิตระหว่างแท็กสคริปต์ลงในไฟล์ที่เรียกและเรียกใช้แทนซึ่งรูปแบบนี้จะช่วยให้คุณสามารถผสมโค้ด JavaScript และ VBScript ในไฟล์เดียวกันได้โดยใช้แท็กสคริปต์หลาย ๆ อันที่คุณต้องการ การจัดการกับข้อความที่ไม่ใช่ข้อความการยกข้อความเป็นหนึ่งในสิ่งที่พบบ่อยที่สุดที่โปรแกรมเมอร์ทำ แต่บางครั้งเราจำเป็นต้องประมวลผลข้อมูลไบนารีดิบเกินไปนี่เป็นสิ่งที่ไม่ค่อยเกิดขึ้นใน VBScript หรือ JavaScript ดังนั้นฉันจะครอบคลุมเฉพาะว่างูหลามทำอย่างไร การเปิดและการปิดไฟล์ไบนารีความแตกต่างที่สำคัญระหว่างไฟล์ข้อความและไฟล์ไบนารีคือไฟล์ข้อความประกอบด้วย octets หรือไบต์ข้อมูลไบนารีโดยแต่ละไบต์จะแสดงถึงอักขระและส่วนท้ายของไฟล์จะถูกทำเครื่องหมายด้วยรูปแบบไบต์พิเศษซึ่งรู้จักกันดี generica lly เป็นจุดสิ้นสุดของแฟ้มหรือ eof ไฟล์ไบนารีมีข้อมูลไบนารีโดยพลการและไม่มีค่าที่ระบุสามารถใช้เพื่อระบุจุดสิ้นสุดของไฟล์จึงต้องใช้โหมดอื่นในการอ่านไฟล์เหล่านี้ผลลัพธ์สุดท้ายคือเมื่อเราเปิด ไฟล์ไบนารีในภาษาไพ ธ อนหรือภาษาอื่นใดที่เราต้องระบุว่ากำลังเปิดอยู่ในโหมดไบนารีหรืออาจทำให้ข้อมูลถูกอ่านถูกตัดทอนที่อักขระ eof ตัวแรกที่ Python พบในข้อมูลวิธีที่เราทำใน Python คือการเพิ่ม ab ไปยังพารามิเตอร์โหมดเช่นนี้แตกต่างจากการเปิดไฟล์ข้อความเท่านั้นคือค่าโหมดของ rb คุณสามารถใช้โหมดอื่นใดก็ได้เพียงแค่เพิ่ม AB wb เพื่อเขียน, AB เพื่อผนวกการลบไฟล์ไบนารีคือไม่มี different to a text file, simply call the close method of the open file object. Because the file was opened in binary mode there is no need to give Python any extra information, it knows how to close the file correctly. Data Representation and Storage. Before we discuss how to access the data within a binary file we need to consider how data is represented and stored on a computer All data is stored as a sequence of bi nary digi t s, or bits These bits are grouped into sets of 8 or 16 called bytes or words respectively A group of 4 is sometimes called a nibble A byte can be any one of 256 different bit patterns and these are given the values 0-255.The information we manipulate in our programs, strings, numbers etc must all be converted into sequences of bytes Thus the characters that we use in strings are each allocated a particular byte pattern There were originally several such encodings but the most common is the ASCII American Standard Coding for Information Interchange Unfortunately pure ASCII only caters for 128 values which is not enough for non English languages A new encoding standard known as Unicode has been produced, which can use data words instead of bytes to represent characters, and allows for over 65000 characters A subset of Unicode called UTF-8 correspond s closely to the earlier ASCII coding such that every valid ASCII file is a valid UTF-8 file, although not necessarily the other way around Python by default supports ASCII and by pre-pending a u in front of a string we can tell Python to treat the string as Unicode Although we also need to tell Python which encoding is in use or it will be confused. In the same way numbers need to be converted to binary codings too For small integers it is simple enough to use the byte values directly, but for numbers larger than 255 or negative numbers, or fractions some additional work needs to be done Over time various standard codings have emerged for numerical data and most programming languages and operating systems use these For example, the American Institute of Electrical and Electronic Engineering IEEE have defined a number of codings for floating point numbers. The point of all of this is that when we read a binary file we have to interpret the raw bit patterns into the correct type of data f or our program It is perfectly possible to interpret a stream of bytes that were originally written as a character string as a set of floating point numbers Or course the original meaning will have been lost but the bit patterns could represent either So when we read binary data it is extremely important that we convert it into the correct data type. The Struct Module. To encode decode binary data Python provides a module called struct short for structure struct works very much like the format strings we have been using to print mixed data We provide a string representing the data we are reading and apply it to the byte stream that we are trying to interpret We can also use struct to convert a set of data to a byte stream for writing, either to a binary file or even a communications line. There are many different conversion format codes but we will only use the integer and string codes here You can look up the others on the Python documentation for the struct module The codes for integer and string are i and s respectively The struct format strings consist of sequences of codes with numbers pre-pended to indicate how many of the items we need The exception is the s code where the prepended number means the length of the string For example 4s means a string of four characters note 4 characters not 4 strings. Let s assume we wanted to write the address details, from our Address Book program above, as binary data with the street number as an integer and the rest as a string This is a bad idea in practice since street numbers sometimes include letters The format string would look like. To cope with multiple address lengths we could write a function to create the binary string like this. So we used a string method - split - more on them in the next topic to split the address string into its parts, extract the first one as the number and then use another string method, join to join the remaining fields back together The length of that string is the number we need in the struct format string Phew. formatAddress will return a sequence of bytes containing the binary representation of our address Now that we have our binary data let s see how we can write that to a binary file and then read it back again. Reading Writing Using Struct. Let s create a binary file containing a single address line using the formatAddress function defined above We need to open the file for writing in wb mode, encode the data, write it to the file and then close the file Let s try it. You can check that the data is indeed in binary format by opening in notepad The characters will be readable but the number will not look like 10 In fact it has dissappeared If you have an editor which can read binary files e g vim or emacs and use that to open you will see that the start of the file has 4 bytes The first of these may look like a newline character and the rest are zeros Now it turns out that, just coincidentally, the numerical value of newline is 10 As we can show using Python. The ord functi on simply returns the ASCII value of a given character So the first 4 bytes are 10,0,0,0 in decimal or 0xA,0x0,0x0,0x0 in hexadecimal. On a 32 bit computer an integer takes up 4 bytes So the integer value 10 has been converted by the struct module into the 4 byte binary sequence 10,0,0,0 Now on intel micro-processors the byte sequence is to put the least significant byte first so that, reading it in reverse, gives us the true binary value 0,0,0,10.Which is the integer value 10 expressed as 4 decimal bytes The rest of the data is basically the original text string and so appears in its normal character format. Be sure not to save the file from within Notepad since although Notepad can load some binary files it cannot save them as binary, it will try to convert the binary to text and can corrupt the data in the process It is worth pointing out here that the file extension that we used is purely for our convenience, it has no bearing on whether the file is binary or text format Some Operati ng Systems use the extension to determine what programme they will use to open the file, but you can change the extension by simply renaming the file, the content will not change it will still be binary or text whichever it was originally You can prove this by renaming a text file in Windows to whereupon Windows will treat the file as an executable, but when you try to run it you will get an error because the text is not really executable binary code If you now rename it back to the file will open in Notepad exactly as it did before, the content has not been altered at all - In fact you could even have opened the text in Notepad while it was named as a and it would have worked just as well. To read our binary data back again we need to open the file in rb mode, read the data into a sequence of bytes, close the file and finally unpack the data using a struct format string The question is how do we tell what the format string looks like In general we would need to find the binary format f rom the file definition there are several web sites which provide this informaton - for example Adobe publish the definition of their common PDF binary format In our case we know it must be like the one we created in formatAddress namely iNs where N is a variable number How do we determine the value of N. The struct module provides some helper functions that return the size of each data type, so by firing up the Python prompt and experimenting we can find out how many bytes of data we will get back for each data type. Ok, we know that our data will comprise 4 bytes for the number and one byte for each character So N will be the length of the data minus 4 Let s try using that to read our file. And that s it on binary data files, or at least as much as I m going to say on the subject As you can see using binary data introduces several complications and unless you have a very good reason I don t recommend it But at least if you do need to read a binary file, you can do it provided you know w hat the data represented in the first place of course. Random Access to Files. The last aspect of file handling that I ll consider is called random access Random access means moving directly to a particular part of the file without reading all the intervening data Some programming languages provide a special indexed file type that can do this very quickly but in most languages its built on top of the normal sequential file access that we have been using up till now. The concept used is that of a cursor that marks the current position within the file, literally how many bytes we are from the beginning We can move this cursor relative to its current position or relative to the start of the file We can also ask the file to tell us where the cursor is currently. By using a fixed linelength perhaps by padding our data strings with spaces or some other character where necessary we can jump to the start of a particular line by multiplying the length of a line by the number of lines This is what g ives the impression of random access to the data in the file. To determine where we are in a file we can use the tell method of a file For example if I open a file and read three lines, I can then ask the file how far into the file I am. Let s look at an example, first I will create a file with 5 lines of text all the same length the equal length business isn t strictly necessary but it does make life easier Then I ll read three lines back and ask where we are I ll then go back to the beginning, read one line then jump to the third line and print it, jumping over the second line Like this. Note the use of the seek function to move the cursor The default operation is to move it to the byte number specified, as shown here, but extra arguments can be provided that change the indexing method used Also note that the value printed by the first tell depends on the length of a newline on your platform, on my Windows XP PC it printed 66 indicating that the newline sequence is 2 bytes long 66 3-20 But since this is a platform specific value and I want to make my code portable I ve used tell again, after reading one line, to work out how long each line really is These kind of cunning ploys are often necessary when dealing with platform specific issues. Some new access modes. Things to remember. Open files before using them. Files can usually only be read or written but not both at the same time. Python s readlines function reads all the lines in a file, while readline only reads one line at a time, which may help save memory. However in v2 4 onwards you don t usually need to use either since Python s file open functions work with for loops. Close files after use. Binary files need the mode flag to end in b. tell and seek enable pseudo-random access to sequential files. If you have any questions or feedback on this page send me mail at. In an earlier tutorial we talked about file I O functions and the use of text files In this C programming tutorial we are going to talk about the use of bina ry files. Binary files. Binary files are very similar to arrays of structures, except the structures are in a disk-file rather than an array in memory Binary files have two features that distinguish them from text files. You can instantly use any structure in the file. You can change the contents of a structure anywhere in the file. After you have opened the binary file, you can read and write a structure or seek a specific position in the file A file position indicator points to record 0 when the file is opened. A read operation reads the structure where the file position indicator is pointing to After reading the structure the pointer is moved to point at the next structure. A write operation will write to the currently pointed-to structure After the write operation the file position indicator is moved to point at the next structure. The fseek function will move the file position indicator to the record that is requested. Remember that you keep track of things, because the file position indic ator can not only point at the beginning of a structure, but can also point to any byte in the file. The fread and fwrite function takes four parameters. A memory address. Number of bytes to read per block. Number of blocks to read. A file variable. This fread statement says to read x bytes size of rec from the file ptrmyfile into memory address myrecord Only one block is requested Changing the one into ten will read in ten blocks of x bytes at once. Let s look at a write example. In this example we declare a structure rec with the members x, y and z of the type integer In the main function we open fopen a file for writing w Then we check if the file is open, if not, an error message is displayed and we exit the program In the for loop we fill the structure member x with a number Then we write the record to the file We do this ten times, thus creating ten records After writing the ten records, we will close the file don t forget this. So now we have written to a file, let s read from the file we have just created Take a look at the example. The only two lines that are changed are the two lines in the for loop With the fread we read-in the records one by one After we have read the record we print the member x of that record. The only thing we need to explain is the fseek option The function fseek must be declared like this. The fseek function sets the file position indicator for the stream pointed to by the stream The new position, measured in characters from the beginning of the file, is obtained by adding offset to the position specified by whence Three macros are declared in stdio h called SEEKSET, SEEKCUR and SEEKEND. If the position declared by whence is SEEKSET, then the position is the beginning of the file. The SEEKEND can be used if you want to go to the end of the file Using negative numbers it is possible to move from the end of the file. If whence is SEEKCUR then the position is set, x bytes, from the current position. Let s take a look at an example. In this example we ar e using fseek to seek the last record in the file This record we read with fread statement and with the printf statement we print member x of the structure myrecord As you can see the for loop also changed The for loop will now countdown to zero This counter is then used in the fseek statement to set the file pointer at the desired record The result is that we read-in the records in the reverse order. A last note if you set the file position indicator to a position in a file and you want the first position in a file then you can use the function rewind to the first position in the file The function rewind can be used like this. With the fseek statement in this example we go to the end of the file Then we rewind to first position in the file Then read-in all records and print the value of member x Without the rewind you will get garbage Try it. That is all for this tutorial. This entry was posted in C Tutorials You can follow any responses to this entry through the RSS 2 0 feed Both comment s and pings are currently closed Tweet This or use to share this post with others. There are currently 32 responses to C Tutorial Binary File I O. Why not let us know what you think by adding your own comment. pankaj on February 22nd, 2010.this is outstanding succint tutorial for file handling helps a lotttttt thank you. betagtx260 on March 25th, 2010.Hi, i have modified the write section for can write just 1 byte, but it don t work. Our structure struct rec int mydata. int counter FILE ptrmyfile struct rec myrecord. ptrmyfile fopen , w if ptrmyfile printf Unable to open file return 1 for counter 1 counter 5 counter counter fwrite myrecord, sizeof struct rec , 1, ptrmyfile fwrite myrecord, sizeof struct rec , 1,ptrmyfile fclose ptrmyfile return 0.in my i read 01 00 00 00 in my hexadecimal editor my question is how can don t write the triple 00 00 00 after i have put my 01 in file because in next while i will get 01 00 00 00 02 00 00 00.Tanks for your help in advance, and sorry for my english im french. betagtx260 If I understand your question correctly you want to see only 1byte in the binary file If this is the case you are using the wrong variable type int in this case , because an unsigned int can hold all the values between 0 and UINTMAX inclusive UINTMAX must be at least 65535 The int types must contain at least 16 bits to hold the required range of values But it also can vary per compiler and the platform you are compiling for. On compilers for 8 and 16 bit processors including Intel x86 processors executing in 16 bit mode, such as under MS-DOS , an int is usually 16 bits and has exactly the same representation as a short On compilers for 32 bit and larger processors including Intel x86 processors executing in 32 bit mode, such as Win32 or Linux an int is usually 32 bits long and has exactly the same representation as a long A int holds 32 bits thus you see 01 00 00 00 in your hex-editor. Try the following example to see the sizeof variable types for your target platform include stdio h. int main printf d d n , sizeof int , sizeof short int printf d d n , sizeof signed int , sizeof unsigned int printf d d n , sizeof char , sizeof unsigned char return 0.The output will be something like this on a linux intel machine 4 2 4 4 1 1.So if you want to see only one byte in your hex editor, change the program to this include stdio h. int main int counter FILE ptrmyfile struct rec myrecord. ptrmyfile fopen , w if ptrmyfile printf Unable to open file return 1. fwrite myrecord, sizeof struct rec , 1, ptrmyfile. fclose ptrmyfile return 0.If you open the file with the hex-editor you will only see 1byte 01 in this case if you compile for intel platform win32 and linux Or if you want to use int, accept that 4 bytes are written in the binary file but at least you know now why this is. Hope that this answers your question. betagtx260 on March 25th, 2010.Thanks for your fast reply, and i understand now, Tanks again. Hi, after piratical and try to understanding write and read, my write is perfect what i want, but i can not read my bin file, i need some help please. this is my write test file work 100.int main int i int counter FILE ptrmyfile struct rec myrecord. ptrmyfile fopen , wb if ptrmyfile printf Unable to open file return 1.for counter 0 counter 10 counter. i fseek ptrmyfile counter SEEKSET fwrite myrecord, sizeof struct rec , 1, ptrmyfile. it generate a file and in a hexadecimal editor you can read 01 02 03 04 05 06 07 08 etc. but i can not read the file with fread, i will appreciate your help again Thanks in advance. betagtx260 Below you will find a binary write example and a binary read example I have tested them and they work, so good luck Write the file include stdio h. struct rec char mydata. int main int i int counter FILE ptrmyfile struct rec myrecord. ptrmyfile fopen , wb if ptrmyfile printf Unable to open file return 1 i 0.for counter 0 counter 10 counter i fseek ptrmyfile counter SEEKSET fwrite myrecord, sizeof struct rec , 1, ptrmyfile i fclose ptrmyfile return 0. Read the file include stdio h. struct rec char mydata. int main int counter FILE ptrmyfile struct rec myrecord. ptrmyfile fopen , r if ptrmyfile printf Unable to open file return 1 for counter 1 counter 10 counter fread myrecord, sizeof struct rec ,1,ptrmyfile printf d n fclose ptrmyfile return 0.Hi, Tanks for your help and support, my last message with my binary writer test, is perfect for me, because i can write a hex value directly in file 00 to FF anywhere i want in file, but i cant read it with fread after, i want practi ce this with a small file i have create because my true application i want manage file, and i have no success with fread with iso file. Is this the right declaration of a binary file Binary files should have b in the mode e g w b or wb Am I right. selewa You are right, if you only look at the syntax But if you open with text editor, you will see all kinds of ascii values This is because we write read records structs So there will be now difference between the content of a file written with only w or wb But to keep the syntax correct, we have changed the source code examples The modes you can use are as follows r open for reading w open for writing file need not exist a open for appending file need not exist r open for reading and writing, start at beginning w open for reading and writing overwrite file a open for reading and writing append if file exists. To open a file in binary mode you must add the letter b to the end of the mode string for example, wb For the read and write modes, you can add the b, either after the plus sign r b or before rb. Good luck and thx for correcting us. Is there a possibility to deal with data packing of the compiler in the structure, because due to this structure members are not necessarily continuou s in the memory. For example, the size of a structure with a uint8 and uint32 variable in there is usually 8 bytes instead of 5 due to compiler data packing. Now, if you want to read the 5 bytes from your binary file using. you will read the wrong bytes in the fields the uint32 variable will only have one byte. Is there any method to prevent this. I ve seen people read 1 byte at a time, then if you need it to be one number just multiply it by it corresponding position. first number n 10000 second number n 1000 third number n 100 forth number n 10 fifth number n. I m not a professional so please research for better ways. In your last example you do the following. fseek ptrmyfile, sizeof struct rec , SEEKEND rewind ptrmyfile. And mention that you get garbage without the rewind If you seek to the end of file and then offset beyond the end of the file, you will be reading garbage, you need a negative offset. These 2 lines are pointless in your example Or am I mistaken. radix the goal of that example is to show how to use of the rewind function That s why the end of the file is searched otherwise there is nothing to rewind from and then rewind is called to go to first position of the file. You will get garbage if rewind isn t called because the for loop will try to read records beyond the end of the file, because there is no check performed to check if the end of the file has been reached. So again it s just to show the use of the rewind function nothing more, nothing less The search for the end of the file is only done, so we that we can rewind. I want to dump some memory into a file, a complex structure which contains pointers to list etc, and again want to set the same memory from this file. Is it possible to do with fwrite fread functions. Sandeep Kumar on February 26th, 2011.I want to store a character array data 89 bytes to a binary file Please send the fwrite function code for this Is it possible to store current PC time to same binary file. the firs t example in the web page only writes to variable x how to write and read using variable y and z. mohammed It s very easy to use variables y and z in the first write and read examples You just have to add some additional lines The examples now only uses the variable x to keep the examples smaller To use all three x, y,z variables add the following lines to the examples. Write for loop for counter 1 counter 10 counter myrecord x counter myrecord y counter myrecord z counter fwrite myrecord, sizeof struct rec , 1, ptrmyfile. The read example for counter 1 counter isaiah on August 4th, 2011.tnx for some example of the program tnx a lot power. This was an extremely helpful tutorial Thank you so much. I understand that fseek offsets the pointer by characters Is there anyway I can offset the pointer by n number of lines I m trying to write overwrite a string to a specific line in my text file I tried. but it does not offset my pointer to line 3 in my textfile Please enlighten me with your wisdom, great one. Thanks again Alan. C Tutorial Binary File I O CodingUnit Programming Tutorials Rate this Share this TwitterFacebookStumbleUponPrintEmailLinkedInLike this LikeBe the first to. kiran reddy on September 28th, 2012.very nicely explained thnXX. Hey everybody Thanks a lot for the kind help It really helped me to my aim what I was finding Thanks again Have happy programming. Thanks for writing this exactly what I needed to know for my project. Gabriel Sylvain on March 2nd, 2013.Amazing tutorial, very well explained Thanks. it is helps me a lot to understand binary you add another program of telling how to add and delete records at particular positions. Hello I am trying to read satellite CEOS format file which contacined all type of data like integer ascii, binary I know structure of binary file I am reading file according to structure but not able to display data Is it require after reading data structure-wise to conver binary to decimal format or can I use cout to display binary data directly. Nadeem khan on July 31s t, 2013.please tell me how can can i change the binary number into radix using string like binary number of 64 will be 1000000 binary 1000000 64 radix. Awesome tutorials very helpful Thanks a lottttt for this. I want to know how images are displayed using the C programming language. Amazing Tutorial, thanks. Papabear on February 3rd, 2014.Simply wonderful tutorial Thank you.

No comments:

Post a Comment