2012-10-18 50 views
2

我是C新手。我無法找到一種方法來編寫能夠讀取所有ASCII字符(包括非打印字符和空格)的程序,來自stdin。 我知道scanf不適用於空白字符(不確定其他非打印字符)。 基本上我想把整個文件放到一個數組中,包含原始文本文件(stdin = .txt文件)的所有內容。 有誰知道我該怎麼做? 謝謝C編程:從標準輸入讀取所有ascii字符

+1

得到,與fgets,getchar函數? – bph

+0

已經選擇一個答案! –

回答

3

閱讀getchar()得到「所有ascii字符」。另外fgetc(stdin)

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

+0

'fgetc(stdin)'與'getchar()'相同。 –

+2

我知道:)他可以閱讀。 –

+0

它的工作非常感謝! – user1755521

1
#include<stdio.h> 
int main(){ 
    char buf[16]; 
    int c; 
    while (c=fread(buf,1,16,stdin)) 
    fwrite(buf,1,c,stdout); 
}