程序在while循环后停止运行

时间:2016-06-20 11:05:11

标签: c

所以,自从我上一次用C语言编程已经有一段时间了,现在我想再次回到C语言,但是我的程序遇到了问题。该程序非常简单,我使用getchar将char存储在char数组中,但由于某种原因,程序在我的while循环后停止运行。

#include <stdio.h>
#define MAXLINE 1000

int main(){
    char c;
    char input[MAXLINE];
    int i = 0;

    while((c = getchar()) != EOF){
         input[i] = c;
         ++i;
    }
    printf("Still running");
}

因此,我的程序不会打印“仍在运行”。

2 个答案:

答案 0 :(得分:2)

发送EOF(Ctrl + D代表* nix Ctrl + Z代表Win),它会显示静止运行。

    import nltk

# function start
def extract_entities(text):
    ind = len(text)-7
    sub = text[ind:]
    print(sub)
    output.write('\nPRODID=='+sub+'\n\n')
    for sent in nltk.sent_tokenize(text):
        for chunk in nltk.ne_chunk(nltk.pos_tag(nltk.word_tokenize(sent))):
            if hasattr(chunk, 'label'):
                output.write(chunk.label()+':'+ ' '.join(c[0] for c in chunk.leaves())+'\n')

# function end

# main program
# -*- coding: utf-8 -*-
import sys
import codecs
sys.stdout = codecs.getwriter("iso-8859-1")(sys.stdout, 'xmlcharrefreplace')
if sys.stdout.encoding != 'cp850':
  sys.stdout = codecs.getwriter('cp850')(sys.stdout.buffer, 'strict')
if sys.stderr.encoding != 'cp850':
  sys.stderr = codecs.getwriter('cp850')(sys.stderr.buffer, 'strict')
file = open('C:\Python34\Description.txt', 'r')
output = open('C:\Python34\out.txt', 'w')

for line in file:
 if not line : continue
 extract_entities(line)

file.close()
output.close()    

答案 1 :(得分:1)

您的计划仅适用于运气,因为getchar会返回int而不是char。原因是getchar可能会返回EOF,这不一定代表char

要修复此错误,您需要将char c替换为int c

相关问题