(C)尝试将.txt文件读入2d数组(并且失败了,请注意!)

时间:2016-02-26 04:06:56

标签: c windows memory memory-management multidimensional-array

我在PHP上工作了很多,有些用C ++,但对C来说是全新的,正如你可以通过我乱七八糟的代码来判断的。基本上,我正在寻找一个.txt文件的内容(一个网格,在这种情况下为我和我的朋友制作一个游戏)到一个二维数组,处理文件的内容,并将处理后的内容输出到一个单独的.txt文件。

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.center = [touches.anyObject locationInView:self.superview];

    CGRect bounds = self.superview.bounds;
    CGRect newFrame = self.frame;

    if (newFrame.origin.x < 0) {
        newFrame.origin.x = 0;
        self.userInteractionEnabled = NO;
    }
    if (newFrame.origin.y<0) {
        newFrame.origin.y = 0;
        self.userInteractionEnabled = NO;
    }
    if (newFrame.origin.x + newFrame.size.width > bounds.size.width) {
        newFrame.origin.x = bounds.size.width - newFrame.size.width;
        self.userInteractionEnabled = NO;
    }
    if (newFrame.origin.y + newFrame.size.height > bounds.size.height) {
        newFrame.origin.y = bounds.size.height - newFrame.size.height;
        self.userInteractionEnabled = NO;
    }

    self.frame = newFrame;
}

我收到了几个控制台错误和“调试错误......变量cols周围的堆栈已损坏。”我已经在各种关于动态内存分配的网站上做了很多阅读,但我显然不太了解如何将其付诸实践并从文件中动态分配2D数组。如果你能帮助我,我们将不胜感激。

Here's a picture of my console window.

And here's one of the contents of my input.txt file.

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

应用所有注释并清理逻辑并执行以下相应的错误检查结果。

以下内容清晰地​​编译。

以下内容未经过测试。

#include <stdio.h>  // fgets(), fopen(), fclose(), 
                    // sscanf(), fprintf(), printf()
#include <stdlib.h> // exit(), EXIT_FAILURE
#include <string.h> // memset()

void grab_sizeInfo( FILE *input, size_t *pRow, size_t *pCols );
void grab_input( FILE* fp_in, size_t rows, size_t cols, char **buffer);
void send_output(char *outfile, size_t rows, size_t cols, char **buffer);
void process_input(size_t rows, size_t columns, char **buffer);

int main(int argc, char *argv[])
{
    size_t rows = 0;
    size_t cols = 0;
    char **buffer = NULL;

    if (argc != 3)
    {
        fprintf(stderr, "Usage: %s InputFilename OutputFilename\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    FILE *fp_in = NULL;
    if( NULL == (fp_in = fopen( argv[1], "r") ) )
    { // then fopen failed
        perror( "fopen for input file failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    grab_sizeInfo( fp_in, &rows, &cols );
    printf("\nRows: %lu\nColumns: %lu\n", rows, cols);

    rows+=2;  // adjust for actual size
    cols+=2;  // adjust for actual size
    cols+=1;  // +1 to allow for NUL terminator byte

    if( NULL == ( buffer = malloc(rows * sizeof(char *)) ) )
    { // then malloc failed
        perror( "malloc for array of pointers to rows failed");
        fclose( fp_in );
        exit( EXIT_FAILURE );
    }

    // implied else, malloc for array of pointers to rows successful

    // prep array of pointers, to make cleanup easy
    memset( buffer, '\0', rows * sizeof(char *) );

    for ( size_t i = 0; i < rows; i++ )
    {
        if( NULL == (buffer[i] = malloc(cols) ) ) 
        { // malloc failed
            perror( "malloc for row failed" );
            // cleanup
            for( size_t j=0; j < rows; j++ )
            {
                free( buffer[j] );
            }
            free( buffer );
            fclose( fp_in );
            exit( EXIT_FAILURE );
        }

        // implied else, malloc for array of char in row successful

    } // end for each row

    grab_input( fp_in, rows, cols, buffer);
    fclose( fp_in );

    process_input(rows, cols, buffer);
    send_output(argv[2], rows, cols, buffer);

    printf("\n%s\n", argv[2]);
    printf("%lu\n", cols);

    return 0;
} // end function: main


void grab_sizeInfo( FILE *fp, size_t *pRow, size_t *pCols )
{
    char firstLine[20] = {'\0'};

    if( fgets( firstLine, sizeof firstLine, fp ) )
    { // successful read
        if( 2 != sscanf( firstLine, "%lu %lu", pRow, pCols) )
        { // then sscanf failed
            perror( "sscanf for rows and columns failed" );
            fclose( fp );
            exit(EXIT_FAILURE);
        }
    }
} // end function: grab_sizeInfo


void grab_input(FILE *fp, size_t rows, size_t cols, char **buffer)
{
    size_t rowNum = 0;
    while ( rowNum < rows && fgets( buffer[rowNum], (int)cols, fp ) )
    {
        printf("%s", buffer[rowNum]);
        rowNum++;
    }

    if( rowNum != rows )
    { // then failed to read complete file
        fprintf( stderr, "number of rows read: %lu Not equal Number expected rows: %lu\n", rowNum, rows );
        for( size_t i=0; i<rows; i++ )
        {
            free( buffer[i] );
        }
        free( buffer );
        fclose( fp );
        exit( EXIT_FAILURE );
    }
} // end function: grab_input