fgets读得太晚了

时间:2014-02-24 18:41:25

标签: c fgets

好。在完全重写了这个程序以支持fgets后,它主要是用文字读取的。

这有两个部分,在gcc linux机器上编译。但是,需要重定向声明。

因此,一旦编译,它必须像这样运行:

./a.out < data1

所以这里是“data1”:

S T E L B M T F E Y D E E P S R T C I A E E 
N N E L I T R L B D E T A R E M U N E T Y L 
N O I T A N I M I R C N I F L E S S E N T A 
A U I D E W A R R A N T N U P R U S S E R P 
P G S G E A L P A P B A N P S A S S N M E A 
C O N S I T U T I O N D E E C W S O O H P D 
S V W D E L A N E E J A M E S M A D I S O N 
A E D E S N E G R J C U L T N O H L T I R A 
A R C E R R T R E E S B O N E E I D N N P R 
S N J U D I C I A L A S S E C O R P E U D I 
S M R A R A E B W B E S S M E O A U V P E M 
O E O I A I L N O U C D O D S S E N N I G R 
L N I D G Y T R C O M P E N S A T I O N N D 
D T O Z E H P Y N D R L E E A O H S C O I B 
I T P S U E T G O L U Z M M R B E H P I R T 
E O I E A R R S U U I B H A Y L L M S T F A 
R I N R E E E F U T L V Q U A R T E R I N G 
S I D B S R R D I Y E N I G M I A N A T I R 
S Q I S E B S C N S P E E C H R O T A E Y N 
D L C M I L I T I A F L R N C A T S S P S E 
R U T E D Y L E B I L C O H M L E T E S Y Y 
L S T R T E W Z L I O S A E N S A E I Y A L 
AMENDMENT
ASSEMBLY
BAIL
BEARARMS
CITIZEN
CIVIL
COMPENSATION
CONGRESS
CONSITUTION
CONVENTIONS
DELEGATED
DOUBLEJEOPARDY
DUEPROCESS
ENUMERATED
FREEDOM
GOVERNMENT
ILLEGAL
INDICT
INFRINGED
JAMESMADISON
JUDICIAL
LAWSUIT
LIBEL
LIBERTY
LIFE
MILITIA
MIRANDA
NECESSARY
PEACEABLY
PEERS
PETITION
POWER
PRESS
PROBABLECAUSE
PROPERTY
PUNISHMENTS
QUARTERING
RELIGION
RIGHTS
SEARCH
SECURITY
SEIZURE
SELFINCRIMINATION
SLANDER
SOLDIERS
SPEECH
SPEEDY
TRIAL
UNREASONABLE
WARRANT
WITNESS

这是程序本身:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 50
#define COLUMNS 50
#define TR 100
#define TC 100

int main( )
{
    int s1 = 0, s2 = 0;
    int sizeOfGrid = 0;
    int wordCount = 0;
    int rowNumber = 0;
    int colNumber = 0;
    /* */
    char tempLetters[ROWS][COLUMNS];
    char letters[ROWS][COLUMNS];
    char words[ROWS][COLUMNS];
    char wordTemp[50];
    char firstRow[100], nextRow[100];
    /* fgets(stdin); */
    char firstRowTemp[100], nextRowTemp[100];
    /* output       input */

    setbuf(stdout, NULL);

    for (s2 = 0; s2 < 100; s2++)
    {
        firstRowTemp[s2] = ' ';
        nextRowTemp[s2] = ' ';


        if (s2 < 50)
        {
            wordTemp[s2] = ' ';
            for (s1 = 0; s1 < 50; s1++)
            {
                words[s2][s1] = ' ';
            }
        }
    }

    /* Let's attempt to despace the first line, and then count the chars. */
    fgets(firstRow,100,stdin);
    s1 = 0;
    s2 = 0;
    for (colNumber = 0; colNumber < strlen(firstRow); colNumber++)
    {
        if ((firstRow[colNumber] != ' ') && (firstRow[colNumber] != '\n'))
        {
            firstRowTemp[s1] = firstRow[colNumber];
            letters[rowNumber][s2++] = firstRowTemp[s1++];
            sizeOfGrid++;
        }
    }

    /* We have now successfully gotten the size of the grid, so lets
     * put it to good use within our next loops to read in the rest
     * of the lines.
     * We take it in a for loop up to sizeOfGrid*2 because right now,
     * we still have spaces. Once we get rid of those, it'll be able to utilize
     * normal sizeOfGrid count.*/
    for (rowNumber = 1; rowNumber < sizeOfGrid*2; rowNumber++)
    {
        fgets(nextRow, 100, stdin);
        s1 = 0;
        s2 = 0;
        for (colNumber = 0; colNumber < sizeOfGrid*2; colNumber++)
        {
            if ((nextRow[colNumber] != ' ') && (tempLetters[rowNumber][colNumber] != '\n'))
            {
                nextRowTemp[s1] = nextRow[colNumber];
                letters[rowNumber][s2++] = nextRowTemp[s1++];
            }
        }
    }

    /* Next up, it's time to store the words. Scan the rest of the file for words
     * and store them as chars. */

    rowNumber = 0;
    colNumber = 0;

    while(fgets(wordTemp,50,stdin) != NULL)
    {
        for (s1 = 0; s1 < strlen(wordTemp); s1++)
        {
            if (wordTemp[s1] != '\n')
            {
                words[rowNumber][colNumber++] = wordTemp[s1];
            }

        }
        colNumber = 0;
        wordCount++;
        rowNumber++;
    }

    /* For testing purposes and making sure it receives the grid entirely.*/

    for (s2 = 0; s2 < sizeOfGrid; s2++)
    {
        printf("%c",firstRowTemp[s2]);
    }
    printf("\nSize of grid is %d\n",sizeOfGrid);
    printf("\n");
    for (rowNumber = 0; rowNumber < sizeOfGrid; rowNumber++)
    {
        for (colNumber = 0; colNumber < sizeOfGrid; colNumber++)
        {
            printf("%c", letters[rowNumber][colNumber]);
        }
        printf("\n");
    }

    for (rowNumber = 0; rowNumber < ROWS; rowNumber++)
    {
        for (colNumber = 0; colNumber < COLUMNS; colNumber++)
        {
            if ((words[rowNumber][colNumber] != ' ') && (words[rowNumber][colNumber] != '\n'))
                printf("%c", words[rowNumber][colNumber]);
            else
                break;
        }
        if (colNumber > 1)
            printf("\n");
    }

    /* End print debugging here.*/

    return 0;
}

现在我的问题本身......

看来fgets正在跳过大量单词,仅从LIBEL开始,而不是通过列表的顶部(在这种情况下是修改)。哪里出错了?

1 个答案:

答案 0 :(得分:0)

在您向我们展示的网格中,字母之间有水平间隔,但字母行之间没有空行,因此您需要使用sizeOfGrid*2循环遍历列,而不是行。