将 ping 命令的输出写入 c 中的文本文件时出现问题?
下面是我的代码,每个线程都会调用该代码,以便将 ping 命令的输出写入文本文件。该文件已正确创建,但其中没有任何内容。现在我没有创建任何线程,只是从 main 中调用了这样的函数:
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。(如果有帮助的话) 谢谢。
Below is my code that will be called by each thread in order to write the output of ping command to a text file. The file is created properly but there is nothing in it. For now i did not create any threads and just called the function like this from main:
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");
}
OUTPUT:
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 . . .
Any ideas on what am i doing wrong???
I am using Visual Studio 2008, Win vista.(if that helps)
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要写入文件,您必须使用fprintf
,而不是printf
或sprintf
!不要使用
代码中的 FILE*
操作。输出会自动重定向到正确的文件,如果您尝试同时从程序内部使用该文件,则会发生不好的事情。只需简单地
system("cmd >> file")
,“cmd”的输出将最终出现在文件中。To write to the file you must usefprintf
, notprintf
orsprintf
!Don't use
FILE*
operations in your code. The output is automatically redirected to the correct file, and if you try to use the file from inside the program at the same time, bad things happen.Just simply
system("cmd >> file")
and the output of "cmd" will end up in the file.为什么在运行 ping 命令之前在此程序中打开文件
我怀疑 ping 无法写入文件,但您看不到 stderr 输出。
运行命令,然后以读取模式打开文件
why are you opening the file in this program before running the ping command
I suspect the ping fails to write to the file but you dont see the stderr output.
run the command then open the file in read mode