我没有正确处理多个块

时间:2015-09-18 04:07:11

标签: c

我有一个作业,我想读取输入行,如

2
67 5 100 1 11 97 98 10 1 110
15 72 10 101 47 67 88 20 94 6 22 11

4
61 11 93 4 73 39 78 34 17 104
23 43 11 93 65 52 20 96 66 31 86 24 40 61 102 13 50 51
73 43 28 73 8 89 31 68 77 27 24 77 42 72 15 24 64 51
25 75 7 90 10 111 17 16

.....

处理每两个整数(块中的第一行只告诉我们将处理多少个单词),添加它们然后匹配 它们对应的ASCII字符。上面的例子是两个块。 输出应为:

 Decoded messages:
 Hello World!
 Happy Bhutanese teacher's day!

我遇到的问题是处理一个包含多个块的文件,超过20个等等,在一个输入文本上使用相同的格式。我可以处理一个块,两个块没关系但不好,因为我的程序似乎没有结束。任何行都不会超过256个字符。 numOfPuzzl指的是我们每个块处理多少个单词。 我非常感谢任何帮助。我附上了我的代码,并尽可能地评论。

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

int main(int argc, char* argv[])
{
    //user will type in file name they choose to process and we allocated  in filename[]
    char filename[256];
    printf("Enter filename: ");
    scanf("%s", filename);

    //process filename username typed in
    FILE *pFile;
    pFile = fopen(filename, "r");

    //if there's nothong to read
    if (pFile == NULL){
        exit(1);
    }

    printf("Decoded messages:\n");

    //create array we will putting lines into
    char myString[256];
    //simply gets the first line, which is always a lone integer
    fgets(myString, 256, pFile);

    int numOfPuzzl;
    sscanf(myString, "%d", &numOfPuzzl);
    //printf("puzzles to solve: %d\n", numOfPuzzl);
    int wordsProcessed = 0;

    //just remember that myString has entire line

    //start processing the lines that follow, a line is a word
    while (fgets(myString, 256, pFile) != NULL){
        int num = 0; //first integer in line
        int secondNum = 0;  //second int. in line
        int tot = 0;  //how many bytes
        int bytes_read = 0; //bytes processed

        char *endOfStrAdr = strchr(myString, '\0');  //returns address of end terminating char.
        int endOfStrIdx = endOfStrAdr - myString;  //this will give me the index of where the terminating char. occurs within my array

        //start scanning integers within array making sure to not sccan out of bounds
        while (tot < endOfStrIdx){

            //first integer allocated as well as how many byes it was
            sscanf(myString + tot, "%d %n", &num, &bytes_read);

            tot += bytes_read;  //keeps tab so we don't have to read from the begn. of array everytime

            //second integer allocated as well as how many byes it was

            sscanf(myString + tot, "%d %n", &secondNum, &bytes_read);

            tot += bytes_read;   ////keeps tab so we don't have to read from the begn. of array everytime

            printf("%c", (char) num + secondNum); //add the two integers and cast them to char

            //we want to check if we are the end of the string, our word
            if (tot ==  endOfStrIdx){

                printf(" ");
                wordsProcessed++;
                //we want to print a new line char. if we finished processing all the words for the puzzle
                if (wordsProcessed == numOfPuzzl){
                    printf("\n");
                    fgets(myString, 256, pFile);
                }
            }
        }
    }
    fclose(pFile);
}

2 个答案:

答案 0 :(得分:2)

  • 忽略谜题之间的空行。
  • 在处理新谜题之前重置参数($1numOfPuzzl)。

要归档,请更改

wordsProcessed

if (wordsProcessed == numOfPuzzl) {
    printf("\n");
    fgets(myString, 256, pFile);
}

答案 1 :(得分:0)

我建议这样:

random.shuffle()