gpt4 book ai didi

C:试图显示包含引号的文件中的内容,但编译器将它们打印成奇怪的符号

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:12 28 4
gpt4 key购买 nike

该程序应该一次在终端显示 20 行文件的内容,并允许用户在每 20 行后按“q”停止程序:

#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 ---------- */

但是,如果文件包含单引号或双引号,它会将它们打印成奇怪的符号:

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.

我该如何解决这个问题?

最佳答案

对于 PowerShell

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

使用示例>disp20("randompoems")


对于 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;
}

使用示例

>chcp 1252
>prog

关于C:试图显示包含引号的文件中的内容,但编译器将它们打印成奇怪的符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25169131/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com