Fortran跨目录读写数据问题:可导入但无法完成导出验证
Hey Rob, let's work through this issue step by step. You mentioned your Fortran (.f90) code can read data from /Users/rob/Desktop/DISSERTATION/DSMCDATA/ without problems, but when you export modified data to the same directory under a new filename, the subsequent verification import fails. Here are the most common culprits and actionable fixes to check:
1. Verify File Paths & Permissions
- Always use absolute paths for exports: If your code lives in
/Users/rob/Desktop/DISSERTATION/DSMCCODE/, relative paths might accidentally write files to your code directory instead ofDSMCDATA. Explicitly define the full path when opening the export file:integer :: export_unit open(unit=export_unit, file='/Users/rob/Desktop/DISSERTATION/DSMCDATA/your_new_file.dat', & status='replace', action='write', iostat=ios) - Check directory permissions: Run
ls -ld /Users/rob/Desktop/DISSERTATION/DSMCDATA/in your terminal to confirm your user account has write access (look for thewflag in the permission string). If not, adjust permissions withchmod u+w /Users/rob/Desktop/DISSERTATION/DSMCDATA/.
2. Match Export Format to Import Format
A mismatch between how you read and write data is a super common issue:
- Text files: Ensure the export uses the same column separators, line endings, and data formatting as the original input. For example, if your input uses space-separated values, don’t switch to commas when exporting. Manually open the exported file to spot obvious formatting errors.
- Binary files: Double-check that variable types, array dimensions, and byte order match exactly. If your input uses
real(8)(double-precision), don’t write the export asreal(4)(single-precision). Even a tiny mismatch will break verification.
3. Fix File Handling in Your Code
- Always close files after use: If you don’t call
close(unit=export_unit)after writing, the data might stay in memory buffers instead of being flushed to disk—making it invisible to your import step. - Avoid unit number conflicts: Reusing the same unit number for read/write operations without closing first can cause crashes or silent failures. Use Fortran 2008’s
newunitkeyword to auto-assign safe, unique units:integer :: import_unit, export_unit, ios ! Import step open(newunit=import_unit, file='/Users/rob/Desktop/DISSERTATION/DSMCDATA/input.dat', & status='old', action='read', iostat=ios) if (ios /= 0) then print *, "Failed to open input file! Iostat code: ", ios stop end if ! ... read data ... close(import_unit) ! Export step open(newunit=export_unit, file='/Users/rob/Desktop/DISSERTATION/DSMCDATA/output.dat', & status='replace', action='write', iostat=ios) if (ios /= 0) then print *, "Failed to open export file! Iostat code: ", ios stop end if ! ... write data ... close(export_unit) - Use
iostatfor error checking: Addingiostatto youropencalls gives you concrete error codes to diagnose issues (e.g., code 13 = permission denied, code 2 = file not found).
4. Debug with Concrete Checks
- Print the exact file paths your code is using for export/import—typos (like missing slashes, capitalization errors) are easy to miss.
- For text files, use the
diffcommand in terminal to compare a small subset of the original and exported data:diff /Users/rob/Desktop/DISSERTATION/DSMCDATA/input.dat /Users/rob/Desktop/DISSERTATION/DSMCDATA/output.dat | head -20
内容的提问来源于stack exchange,提问作者Rob A.
相关产品推荐
相关产品推荐

