将 ping 命令的输出写入 c 中的文本文件时出现问题?

发布于 2024-10-28 11:03:07 字数 1709 浏览 6 评论 0原文

下面是我的代码,每个线程都会调用该代码,以便将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮萍、无处依 2024-11-04 11:03:07

要写入文件,您必须使用 fprintf,而不是 printfsprintf


不要使用 代码中的 FILE* 操作。输出会自动重定向到正确的文件,如果您尝试同时从程序内部使用该文件,则会发生不好的事情。

只需简单地 system("cmd >> file") ,“cmd”的输出将最终出现在文件中。

To write to the file you must use fprintf, not printf or sprintf!


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.

玻璃人 2024-11-04 11:03:07

为什么在运行 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文