我在 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 的引用文件中只有一行,那么它就可以正常工作。如果有多个,它只会对最后一个起作用。
据我所知,代码正在循环中正确运行并从查找文件中获取正确的数据,但重命名无法正常工作。 任何想法,将不胜感激。 谢谢
最佳答案
fgets()
如果找到,将换行符存储在它正在填充的缓冲区中:
Reads at most count - 1 characters from the given file stream and stores them in str. The produced character string is always NULL-terminated. Parsing stops if end-of-file occurs or a newline character is found, in which case str will contain that newline character.
因此目标
文件名将包含换行符,这是非法的。在尝试 rename()
之前删除它:
char* nl_ptr = strrchr(destination, '\n');
if (nl_ptr) *nl_ptr = 0;
最后一行有效,因为没有换行符。
关于c - 为什么c脚本只在最后一次执行循环时重命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16064095/