是否会调用因函数而停止运行的C程序?

问题描述 投票:0回答:3

这可能很难解释。我正在开发一个程序,它接收一个包含数字的文件。前两个数字是矩阵行和列的维度。其余的数字是矩阵的元素。我遇到麻烦的是,在我创建了一个函数来读取给定c样式字符串中的数字后,程序停止执行任何操作。它编译并运行,但没有做任何事情,甚至没有打印主要后的第一行。

proj2.c

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

float readNum(char* buffer, int *pos);

int main(){


    char buffer[512];

    printf("Enter File Name: ");
    //char* fileName = fgets(buffer, sizeof(buffer), stdin);
    FILE* file = fopen("matrix.txt", "r");
    if(file == NULL){
        printf("ERROR COULD NOT OPEN FILE\n");
        exit(1);

    }
    int row = 0;
    int col = 0;
    int rowcheck = 0;
    int colcheck = 0;
    int matrixcheck = 0;
    while(!feof(file)){
        printf("HELLO");

        if(fgets(buffer,sizeof(buffer),file) != NULL){
            //position of current character
            int pos = 0;

            //current character
            char current;

            //loop to determine the dimensions of the matrix
            if(colcheck == 0 && rowcheck == 0){
                while(colcheck == 0 || rowcheck == 0){
                    //set current character
                    current = buffer[pos];
                    //determine if current character is a number and that the nex character is a space
                    //for single digit row dimensions
                    if(current >= '0' && current <= '9'  && buffer[pos+1] == ' ' && rowcheck == 0){
                        row += current - '0';
                        rowcheck = 1;
                    }
                    //if not single digit row dimension add the current character times 10
                    //and repeat loop to obtain the second digit
                    else if (buffer[pos+1] >= '0' && buffer[pos+1] <= '9' && rowcheck == 0){
                        row += (current - '0') * 10;
                    }
                    //for columns check if current character is a number and if the next character is space or newline
                    //and that row has already been checked
                    else if(current >= '0' && current <= '9' && (buffer[pos+1] == ' ' || buffer[pos+1] == 10) && rowcheck == 1){
                        col += current - '0';
                        colcheck = 1;
                    }
                    //final check for if columns is double digit so check if next char is a number and that current char is
                    //not a space
                    else if(buffer[pos] != ' ' && buffer[pos+1] >= '0' && buffer[pos+1] <= '9' && rowcheck == 1){
                        col += (current - '0' ) * 10;
                    }
                    pos++;
                    printf("rows: %d  cols: %d", row,col);
                }
            }
            //condition to ensure columns and rows have been determined
            else if(colcheck == 1 && rowcheck == 1){
                //loop to find the elements of the matrix
                while(matrixcheck == 0){
                    current = buffer[pos];
                    if(buffer[pos + 1] != 10){
                        if((current >= '0' && current <= '9') || current == '-' || current == '.'){
                            float num = readNum(buffer, &pos);
                            printf("number: %f", num);
                        }
                    }
                }

            }
        }
    }
    fclose(file);
}

和readNum.c

#include <stdio.h>
#include <math.h>

float readNum(char* buffer,int *pos){
    int negative = 1;
    int y = 0;
    float number = 0;
    if(buffer[*pos] == '-'){
        negative = -1;
        (*pos)++;
    }
    while(buffer[*pos + y] >= '0' && buffer[*pos + y] <= '9'){
        y++;
    }
    for(int z = 0; z < y; z++){
        number += (buffer[*pos + z] - 48) * pow(10, y - z - 1);
    }
    *pos += y;
    if(buffer[*pos] == '.'){
        (*pos)++;
        int d = 0;
        while(buffer[*pos + d] >= '0' && buffer[*pos + d] <= '9'){
            if(buffer[d + *pos] == '.'){
                printf("ERROR: multiple decimals in an element");
            }
            d++;
        }
        for(int z = 0; z < d; z++){
            number += (buffer[z + *pos] - '0') * pow(10, -z - 1);
        }
        pos += d;
    }
    return number * negative;
}

评论出来的

float num = readNum(buffer, &pos);
printf("number: %f", num);

允许程序正常运行,但取消注释它只是停止做任何事情,在日食控制台只是保持空白运行的东西或其他我稍后终止它因为没有发生任何事情,甚至没有打印第一行。

