c - 如何用if函数打开3个文件

标签 c

您好,我正在尝试将第 3 个文件添加到下面的代码中,但我不知道如何执行此操作。

原代码:

char *fileOpen1;
char *fileOpen2;
while(fgets(line, 50, fr) != NULL)
{
    if(count == 0)
    {
        fileOpen1 = "file1.pifz";
    }
    else 
    {
        fileOpen2 = "file2.pifz";
    }
    count++;
    printf("%s", line);
    printf("%s", fileOpen1);
    printf("%s", fileOpen2);
}
fclose(fr);

新的测试代码(没用)

char *fileOpen1;
char *fileOpen2;
char *fileOpen3;
while(fgets(line, 50, fr) != NULL)
{
    if(count == 0)
    {
        fileOpen1 = "file1.pifz";
    }
    else if(count == 0)
    {
        fileOpen2 = "file2.pifz";
    }
    else 
    {
        fileOpen3 = "file3.pifz";
    }
    count++;
    printf("%s", line);
    printf("%s", fileOpen1);
    printf("%s", fileOpen2);
    printf("%s", fileOpen3);
}
fclose(fr);

如果我使用(新测试代码)它会显示 file1 和 file3 两次,而不是 file1 > file2 > file3

    imageString(im, black, 15, 160,253 , thePrograms[0].progName); //needs to be file1
    imageString(im, black, 15, 160,303 , thePrograms[1].progName); //needs to be file2, but is file3
    imageString(im, black, 15, 160,353 , thePrograms[2].progName); //needs to be file3

最佳答案

if(count==0)
{
    // You're here if count is 0
}
else if(count==0)
{
   // You're here if count isn't 0 and is 0
}
else
{
   // You're here if count isn't 0
}

您不能进入第一个 else if,因为您已经确定计数不为 0。

关于c - 如何用if函数打开3个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27787637/

相关文章:

c - 线程 1 : EXC_BAD_ACCESS

c - 将一组动态的可变参数传递给 C 函数

c - EOF 在 Mac Xcode (C) 中不起作用?

c - 将部分拆分为多个内存区域

c - "srmount error"是什么意思?

c++ - 使用 GCC 的函数检测,为什么使用 C++ STL 容器或流 I/O 会导致段错误?

c - 基于链表的 Infix 计算器中的错误

c - 为 Makefile 设置目标位置

c - 使用优化 (GCC) 时,“while”语句的工作方式不同

c - 使用C在单列中解析具有逗号分隔值的csv文件