วันเสาร์ที่ 30 พฤษภาคม พ.ศ. 2563

การดำเนินการไฟล์ ติวเตอร์เรียล 12

Tutorial 12 – File Operations

ไซแลบสามารถดำเนินการไฟล์บนดิสค์  และฟังก์ชันนำมาใช้ได้ในการเปิด ปิด อ่านและเขียนไฟล์ในดิสค์  บรรทัดของโค๊ดต่อไปนี้แสดงให้เห็นว่าสามารถทำให้งานลุล่วงไปได้

-->n=10; x=25.5; xy=[100 75;0 75; 200, 0];
-->fd=mopen(“ex01.dat”, “w”);        // Opens a file ex01.dat for writing
-->mfprintf(fd, “n=%d, x=%f\n”, n, x);
-->mfprintf(fd, “%12.4f\t%12.4f\n”, xy);
-->mclose(fd);

ตอนนี้เราสามารถเปิดไฟล์ ex01.dat ในเท็กซ์เอดิเตอร์ เช่นโน้ตแพด และดูเนื้อหาได้ เราจะสังเกตว่าคำสั่งคล้ายคลึงสอดคล้องกับคำสั่งในภาษาin C, กล่าวคือ, fopen(), fprintf() และ fclose().

ให้สังเกตว่าคำสั่งที่สอง รูปแบบสตริงต้องเพียงพอที่จะพิมพ์ได้เต็มแถวของเมทริกซ์ xy,  ลำดับของการดำเนินการต้องเป็นไฟล์เสมอและได้มาซึ่งไฟล์อธิบาย ให้เขียนลงไฟล์โดยใช้ไฟล์อธิบาย และปิดไฟล์โดยใช้ไฟล์อธิบาย โหมดที่แตกต่างกันที่ซึ่งไฟล์หนึ่งสามารถถูกเปิดออกมา คือโหมด r สำหรับการอ่านจากไฟล์  ไฟล์นั้นต้องมีอยู่ ถ้าไฟล์น้นไม่มีอยู่ mopen  คืนกลับโค๊ดข้อผิดพลาด -2.   โหมด w For writing to the file. If the file exists, its contents will be erased and writing will begin from the beginning of the file. If the file does not exist, a new file will be created. a For appending to the file. If the file exists, it will be opened and writing will start from the end of the file. If the file does not exist, a new one will be created. The functions mprintf() and msprintf() are similar to mfprintf(), except that they send the output to the standard output stream and a designated string, respectively, instead of to a file. The statements to print the above data to the standard output (Scilab text window) are as follows: -->mprintf(“n=%d, x=%f\n”, n, x); -->mprintf(“%12.4f\t%12.4f\n”, xy); Being a standard file, it is not necessary to explicitly open and close the standard output file. To output the same data to the strings s1 and s2, use the following commands: -->s1 = msprintf(“n=%d, x=%f\n”, n, x); -->s2 = msprintf(“%12.4f\t%12.4f\n”, xy); Reading data from files into variables in the Scilab workspace is equally easy. The file will have to be opened in read mode and after values have been read from files, the file will have to be closed. The function to read data is mfscanf(). The following code fragment reads an integer and a floating point values from the first line in the data file and store them in the variables n and x, respectively. The second mfscanf() statement executed within the loop, reads the values in the subsequent lines into the variable a. Following is the code: Tutorial 12 – File Operations | 35 [fd, err] = mopen('test.dat', 'r'); [n, x] = mfscanf(fd, “%f”); for i = 1:4 a(i,:) = mfscanf(fd, “%f %f %f\n”); end The function mfscanf() returns n, the number of values read and the value x read. In this example n must be 1. If this value is not 1, which may happen if the file does not contain enough data, it indicates an error. Test the above code with the following data read from the file: -10.0 10. 2. 5. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. After executing the above statements, verify whether the data has been read correctly into the variables n, x and a by displaying their values.

ไม่มีความคิดเห็น:

แสดงความคิดเห็น