다음은 각 스레드가 ping 명령의 출력을 텍스트 파일에 기록하기 위해 호출 할 코드입니다. 파일이 제대로 작성되었지만 파일이 없습니다. 지금 내가 어떤 스레드를 생성하지 않았고, 단지 주에서이 같은 함수를 호출 :c의 텍스트 파일에 ping 명령의 출력을 쓰는 데 문제가 있습니까?
customPing("www.google.com", 10, 2, 1);
void customPing(char *url, int test_interval,int samplesPerTest, int testDuration)
{
printf("-->%s %d(sec) %d %d(hrs)\n", url, test_interval, samplesPerTest, testDuration);
int durationInProgress = 0,
durationInSeconds = testDuration * 10,
n = samplesPerTest;
char pingCmd[80];
char filename[10];
FILE *fptr;
sprintf(filename, "pingResult%d.txt", fileCounter++);
fptr = fopen(filename, "a");
sprintf(pingCmd, "ping -n %d %s >> %s ", n, url,filename);
printf("ping command: %s\n", pingCmd);
while (durationInProgress <= durationInSeconds)
{
system(pingCmd);
durationInProgress += test_interval;
printf("Going to sleep...\n");
fclose(fptr);
Sleep(test_interval);
fptr = fopen(filename, "a");
}
printf("***Done***\n");
}
출력 : 내가 잘못하고있는 무슨에
1. Enter url: www.google.ca
2. Enter Testing-Interval(10 sec, 20 sec, etc): 10
3. Test Samples per test (10, 100 etc): 2
4. Start test (yes/no): yes
4. Test Duration (1 hr, 24 hrs, etc) 1
***Test Starting***
-->www.google.com 10(sec) 2 1(hrs)
ping command: ping -n 2 www.google.com >> pingResult0.txt
The process cannot access the file because it is being used by another process.
Going to sleep...
The process cannot access the file because it is being used by another process.
Going to sleep...
***Done***
Press any key to continue . . .
어떤 아이디어? ? Visual Studio 2008, Win Vista를 사용 중입니다. (도움이되는 경우) 감사합니다.
, 왜 그냥 직접 다시 부모 핑에서 파이프'popen'에게 결과를 사용하지 읽기 모드로 파일을 열고 다음 명령을 실행? –