main()
{
    FILE *file = fopen ("COMMAND.txt", "r");
    FILE *file1 = fopen ("LOG.log", "a");
    if (file != NULL)
    {
        char line [255]; /* Max line Size*/

        while (fgets (line, sizeof line, file ) != NULL) /* Reading a line */
        {
            char first[10];
            if (sscanf(line, "%9s", first) ==1)
            {
                fprintf(file1, "######\n"); /* it suppose to print on the first line only when u executed */
                fprintf(file1, "%s\n", first);
            }
            printf(line); /* print on command prompt */

        }
        fclose (file);
        fclose (file1);
    }
    else
    {
        perror ("Please check the file name and try again!");
    }

}

having problem with printing a ###### as the first line only.
the idea putting ###### is seperating a new content with the old content.
with my code the ###### will print just before every single line

deceptikon commented: For using a field width with sscanf(). :) +11

Recommended Answers

All 5 Replies

Either
1) print the ##### before you start the while loop
2) set a flag named FirstLineFlag to true before you enter the loop and set it to false after you print. You can work out the rest of the details.

hmmm.. Thanks for that..
im new to programming so this kind of thing is killing me..

thanks,helpfull tip for a beginner as i am

Introducing a flag with additional code to set and check it seems like a poor solution, especially when it's not needed. Doesn't simply moving the printing instruction out of the loop do what you describe?

main()
{
    FILE *file = fopen ("COMMAND.txt", "r");
    FILE *file1 = fopen ("LOG.log", "a");

    if (file != NULL)
    {
        char line [255]; /* Max line Size*/

        fprintf(file1, "######\n"); /* it suppose to print on the first line only when u executed */

        while (fgets (line, sizeof line, file ) != NULL) /* Reading a line */
        {
            char first[10];
            if (sscanf(line, "%9s", first) ==1)
            {
                fprintf(file1, "%s\n", first);
            }
            printf(line); /* print on command prompt */

        }
        fclose (file);
        fclose (file1);
    }
    else
    {
        perror ("Please check the file name and try again!");
    }
}

Introducing a flag with additional code to set and check it seems like a poor solution, especially when it's not needed. Doesn't simply moving the printing instruction out of the loop do what you describe?

You're right, IF it's not needed. But that all depends on if the following output is acceptable:

line
line
line
########   'run #2
line
line
line
########   'run #3
########   'run #4
########   'run #5
line
line
########   'run #6
########   'run #7
########   'run #8
########   'run #9
line
line
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.