这是一个正在阅读的样本文件3 2 56 12 98 25 34.5 45

先感谢您

解决方案已被发现,我不确定是否每个人都能理解程序中究竟发生了什么。 main根本不会运行,第一行不会打印任何东西。对此的解决方案是在第一个print语句后使用fflush(stdout)。

c function
3个回答
1
投票

当您尝试读取浮点数时,逐字符解析文件是一种复杂的方法。使用标准库提供的功能。

您的代码可能会产生未定义的行为,因为您不检查buffer的边界,例如:

if(current >= '0' && current <= '9'  && buffer[pos+1] == ' ' && rowcheck == 0){
    row += current - '0';
    rowcheck = 1;
}

你永远不会检查你是否读取了'\0'终止字节并继续增加posbuffer[pos+1]可能超出限制。另外,我不明白你是如何解析尺寸的。这就是为什么我告诉你,不要重新发明轮子,使用你可以使用的工具。

你说尺寸在第一行,那么你可以通过这样做获得尺寸:

char buffer[512];
if(fgets(buffer, sizeof buffer, file) == NULL)
{
    fprintf(stderr, "File is empty\n");
    flcose(file);
    return 1;
}

size_t cols,rows;

if(fscanf("%zu %zu", &rows, &cols) != 2)
{
    fprintf(stderr, "Invalid file format, cannot get columns and rows\n");
    fclose(file);
    return 1;
}

if(rows == 0 || cols == 0)
{
    fprintf(stderr, "Invalid dimension %zux%zu\n", rows, cols);
    fclose(file);
    return 1;
}

现在,您可以像这样解析文件:

float matrix[rows][cols] = { 0 };

for(size_t i = 0; i < rows; ++i)
{
    if(fgets(buffer, sizeof buffer, file) == NULL)
    {
        fprintf(stderr, "End of file reached before filling matrix\n");
        fclose(file);
        return 1;
    }

    int pos;
    char *scan = buffer;

    for(size_t j = 0; j < cols; ++j)
    {
        if(sscanf(scan, "%f%n", matrix[i] + j, &pos) != 1)
        {
            fprintf(stderr, "Invalid format at line %zu\n", i+2);
            break; // continue parsing with the next line
        }

        scan += pos;
    }
}

fclose(file);

printf("matrix[%zu][%zu] = %f\n", rows/2, cols/2, matrix[rows/2][cols/row]);

此代码更加健壮,因为它会检查函数是否按预期工作。如果在填充矩阵之前无法读取更多行,则可以返回错误消息并结束程序。如果行没有正确的格式,我忽略该行,并且行也填充0,同时还打印错误消息。如果行数多于行数,则会忽略它们,并且不会溢出缓冲区。意图也更清晰,更容易理解我在做什么。

就像我在开始时说的那样,使用标准C库提供的功能比尝试再次发明轮子更好。您的代码很复杂且难以阅读。

另见why is while(feof) always wrong。使用fgets时管理文件结尾更容易,因为当没有更多数据可以读取时,fgets返回NULL,原因是I / O错误或文件达到了EOF。这就是为什么我上面的例子总是检查fgets的返回值。请注意我如何在%n格式中使用scanf%n返回从输入到目前为止所消耗的字符数,这在循环中使用sscanf时是一个很好的信息。我还检查scanf是否没有返回匹配元素的数量(请注意,%n不会增加匹配元素的数量)。有关这方面的更多信息,请参阅documentation of scanf


0
投票

这个循环可以永远运行:

while(buffer[*pos] >= '0' && buffer[*pos] <= '9'){
            y++;
}

我们怎样摆脱这个循环?:

        while(matrixcheck == 0){
            current = buffer[pos];
            if(buffer[pos + 1] != 10){
                if((current >= '0' && current <= '9') || current == '-' || current == '.'){
                    float num = readNum(buffer, &pos);
                    printf("number: %f", num);
                }
            }
        }

0
投票

解决方案已被发现,我不确定是否每个人都能理解程序中究竟发生了什么。 main根本不会运行,第一行不会打印任何东西。对此的解决方案是在第一个print语句后使用fflush(stdout)。

© www.soinside.com 2019 - 2024. All rights reserved.