字符串连接在Fortran中不起作用

如何解决字符串连接在Fortran中不起作用

我有一个子例程,该子例程将字符串连接起来以创建文件名,因此我可以跟踪非线性迭代中的解决方案更改。但是,它似乎不起作用。我之前使用此代码段没有任何问题,所以我想知道这是否依赖某些编译器标志吗?字符串连接部分位于底部,我返回以下内容:

output_name:PLT / Res 文件名1: PLT / Res
文件名2: PLT / Res
写入文件... 0

清楚表明我的字符串未连接。 我用以下方式调用子例程:

   IF (MOD(counter,plot_freq) == 0) call plotSolution(2,counter,'PLT/Solution',Mesh,W)

例程在下面。

subroutine plot_nodal_field(opt,iter,output_file,Q)
   use Globals_module,only : c_in
   use inputs_module,only : p_in,gamma,rho_in,M_inf
!--------------------------------------------end of use--------------------------------------------!
   integer(i4),intent(in) :: iter,opt
   type(Mesh_Type),intent(in) :: Mesh
   character(*),intent(in) :: output_file
   real(dp),intent(in) :: Q(Mesh%numFields,Mesh%numNodes)
!------------------------------------------end of intents------------------------------------------!
   integer(i4)   :: elem_id,n1,n2,n3
   character(80) :: filename,file_num
   integer(i4)   :: iunit
   integer(i4)   :: n,io,i
   real(dp)      :: x,y,rho,rhoU,rhoV,rhoE,u,v,P,CP,c,MachNum,s
