【问题标题】:Variable is set equal to zero after eighth iteration第八次迭代后变量设置为零
【发布时间】:2020-09-30 23:43:40
【问题描述】:

我正在为班级做 c 样式文件的作业。我已经走了很远,但是在我的循环中,一个变量的行为很奇怪。在第八次迭代之后 z 应该设置为 9 时设置为 0。我不知道为什么它会设置为 0,如果它明确添加 1。

//Aaron Hervey
//Date: 09/23/2020

#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>

struct Room{
    long size;
    char * details;
    char * name[sizeof(size)];
    char * desc[sizeof(size)];
    char * exit[sizeof(size)];
};

int main(int argc, char *argv[]){
    //Variables
    unsigned int i = 0;
    char * holder;
    long size;
    char choice;
    bool fa = true;
    FILE *fl;
    Room p;
    //Error checking commmand line
    if (argc < 1) {
          printf("Usage: %s <filename>\n", argv[0]);
          return -1;
    }
    char test[] = "room1";
    fl = fopen(test, "rb");
    if (fl==nullptr){
        printf ("Error opening file");
    }
    
    //Get File Size
    fseek(fl, 0, SEEK_END);
    size = ftell(fl);
    p.size = size;
    rewind(fl);

    //Allocate whole fize size to var & Write file into struct member
    p.details = (char*)malloc(sizeof(char)*size);
    fread(p.details, 1, size, fl);

    //Write file into struct
    unsigned int z = 0;
    while (true){
        if (z==8){
            int test = 1;
        }
        if(z>0){
            holder = strtok(nullptr , "~");
            if(holder == nullptr){
                break;
            }
            p.name[z] = holder;

            holder = strtok(nullptr , "~");
            p.desc[z] = holder;

            holder = strtok(nullptr , "~");
            p.exit[z] = holder;
            z++;  
        }
        else{
            holder = strtok(p.details , "~");
            if(holder == nullptr){
                break;
            }
            p.name[z] = holder;

            holder = strtok(nullptr , "~");
            p.desc[z] = holder;

            holder = strtok(nullptr , "~");
            p.exit[z] = holder;
            z++;
        }
    }

fclose(fl);

    //Menu
while(fa){
    printf("> ");
    scanf("%c", &choice);
        switch (choice){
            case 'q':
                fa = false;
                break;
            case 'l':
                printf("%s\n", p.name[i]);
                printf("%s\n", p.desc[i]);
                printf("Exits: %s\n", p.exit[i]);
                i++;
                break;
            case 'w':
                break;
            case 'n':
                break;
            case 's':
                break;
            case 'e':
                break;
        }
    }
    return 0;
}

如您所见,我有一个循环,它不断地将文件信息从结构存储到数组中,但我似乎无法理解为什么迭代变量不合作

【问题讨论】:

  • scanf(" %c", &amp;choice); -- 注意添加的空格..
  • 等等,我在我的代码中没有看到。你告诉我添加它吗? @DavidC.Rankin
  • // Menu 中,每次用户输入一个选项并按Enter 时,'\n' 将留在stdin 中,您将其作为下一个输入。在 "%c" 之前添加一个空格可确保将其忽略。
  • 感谢您的建议!我在输入时遇到问题,但无法确定@DavidC.Rankin
  • 更好,char choice[256]; 然后fgets (choice, sizeof choice, stdin); 然后switch (*cihoice) { ... } 相当于switch (choice[0]) { ... }。这样,即使用户输入了"e is for everyone...",整个行也被消耗掉了,而您与'e' 进行比较也是一样的,但是任何一个输入影响下一个输入的机会都被消除了......

标签: c file-io


【解决方案1】:
long size;
char * name[sizeof(size)];

这将name 声明为通常由8 个(可能是4 个或其他值)char * 组成的数组。

p.name[z] = holder;

z 等于 8 时,您的写入超出了数组的范围,因此任何事情都可能发生,其中之一就是覆盖变量 z 本身。

【讨论】:

  • 你对我能做些什么来纠正这个问题有什么建议吗?我希望它们足够灵活或足够大以处理任何文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 1970-01-01
  • 2016-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多