Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将ping命令的输出写入c中的文本文件时出现问题?_C_Multithreading_File Io_Accessibility_Ping - Fatal编程技术网

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

将ping命令的输出写入c中的文本文件时出现问题?,c,multithreading,file-io,accessibility,ping,C,Multithreading,File Io,Accessibility,Ping,下面是我的代码,每个线程将调用这些代码,以便将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(hr

下面是我的代码,每个线程将调用这些代码,以便将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");
}
你知道我做错了什么吗??? 我正在使用VisualStudio2008,WinVista。(如果有帮助的话) 谢谢。

要写入文件,必须使用
fprintf
,而不是
printf
sprintf


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

只需简单地
系统(“cmd>>文件”)
“cmd”的输出将在文件中结束。

要写入文件,必须使用
fprintf
,而不是
printf
sprintf


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


只要简单地
系统(“cmd>>文件”)
和“cmd”的输出将在文件中结束。

为什么在运行ping命令之前要在此程序中打开文件

我怀疑ping无法写入文件,但您没有看到stderr输出


运行命令,然后以读取模式打开文件

为什么在运行ping命令之前要在此程序中打开文件

我怀疑ping无法写入文件,但您没有看到stderr输出


运行命令,然后以读取模式打开文件

,但我的命令设置如下:ping-n2>>filename.txt,我认为>>将负责写入文本文件。如果我错了,请纠正我。sprintf写入一个字符串,这就是我创建文件名的方式。是的,我在喝了一杯热咖啡后查看了代码,我觉得如果我在使用>>那么为什么要使用文件*。另一个是文件名数组,它的长度是10,我把它改为20,现在可以了。谢谢你的帮助。但是我的命令设置是这样的:ping-n2>>filename.txt,我想>>将负责写入文本文件。如果我错了,请纠正我。sprintf写入一个字符串,这就是我创建文件名的方式。是的,我在喝了一杯热咖啡后查看了代码,我觉得如果我在使用>>那么为什么要使用文件*。另一个是文件名数组,它的长度是10,我把它改为20,现在可以了。感谢您的帮助。与其将ping的输出定向到一个文件,然后读取该文件,为什么不使用
popen
将ping的结果直接传输回父级?与其将ping的输出定向到一个文件,然后读取该文件,为什么不使用
popen
将ping的结果直接传输回父级?
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 . . .