!------------------------------------------end of declare------------------------------------------!


  iunit = 2

  write(file_num,'(I6.6)') iter
  write(*,*) 'output_name:',output_file
  filename = adjustl(trim(output_file)) // '_' // adjustl(trim(file_num)) // '.plt'
  write(*,'(A)') 'filename1:',filename
  filename = adjustl(trim(filename))
  write(*,'(A)') 'filename2:',filename
  print*,'# Writing to File...',iter

   open(unit = iunit,file = filename,status = 'replace',iostat = io)
   if( io /= 0) then
       print*,'ERROR: Opening 2nd order plt file'
   else
       if (opt == 1) then 
           write(iunit,'(A)')  'VARIABLES = "x","y","rho","U","V","P","MachNum","entropy"'
       elseif(opt == 2) then
           write(iunit,"P"'
   end if
       write(iunit,'(A7,I,A3,A41)')  'ZONE N=',Mesh%numNodes,' E=',Mesh%numTri,' DATAPACKING=POINT ZONETYPE=FETRIANGLE'
       do i = 1,Mesh%numNodes
           x    = Mesh%nodeCoords(1,i)
           y    = Mesh%nodeCoords(2,i)
           if (opt == 1) then
               rho  = Q(1,i)
               U = Q(2,i)
               V = Q(3,i)
               P = Q(4,i)
               c = sqrt(gamma*P/Q(1,i))
               MachNum = sqrt(Q(2,i)**2 + Q(3,i)**2)/c
               s = P/Q(1,i)**gamma - P_in/rho_in**gamma

               write(iunit,'(8ES)') Real(x),Real(y),Real(Q(1,i)),Real(Q(2,Real(Q(3,Real(Q(4,&
               & Real(MachNum),Real(s)
           elseif (opt == 2) then
               write(iunit,'(6ES)') Real(x),i))
           end if
     
       end do
       do i = 1,Mesh%numTri
           write(iunit,'(3I)') Mesh%trilist(1,i),Mesh%trilist(2,Mesh%trilist(3,i) 
       end do
   end if
   close(iunit)

end subroutine

编辑:

我试图提出一个最小的可验证示例:

program plotSolution                                                                                                        
!--------------------------------------------end of use--------------------------------------------!
integer       :: iter
character(80) :: filename
!------------------------------------------end of intents------------------------------------------!
filename = 'PLT/Solution'
iter = 1
call plot_nodal_field(iter,filename)

contains


subroutine plot_nodal_field(iter,output_file)
!--------------------------------------------end of use--------------------------------------------!
integer,intent(in) :: iter
character(*),intent(in) :: output_file
!------------------------------------------end of intents------------------------------------------!
character(80) :: filename,file_num
integer       :: iunit
!------------------------------------------end of declare------------------------------------------!


iunit = 2

write(file_num,'(I6.6)') iter
write(*,output_file
filename = adjustl(trim(output_file)) // '_' // adjustl(trim(file_num)) // '.plt'
write(*,filename
filename = adjustl(trim(filename))
write(*,filename
print*,iter

open(unit = iunit,iostat = io)
if( io /= 0) then
   print*,'ERROR: Opening 2nd order plt file'
else
end if
close(iunit)
end subroutine
end program

但这可行: 并返回:

输出名称: PLT /解决方案

文件名1: PLT / Solution_000001.plt
文件名2: PLT / Solution_000001.plt
写入文件... 1 错误:正在打开二阶plt文件

这向我表明我的主代码中可能存在一些奇怪的内存错误。我不确定。感谢所有建议和帮助!

解决方法

我使用此测试程序尝试了发布的例程:

  • 我找不到gfortran> = 5.3.0的问题,但是根据我的经验,gfortran的字符串处理在过去的版本中一直存在很多错误;
  • 每当我有可变大小的字符串时,我的编码习惯是拥有固定长度的字符串缓冲区(例如character(len=BUFFER_SIZE) :: buffer),进行所有临时剪切和粘贴,然后将相关部分分配给变量-函数末尾的长度变量(选择最大长度加上盐粒!)
  • 在所附示例中,我发布了一个示例路径创建函数。可以将其通用化以便更灵活地使用(选择文件夹名称等);
program test_string
   implicit none
   
   integer :: counter
   character(len=:),allocatable :: fileName
   
   counter = 2
   
   ! Original routine   
   call create_string_orig(counter,'PLT/Solution')
   
   ! With path function
   fileName = output_fileName('PLT',counter)
   write(*,*) 'filename with function: ',fileName
   
   contains
   
   function output_fileName(folder,iter) result(fileName)
      character(*),intent(in) :: folder      
      integer,intent(in) :: iter
      character(len=:),allocatable :: fileName
      
      ! Local variables 
      character(*),parameter :: prefix = 'Solution'
      character(*),parameter :: ext    = '.plt'
      character(len=1024) :: buffer
      character(len=6)    :: fileID
      integer :: fullLen
      
      write(fileID,'(I6.6)') iter
      
      buffer = trim(adjustl(folder)) // filesep() // prefix // '_' // fileID // ext
      
      fullLen = len_trim(buffer)
      
      allocate(character(len=fullLen) :: fileName)
      fileName(1:fullLen) = buffer(1:fullLen)
      
   end function output_fileName
   
   ! Get current system's folder separator
   character function filesep()
      character(len=9999) :: system_path
      integer             :: i

      system_path = repeat(' ',len(system_path))
      call get_environment_variable('PATH',system_path)

      do i = 1,len(system_path)
         if (system_path(i:i) == '/') then
            filesep = '/'
            return
         elseif (system_path(i:i) == '\') then
            filesep = '\'
            return
         endif
      end do

      ! Use default if unable to find a separator
      filesep = '/'
      return

   end function filesep   
   
   
   subroutine create_string_orig(iter,output_file)
      integer,intent(in) :: iter
      character(*),intent(in) :: output_file
      
      character(80) :: filename,file_num
       
      write(file_num,'(I6.6)') iter
      write(*,*) 'output_name: ',output_file
      filename = adjustl(trim(output_file)) // '_' // adjustl(trim(file_num)) // '.plt'
      write(*,*) 'filename1: ',filename
      filename = adjustl(trim(filename))
      write(*,*) 'filename2: ',filename
      print*,'# Writing to File...',iter
      
      end subroutine create_string_orig
end program  

这将产生以下输出(在Windows上运行):

$ a.exe
 output_name: PLT/Solution
 filename1: PLT/Solution_000002.plt
 filename2: PLT/Solution_000002.plt
 # Writing to File...           2
 filename with function: PLT\Solution_000002.plt
,

我感谢你们提供的所有帮助,并决定今天回到这里并发现问题。据我所知,这是一个编译器标志问题,其中编译器/预处理器将连接运算符 (//) 作为 C++ 注释,而预处理器在连接后将所有内容注释掉。我不得不使用连续符号来拼凑一个修复程序,但它现在有效,然后我可以再次查看标志。如果有人有任何见解,我将不胜感激。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-