1

我在 Siemens WinCC 7.0 中使用 C 脚本来读取包含源和目标逗号分隔的文本文件,例如

C:\Users\Administrator\Desktop\C File Transfer Test\Source\Cat.txt,P:\Cat.txt
C:\Users\Administrator\Desktop\C File Transfer Test\Source\Cat1.txt,P:\Cat1.txt
C:\Users\Administrator\Desktop\C File Transfer Test\Source\Cat2.txt,P:\Cat2.txt
C:\Users\Administrator\Desktop\C File Transfer Test\Source\Cat3.txt,P:\Cat3.txt
C:\Users\Administrator\Desktop\C File Transfer Test\Source\Cat4.txt,P:\Cat4.txt

我正在使用以下代码打开此文件并循环将文件从源移动到目标

#include "apdefap.h"


void File_Transfer()
{
    #define MODUL   "CopyProjekt "
    char pathIn[100];
    char pathOut[100];
    char szProjektname[255];
    FILE * fpInFile ;
    FILE * fpOutFile ;
    FILE *TempSource;
    FILE *TempDestination;  
    #pragma code ("kernel32.dll")
    BOOL CopyFileA(LPCTSTR,LPCTSTR,BOOL);
    #pragma code ()
    DM_DIRECTORY_INFO dmDirInfo;
    DM_PROJECT_INFO dmProjectInfo;
    CMN_ERROR dmError;
    char *source;
    char *destination; 
    char line[1000];
    char * tokens;
    char *tempTokens;
    int i;
    char tempString[1000];
    if (DMGetProjectDirectory("PDLRT", szProjektname, &dmDirInfo, &dmError )!= NULL)
    {   
        strcat(pathIn , dmDirInfo.szProjectDir) ;
        strcat(pathOut, dmDirInfo.szProjectDir) ;
        strcat(pathIn ,"FilesForTransfer\\FileData.txt");
        strcat(pathOut ,"FilesForTransfer\\FileDataTemp.txt");
        //(NULL,pathIn ,"2", MB_YESNO|MB_ICONQUESTION|MB_SYSTEMMODAL);      
    }
    //Open the file containing the folder names and paths
    fpInFile = fopen(pathIn,"r" );
    fpOutFile = fopen(pathOut,"w" );
        while (fgets(line,sizeof line,fpInFile) != NULL)
        {
            MessageBox(NULL,line,"Read Line",MB_YESNO|MB_ICONQUESTION|MB_SYSTEMMODAL);
            tempTokens = line;
            tokens = strtok(tempTokens ,",");
            while (tokens != NULL)
            {
                if (i == 0)
                {
                    source = tokens ;   
                }
                else 
                {
                    destination = tokens ;
                }
                i = i + 1;              
                //read the tokens again
                tokens = strtok(NULL ,",");
            }           
    //MessageBox(NULL,source ,destination, MB_YESNO|MB_ICONQUESTION|MB_SYSTEMMODAL);
            //Move the file from source to destination
            //if (CopyFileA(source,destination,FALSE) != 0)             
            if (rename(source ,destination )!=0)
            {
    MessageBox(NULL,"FAILED" ,"Transfer", MB_YESNO|MB_ICONQUESTION|MB_SYSTEMMODAL);
            }
            else
            {
         MessageBox(NULL,"PASSED","Transfer",MB_YESNO|MB_ICONQUESTION|MB_SYSTEMMODAL);
            }
        }       
    //rename(source,destination)
    //fputs(tempstring,fpOutFile );

    fclose(fpInFile );
    fclose(fpOutFile );
    remove(pathIn);
    rename(pathOut ,pathIn );

    //MessageBox(NULL,"done"  ,"Done" , MB_YESNO|MB_ICONQUESTION|MB_SYSTEMMODAL);
}

消息框每次都正确执行,源和目标将正确显示。但是,只有最后一个文件重命名有效。如果 source,destination 的参考文件中只有一行,那么它将正常工作。如果有多个,它只会在最后一个起作用。

据我所知,代码正在正确地通过循环并从查找文件中获取正确的数据,但重命名无法正常工作。任何想法,将不胜感激。谢谢

4

1 回答 1

1

fgets()如果找到,则将换行符存储在它正在填充的缓冲区中:

从给定的文件流中最多读取 count - 1 个字符并将它们存储在 str 中。生成的字符串始终以 NULL 结尾。如果出现文件结尾或找到换行符,则解析停止,在这种情况下 str 将包含该换行符。

所以destination文件名将包含换行符,这是非法的。在尝试之前将其删除rename()

char* nl_ptr = strrchr(destination, '\n');
if (nl_ptr) *nl_ptr = 0;

最后一行有效,因为没有换行符。

于 2013-04-17T15:34:22.587 回答