2

This program is supposed to display the contents of a file at the terminal 20 lines at a time, and allows user to press 'q' after every 20 lines to stop the program:

#include    <stdlib.h>
#include    <stdio.h>

int main ( int argc, char *argv[] )
{
    int     i;
    FILE    *inputFile = fopen ( "randompoems", "r" );
    char    buffer[256];

    do {
        for ( i = 0; i < 20 && ! feof (inputFile); i++ ) {
            fgets(buffer, sizeof(buffer), inputFile);
            printf ( buffer );
        }
    } while ( ( fgetc(stdin) != 'q' ) && ! feof (inputFile) );

    fclose (inputFile);

    return EXIT_SUCCESS;
}       /* ----------  end of function main  ---------- */

However, if the file contains single or double quotes, it prints them into weird symbols:

The End of the World
By Dana Gioia

ôWe're going,ö they said, ôto the end of the world.ö
So they stopped the car where the river curled,
And we scrambled down beneath the bridge
On the gravel track of a narrow ridge.

We tramped for miles on a wooded walk
Where dog-hobble grew on its twisted stalk.
Then we stopped to rest on the pine-needle floor
While two ospreys watched from an oak by the shore.

We came to a bend, where the river grew wide
And green mountains rose on the opposite side.
My guides moved back. I stood alone,
As the current streaked over smooth flat stone.

Shelf by stone shelf the river fell.
The white water goosetailed with eddying swell.
Faster and louder the current dropped
Till it reached a cliff, and the trail stopped.

I stood at the edge where the mist ascended,
My journey done where the world ended.
I looked downstream. There was nothing but sky,
The sound of the water, and the waterÆs reply.

How do I fix this?

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
vxs8122
  • 834
  • 2
  • 10
  • 22
  • 7
    This seems very much like a problem with encoding. Do you know what file encoding the file is, and what encoding your terminal displays the output in? – Frxstrem Aug 06 '14 at 20:06
  • Ok, just found out that my file is in ASCII encoding and the PowerShell terminal is in 16-bit Unicode encoding. – vxs8122 Aug 06 '14 at 20:16
  • @vxs8122 Probably should answer your own question, or close it. – Fiddling Bits Aug 06 '14 at 20:30
  • Well, I am still trying to figure out how to fix this encoding problem. I am thinking about what to include in the program. – vxs8122 Aug 06 '14 at 20:45
  • Actually, I think both are in ASCII encoding. When I type in `Write-Host $OutputEncoding` it outputs `System.Text.ASCIIEncoding`, and when I type in `Get-FileEncoding randompoems` it outputs `ASCII`. Now, I don't know what is the real problem is. – vxs8122 Aug 06 '14 at 20:54
  • Just print out the value of the actual characters in the file, or use a hex editor to check them. That'll tell you easily enough whether or not it's ASCII. Since the characters for the opening and closing quotes appear to be different, I'm guessing it's not. – Crowman Aug 06 '14 at 21:22
  • I'll bet the problem is that the quotes are Word's 'smart-quotes': http://stackoverflow.com/questions/1262038/how-to-replace-microsoft-encoded-quotes-in-php – Michael Burr Aug 06 '14 at 22:44
  • Yeah, the randompoems file is pasted from a website and contains "left" and "right" quotes. I am trying to get their values and convert them to regular quotes. – vxs8122 Aug 06 '14 at 22:53
  • 1
    `printf ( buffer );` -- This is dangerous; any `%` characters that happen to be in `buffer` will cause `printf` to misbehave. Use `printf("%s", buffer);` or `fputs(buffer, stdout);`. This isn't the cause of the problem you're seeing, but you should definitely fix it. – Keith Thompson Aug 06 '14 at 23:53

1 Answers1

1

for PowerShell

function disp20($path){
    foreach($lines in ( Get-Content $path -ReadCount 20)){
        Out-Host -InputObject $lines
        $in = Read-Host
        if($in -eq "q"){
            break
        }
    }
}

Example of use >disp20("randompoems")


for C

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <wchar.h>

int main ( int argc, char *argv[] ){
    wchar_t buffer[256];
    int i = 0;
    FILE* f = fopen("randompoems", "r,ccs=UTF-16LE");
    setlocale(LC_ALL, "English_United States.1252");
    while(fgetws(buffer, 256, f)){
        wprintf(L"%s", buffer);
        if(++i % 20 == 0 && fgetc(stdin) == 'q')
            break;
    }
    fclose(f);
    return EXIT_SUCCESS;
}

Example of use

>chcp 1252
>prog
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • Thanks, but it has to be in C programming language, as part of the exercise. – vxs8122 Aug 06 '14 at 22:42
  • Hmm, when I compiled it, I got the following errors: `warning: implicit declaration of function fgetws [-Wimplicit-function-declaration]` and `warning: implicit declaration of function ‘wprintf’ [-Wimplicit-function-declaration]` – vxs8122 Aug 06 '14 at 23:55
  • 1
    @vxs8122 try `#include ` – BLUEPIXY Aug 07 '14 at 00:18