0

我一直在尝试遍历预定的字符数组并将其与扫描的单个字符进行比较。如果扫描的字符在数组中,我想将它添加到二维数组中,如果它不在数组中,我想进行错误处理。

我的代码目前是

    char c;
    char legalChar[] = "./\\=@ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
    int rowCount = 0;
    int colCOunt = 0;
    int i = 0;
    FILE * map;

    while (c = fgetc(map), c != EOF) {
        while (i < (sizeof(legalChar))){
            if (c == legalChar[i]){
                if (c == '\n'){
                    /*Add 1 to number of rows and start counting columns again */
                    rowCount++;
                    colCount = 0;
                }
                else {
                    /*would have code to add char to 2d array here */
                    colCount++;
                }
            }
        i++;
    }

我计划有

    if (c != legalChar[i]){
        /*Error handling */
    }

但这不起作用,因为它只是在每次迭代时跳入这个 if 语句。

目前程序的输出是 colCount 被赋值为 1,rowCount 保持为 0。迭代的所有字符都在 legalChar[] 数组中,所以我不确定我做错了什么。

任何建议将不胜感激。

谢谢

4

3 回答 3

1

我认为您可以使用 strchr 简化此代码:

    char c;
    char legalChar[] = "./\\=@ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
    FILE * map;
    int legal = 1;

    while (c = fgetc(map) && c != EOF && 1 == legal) {
        if (NULL == strchr(legalChar, c)) {
           legal = 0;
           // Error message pointing out invalid character
        }
        else {
           // Add to array
        }
    }

一旦发现无效字符,上述将中止循环。如果您不想这样做,只需删除对“合法”变量的所有引用。

于 2013-08-08T04:54:22.700 回答
1

在这里我做了一个小测试,我的代码运行良好,也许你可以试试:

#include "stdio.h"
  2 int main()                                                                                                                                              
  3 {
  4     char c;
  5     char legalChar[33] = "./\\=@ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
  6     int rowCount = 0;
  7     int colCount = 0;
  8     int i = 0;
  9     FILE * map;
 10     if(NULL ==(map = fopen("error.txt","r")))
 11             return -1;
 12     int is_ok; 
 13     while (!feof(map))
 14     {
 15          c = fgetc(map);
 16          i = 0;
 17          is_ok = 0;
 18          while (i < (sizeof(legalChar)))
 19          {
 20             if (c == legalChar[i])
 21             {
 22                 if(c == '\n')
 23                 {
 24                     rowCount++;
 25                     colCount = 0;
 26                 }   
 27                 else
 28                 {
 29                     colCount++;
 30                 }   
 31                 is_ok = 1;
 32             }   
 33             else
 34             {
 35                 if(i == 31&&is_ok == 0)// not the char in legalChar
 36                 {
 37                     printf("This char %c is ilegal in %d ,%d \n",c,rowCount,colCount);
 38                     colCount++;
 39                 }  
 40             }
 41             ++i;
 42         }
 43     }  
 44     return 0;
 45 } 
于 2013-08-08T04:52:08.613 回答
1

你的问题是这if (c != legalChar[i])几乎总是正确的。说输入的字符是M,里面很明显legalChar。如果你检查那c != legalChar[i],你是c != '.'第一次检查那,这显然是正确的。

处理此问题的更好方法是设置一个标志值,该标志值以 false 开头,如果您发现某些内容,则将其设置为 true。完成循环后,如果标志仍然为假,那么您就知道没有找到该值。

此外,您应该i在每次通过循环时重置,并且for循环比循环更有意义while,特别是如果您使用c99,i可以在循环中声明..

int c;
char legalChar[] = "./\\=@ABCDEFGHIJKLMNOPQRSTUVWXYZ\n";
int rowCount = 0;
int colCOunt = 0;
int i = 0;
int found = 0;
FILE * map;

while (c = fgetc(map), c != EOF) {
    found = 0;
    for (i = 0; i < sizeof(legalChar); i++){
        if (c == legalChar[i]){
            if (c == '\n'){
                /*Add 1 to number of rows and start counting columns again */
                rowCount++;
                colCount = 0;
            }
            else {
                /*would have code to add char to 2d array here */
                colCount++;
            }
            found = 1;
            // break out of loop here?
        }
    }
    if (!found) {
        // Error handling here
    }
}
于 2013-08-08T03:34:39.310 回答