2

我是 C 新手。我很难找到一种方法来编写一个能够从标准输入读取所有 ASCII 字符(包括非打印字符和空格)的程序。我知道 scanf 不适用于空白字符(不确定其他非打印字符)。基本上我想将整个文件放入一个数组中,其中包含原始文本文件(stdin = .txt 文件)中的所有内容。有谁知道我该怎么做?谢谢

4

2 回答 2

3

继续阅读getchar()以获取“所有 ascii 字符”。也fgetc(stdin)

要写入文件,您需要使用FILE *fopen(const char *filename, const char *mode) function, fputc(int c, FILE *stream), fclose(FILE *)

于 2012-10-18T07:51:44.480 回答
1
#include<stdio.h>
int main(){
  char buf[16]; 
  int c; 
  while (c=fread(buf,1,16,stdin)) 
    fwrite(buf,1,c,stdout);
}
于 2012-10-18T08:05:35.280 回答