简体   繁体   中英

Cannot Print Variable Value that Was Incremented in Loop

I am currently trying to learn the C programming language and am using Kernighan and Ritchie's The C Programming Language, Second Edition . I am trying to write a program similar to an example they give in the book in chapter one regarding text streams.

My program is supposed to print out a text file to the terminal and then count the number of characters before printing that figure too. This is my code:

#include <stdio.h>

int main()
{
    int count = 0, c;

    while ((c = getchar()) != EOF) {
        count++;
        putchar(c);
    }

    printf("\n%d characters\n", count);
    return 0;
}

This is the output:

This is a simple file
With two very small lines
I mean three
1 characters

But when I add in this line below count++ :

printf("%d", count);

The result is:

1T2h3i4s5 6i7s8 9a10 11s12i13m14p15l16e17 18f19i20l21e22
23W24i25t26h27 28t29w30o31 32v33e34r35y36 37s38m39a40l41l42 43l44i45n46e47s48
49I50 51m52e53a54n55 56t57h58r59e60e61
1 characters

This means that count is being incremented, but I cannot access its value from outside of the while loop. I thought that if the declaration of the variable (in this case int count = 0 is outside the loop), then the value could still be reached after the loop.

If it makes a difference, this is what I call it with:

./example <text.txt

Am I making a silly mistake? Any help would be appreciated. Thanks

Update

I do not know if it makes a difference, but the compiler I am using is: powerpc-apple-darwin9-gcc-4.0.1. Is that what the problem is?

Update 2

This is the command I used to compile the source: gcc -Wall example.c -o example . A weird thing has just happened though. It worked perfectly when I ran this:

#include <stdio.h>

int main()
{
    int count = 0, c;

    while ((c = getchar()) != EOF) {
        count++;
        putchar(c);
    }
    printf("x");
    printf("\n%d characters\n", count);
    return 0;
}

The output was:

This is a simple file
With two very small lines
I mean three
61 characters

The only difference between this code and the code above is the printf("x") line. I don't understand why it suddenly works if I print something else and I don't understand why it isn't visible. Any character can be put in the command and the same result happens. If more than one character is added, then the second one, and any after that, is printed.

Most likely your reference of characters to where characters are stored in memory is incorrectly coded which is why your output comes out like that. Ensure that the file and text within are correctly referenced. Other than that, the code seems OK.

I think this is what you should do:

int count = 0, c;
FILE *file = fopen("example.txt", "r");

while ((c = getc(file)) != EOF) {
    count++;
    putchar(c);
}

printf("\n%d characters\n", count);
fclose(file);

Try issuing a fflush(stdout) immediately before the printf("\\n%d characters\\n", count);

FYI: your origninal code executes as expected on Ubuntu 14.04 Linux.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM