Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4237918
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:50:12+00:00 2026-05-21T02:50:12+00:00

Below is my code that will be called by each thread in order to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:50:13+00:00Added an answer on May 21, 2026 at 2:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the simplest, fastest way to complete the PHP code below such that
The code below shows a sample that I've used recently to explain the different
I first got an error usign the code below, explaining that DataGridLinkButton' must be
I have noticed that my geocoder is inconsistent in the code shown below because
I'm using this code, and I get the stack trace that is listed below.
When running the below code a type is never returned, despite there being a
In the below code snippet can i replace char * to const char *
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
In the below code sample, what does {0:X2} mean? This is from the reflection
In the below code, the ListBox gets filled with the names of the colors

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.