跳过空格并检测线路末端?

时间:2014-02-23 01:34:14

标签: c

我正在尝试创建一个读取以下内容的程序(调用此文本块数据1):

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

我知道行尾是\ 0。我基本上试图将整个事物分配给一个50 x 50(这是22 x 22)的数组,所有其他空白区域只是空格(即第23列,第1行只是一个空格)。

如何读取字符,跳过空格并检测行尾?

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

int main()
{
    int sizeOfGrid = 0;
    int rowNumber = 0;
    int colNumber = 0;
    /* */
    char letters[ROWS][COLUMNS];
    FILE *fp1 = fopen("data1","r");
    if (fp1)
    {
        for (rowNumber = 0; rowNumber < ROWS; rowNumber++)
        {
            for(colNumber = 0; colNumber < COLUMNS; colNumber++)
            {
                letters[rowNumber][colNumber] = ' ';
            }
        }
        /* Lets speak psuedocode here:
         * Search data1's first line
         * While character isn't end of line
         *  If character isn't a space,
         *      sizeOfGrid++ <--Count all the characters to get the dimensions.
         *      ^--Note that the width and height are locked together, i.e.
         *      an array with 18 width will have the same height as the width,
         *      so 18 width and 18 height.
         *  
         * While rowNumber and colNumber < (sizeOfGrid - 1)
         *  for rowNumber = 0; rowNumber < (sizeOfGrid - 1); rowNumber++
         *      for colNumber = 0; colNumber < (sizeOfGrid - 1); 
         *          letters[rowNumber][colNumber] gets the next character in data
         * */
    }

    return 0;
}

有多个文件需要读取,所以我想将其设置为尽可能动态,因此sizeOfGrid会在打开data2和data3时发生变化;它基本上重复了所有三个文件。

3 个答案:

答案 0 :(得分:1)

由于您似乎不知道行数和列数(除了检查文件),您应该立即将整行读入字符串gets()(或更好fgets()因为它允许最大线长度)。然后,您可以查找非空格(或大写字母),以确定矩阵的宽度(以及高度)。将该行声明为char的数组,其大小足以容纳最长的行。

答案 1 :(得分:0)

试试这段代码:

int rowNumber = 0;
int colNumber = 0;
int c;
/* */
char letters[ROWS][COLUMNS];
FILE *fp1 = fopen("data1","r");

memset(&letters[0][0], 32, ROWS*COLUMNS);

if(fp1 == NULL){printf("Cannot open file"); return 0;}

while( (c = fgetc(fp)) != EOF ){
    c = fgetc(fp);

    if(c == '\n'){
        if(rowNumber == ROWS - 1){printf("rowNumber = max \n"); break;}
        rowNumber++; colNumber = 0;
    }
    else if(c != ' '){
        if(colNumber == COLUMNS - 1){printf("colNumber = max \n"); break;}
        letters[rowNumber][colNumber] = (char)c; colNumber++;
    }
    else{;}
}

瓦尔特

答案 2 :(得分:0)

好的,澄清之后,这是我的话:

我的建议是使用fgetc而不是其他所有内容。就像在许多其他情况下一样,它是最轻松的选择。 fgetc获取文件中插入符号/光标所在的字符,将插入符号/光标前进到下一个位置。因为无论如何你几乎都会发出文件中的每个字符,它只是完全符合这种情况。

一个重要的评论: fgetc返回一个整数值。这是为了确保它能够正确处理EOF(-1)错误返回。

您所需要做的就是存储fgetc给您的角色,检查它是否为空格' '或新线'\n'或是您的字母'我喜欢存储,并做任何你需要做的事情。

如果不修改你到目前为止所做的事情,这里有一种方法:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 50
#define COLUMNS 50

int main( )
{
    int sizeOfGrid = 0;
    int rowNumber = 0;
    int colNumber = 0;
    /* */
    char letters[ROWS][COLUMNS] = { 0 };  // { 0 } for the initialization of each element with zeros
    FILE *fp1 = fopen( "data1", "r" );

    int currentChar;        // <-- introduced this variable

    if ( fp1 )
    {
        for ( rowNumber = 0; rowNumber < ROWS; rowNumber++ )
        {
            for ( colNumber = 0; colNumber < COLUMNS; colNumber++ )
            {

                // fgetc gets the character that the cursor/caret is currently on
                // moves the cursor/caret to the next poisition inside the file
                currentChar = fgetc( fp1 );

                // if it was a space, continue
                // but decrease the colNumber by 1 first, because it will get increased
                if ( currentChar == ' ' ) {
                    colNumber--;
                    continue;
                }

                // if it was a new line ('\n' or 10, the same)
                // just break out back inside the rowNumber loop
                if ( currentChar == '\n' ) {
                    // break out of the nested loop if we were on the last row of the grid
                    if ( rowNumber == sizeOfGrid - 1 ) rowNumber = ROWS;
                    break;
                }

                // having advanced this far implies that we have a valid line
                // so... increase the sizeOfGrid by 1

                // detect the size of grid through the amount of letters on the first row
                if ( rowNumber == 0 ) sizeOfGrid++;
                letters[rowNumber][colNumber] = currentChar;
            }
        }

        /* Lets speak psuedocode here:
        * Search data1's first line
        * While character isn't end of line
        *  If character isn't a space,
        *      sizeOfGrid++ <--Count all the characters to get the dimensions.
        *      ^--Note that the width and height are locked together, i.e.
        *      an array with 18 width will have the same height as the width,
        *      so 18 width and 18 height.
        *
        * While rowNumber and colNumber < (sizeOfGrid - 1)
        *  for rowNumber = 0; rowNumber < (sizeOfGrid - 1); rowNumber++
        *      for colNumber = 0; colNumber < (sizeOfGrid - 1);
        *          letters[rowNumber][colNumber] gets the next character in data
        * */
    }

    // for testing
    /*for ( rowNumber = 0; rowNumber < sizeOfGrid; rowNumber++ ) {
        for ( colNumber = 0; colNumber < sizeOfGrid; colNumber++ ) {
            printf( "%c ", letters[rowNumber][colNumber] );
        }
        putchar( 10 );
    }
    getchar( );*/

    return 0;
}

它中有很多评论,希望不会在你的脑海中留下任何问题。如果仍然存在混淆,请随时提出。

编辑#1:更新了代码以使用新引入的data1文件结构,该结构不仅仅包含网格。

相关问题