使用echo读取文件名,并使用该文件 [英] reading in filename using echo and using the file

查看:568
本文介绍了使用echo读取文件名,并使用该文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用回声FILENAME.TXT | AC ,在终端和从文件中读取到一个数组我在头文件中得到的数据,但在code我到目前为止只是给了我一个无限循环。我试图存储在本地阵列的信息也不过还是同样的结果。

 主(INT ARGC,CHAR *的argv []){
    EXTERN字符测试[];
    FILE * FP;
    INT I = 0;
    INT℃; //是CHAR℃;本来
    如果(的argc == 1){
        FP =标准输入;
    }其他{
        FP = FOPEN(的argv [1],R);
    }
    而((C = GETC(FP))!= EOF){
        测试由[i] = C;
        的printf(%C,测试[I]);
        我++;
    }
}


解决方案

(1)改变变量c为int所以它可以识别EOF。

(2)你的printf前不要增量I否则你会被打印垃圾。

不知道你正在尝试与回波的事情来完成的。

I'm trying to use "echo FileName.txt | a.c" in terminal and read the data from the file into a array i got in a header file, but the code I have so far is just giving me a infinite loop. I tried storing the info in a local array also but still the same result.

main(int argc, char *argv[]) {
    extern char test[];
    FILE *fp;
    int i = 0;
    int c;       // was char c; originally
    if (argc == 1) {
        fp = stdin;
    } else {
        fp = fopen (argv[1], "r");
    }
    while ((c = getc(fp)) != EOF) {
        test[i] = c;
        printf("%c", test[i]);
        i++;
    }
}

解决方案

(1) Change variable c to an int so it recognizes EOF.

(2) Don't increment i before your printf or you will be printing junk.

Not sure what you are trying to accomplish with the echo thing.

这篇关于使用echo读取文件名,并使用该文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