簡體   English   中英

C編程-fgets()無限while循環

[英]C Programming - fgets() infinite while loop

我在從文本文件獲取增強矩陣時遇到問題。 該程序將在成功讀取文本文件中的所有數據后將進行簡單的段錯誤處理,這很可能是因為它仍在尋找另一個令牌來達到或fgets()未達到NULL狀態? 老實說我迷路了...

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include "apmatrix.h"
#define NUMARGS 2 
#define BUFSIZE 128

int main (int argc, char *argv[]) 
{ 
    /* Matrices that will always be used */
    Matrix* A;
    Vector* b;
    Vector* x;
    iVector* p;

    /* Used in LU factorization */
    Matrix* L;
    Matrix* U;

    /* Standard character read-in variables needed */
    char sep[] = " "; /* parsing separator is space */
    char *z,*buffer;  /* buffer and pointer to scan buffer */
    FILE *ifp;        /* source file */ 
    unsigned buflen;  /* length of line read */
    int flag, count;
    int Rows,Columns;/* to hold number read */
    int CurrentRow=0;

    /* first check to see if have required number for argc */ 
    /* if not, error has occurred, don't do anything else */ 
    if (argc == 2 || argc==3) 
    { 
        /* correct number of arguments used, try to open both files */ 
        buffer = (char*) malloc(BUFSIZE); /* allocate buffer */
        ifp = fopen (argv[1], "r");  /* open file for reading */ 

        if (ifp && buffer) { /* if successful proceed */
            /* read file line by line */
            flag=0;
            while (fgets(buffer, BUFSIZE, ifp) != NULL) {       
                /* defensive/secure programming */
                buflen = strlen(buffer);
                /* parse line */            
                z = strtok(buffer,sep);

                /* take in the values of the rows and columns */
                if(flag == 0){
                    ++flag;                 /* Flag = 1 */

                    Rows = atoi(z);         /* Convert the string to double */
                    printf("  %d",Rows);
                    z = strtok(NULL, sep);  /* find next number */
                    Columns = atoi(z);      /* Convert the string to double */           
                    printf("  %d",Columns);
                    putchar('\n');

                    A = m_alloc(Rows,Columns);
                    b = v_alloc(Rows);
                    x = v_alloc(Rows);
                    p = iv_alloc(Rows);

                    L = m_alloc(Rows,Columns);
                    U = m_alloc(Rows,Columns);
                }
                /* take in the values of the matrices */
                else{

                    for(int i = 0; i < Rows; i++){
                        A->mat[CurrentRow][i] = (Real) atof(z);             /* Convert the string to an int*/
                        z = strtok(NULL, sep);  /* find next number */
                        printf("   %2.3f   ",A->mat[CurrentRow][i]);
                    }

                    b->vec[CurrentRow] = (Real) atof(z);            /* Convert the string to an int */
                    printf("   %2.3f   ",b->vec[CurrentRow]);           
                    putchar('\n');
                    CurrentRow++;
                    putchar('\n');
                }

            } 
            fclose (ifp); 

...

我沒有打印其余的代碼,因為我只是認為它與手頭的問題無關。 我想知道為什么會出現此問題,以及如何在代碼中修復它,因為稍后在程序中我將需要信息來解決Ax = b。 文本文件為我提供了增強矩陣[A | b],因此我使用LU分解求解x。 我認為這可能與緩沖區本身有關,但是我對C編程也沒有經驗。

我正在閱讀的文本文件也是這樣寫的...

 3 4
 2 1 1  1
 6 2 1 -7
-2 2 1  7 

這些是我從代碼中得到的結果

  3  4
   2.000      1.000      1.000      1.000

   6.000      2.000      1.000      -7.000

   -2.000      2.000      1.000      7.000

Segmentation fault (core dumped)

感謝您的時間。 :)

更新:我嘗試運行gdb進行調試,但我一直試圖使用文本文件運行核心轉儲。 (例如,。/ hw6 ge1.txt是我要在命令行中鍵入的內容)。 我如何使用文本文件來運行它,因為沒有它它就存在段錯誤。

 c
Continuing.
Expecting two or three arguments
The second argument must be a text file to read from.
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400a4e in m_free (M=0x0) at apmatrix.c:32
32              free(M->mat[0]); /* free data */
1) compile your program with -ggdb parameter
2) link your program with the -ggdb parameter

這將導致包含gdb調試器可用的最大調試信息量的可執行文件。

3) assure the source code is visible from where the program is to be run
   (in the same directory works very well)
4) at the command line: gdb yourProgramName
5) from within gdb enter the following commands
    br main    <-- sets breakpoint at main
    run        <-- execution program until breakpoint reached
    c          <-- continue execution
6) when the program crashes, enter the following commands
   bt          <-- display a back trace
7) examine the back trace to see which line your program crashed on.

8) quit y      <-- to exit the program
9) let us know which line that is.  
   paste/edit the line into your question
   as a line number would be meaningless to us.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM