簡體   English   中英

C使用動態分配從文件讀取行和空格

[英]C reading lines and spaces from a file using dynamic allocation

我試圖通過使用逆向工程過程從文件中讀取矩陣。 我嘗試做的第一部分是使用malloc或calloc創建文件。 我使用malloc是因為提示用戶輸入的行數或列數。 我還制作了要在屏幕上打印出的最終矩陣和用戶定義的文件名。

我現在遇到的問題是我能夠在第二部分上獲取文件名,但是一旦找到文件名,程序將凍結並停止執行下一個操作。

這是我的代碼如下:

//preprocessors

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

//macros

#define STRING_MAX 50

//function prototypes

void printDiagonal (double ** m, int r, int c);

int main (int argc, char * argv [ ])
{
FILE * ptrIn;
FILE * ptrOut;

char inFileName [STRING_MAX];
char outFileName [STRING_MAX];

int row;
int col;

int r;
int c_temp;
int c;

char spaces;
char lines;

int i, j, k;

int ctrSpaces = 0;
int ctrLines = 0;
int ctrH = 0;
int ctrV = 0;

double matrixVals;
double ** matrixIn;
double ** matrixOut;

//get a file name first

printf ("\nEnter a file name >> ");
scanf ("%s", inFileName);

//open the file

ptrIn = fopen (inFileName, "w");

//get matrix dimensions

printf ("\nEnter the row and column sizes >> ");
scanf ("%d", &row);
scanf ("%d", &col);
printf ("\nRows >> %d\nCols >> %d\n\n", row, col);

matrixIn = (double **) malloc (row * sizeof (double *));
if (matrixIn == NULL)
{
    printf ("\nSORRY, NOT ENOUGH MEMORY!\n\n");
    exit (0);
}
for (i = 0; i < row; i ++ )
{
    matrixIn [i] = (double *) malloc (col * sizeof (double));
    if (matrixIn [i] == NULL)
    {
        printf ("\nSORRY, NOT ENOUGH MEMORY!\n\n");
        exit (0);
    }
}

//input the values into the matrix

printf ("\nEnter the values into the matrix >> \n\n");

for (i = 0; i < row; i ++)
{
    for (j = 0; j < col; j ++)
    {
        printf ("\n\nEnter [%d][%d] of [%d][%d] >> ", (i + 1), (j + 1), row, col);
        scanf ("%lf", &matrixIn [i] [j]);
    }
}

//print out the matrix

printf ("\nElements in the matrix >> \n\n");
for (i = 0; i < row; i ++)
{
    for (j = 0; j < col; j ++)
    {
        printf ("%.3lf\t", matrixIn [i][j]);
        fprintf (ptrIn, "%.3lf\t", matrixIn [i][j]);
    }
    printf ("\n");
    fprintf (ptrIn, "\n");
}
printf ("\n");
fprintf (ptrIn, "\n");

for (i = 0; i < row; i ++)
{
    free (matrixIn [i]);
}
free (matrixIn);

fclose (ptrIn);

printf ("\n-----------------------------------------------------------------------------------------------------------------\n\n");

/**------------------------------------------------------------------------------------------------------------------------------------------------------------------------------**/

//now retrieve the file by first getting user input for file name and check that the file exists

FILE_NAME:

printf ("\nEnter the name of file to access >> ");
scanf ("%s", outFileName);
printf ("\nThe name of the file is >> %s\n\n", outFileName);

//now open the file

ptrOut = fopen (outFileName, "r");

//check that the file exists

if (ptrOut == NULL)
{
    printf ("\nSORRY, THE FILE DOES NOT EXIST. PLEASE ENTER A VALID FILE NAME.\n\n");
    goto FILE_NAME;
}

//get the number of rows and columns

for (lines = fgetc (ptrOut); lines != EOF; lines == fgetc (ptrOut))
{
    if (lines == '\t' || lines == ' ')
    {
        ctrLines ++;
        if (lines == EOF)
        {
            break;
        }
    }
}

c_temp = ctrLines;

for (spaces = fgetc (ptrOut); spaces != EOF; spaces = fgetc(ptrOut))
{
    if (spaces == '\n')
    {
        ctrSpaces ++;
        if (spaces == EOF)
        {
            break;
        }
    }
}

r = ctrSpaces;

//total number of rows is:

c = c_temp / r;

printf ("\nNumber of rows >> %d\n\nNumber of columns >> %d\n\n", r, c);

matrixOut = (double **) malloc (r * sizeof (double *));
if (matrixOut == NULL)
{
    printf ("\nSorry, not enough memory!\n\n");
    exit (0);
}

for (i = 0; i < r; i ++)
{
    matrixOut [i] = (double *) malloc (c * sizeof (double));
    if (matrixOut [i] == NULL)
    {
        printf ("\nSorry, not enough memory!\n\n");
        exit (0);
    }
}

for (i = 0; i < r; i ++)
{
    free (matrixOut [i]);
}
free(matrixOut);

fclose (ptrOut);

//reopen the file and get the information from the file

ptrOut = fopen (outFileName, "r");

matrixOut = (double **) malloc (c * sizeof (double *));

for (i = 0; i < r; i++)
{
    matrixOut [i] = (double *) malloc (c * sizeof (double));
}

printf ("\nMatrix from File %s of dimensions %d X %d >>\n\n", outFileName, r, c);

for (i = 0; i < r; i ++)
{
    for (j = 0; j < c; j ++)
    {
        fscanf (ptrOut, "%lf", &matrixVals);
        matrixOut [i] [j] = matrixVals;
        printf ("%.3lf\t", matrixOut [i] [j]);
    }
    printf ("\n");
}
printf ("\n");

for (i = 0; i < r; i ++)
{
    free (matrixOut [i]);
}
free(matrixOut);

fclose (ptrOut);

return (0);
}

這是我的輸出: https : //1drv.ms/u/s ! AqM2tvkVzTumh58mcliiAUKD9Uu7Ew

為了澄清我的問題。

謝謝

我終於感謝了user3629249。 我只需要分開讀取行號和讀取行號這兩個操作。 對於行號,我必須從總行數中減去1,因為它在EOL之后增加了\\0

這是我的最終代碼(以及我最初沒有提到的用於打印方陣的主要和“次要”對角線的功能)。

 //preprocessors

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

//macros

#define STRING_MAX 50

//function prototypes

void printMajorDiagonal (double ** m, int r, int c);
void printMinorDiagonal (double ** m, int r, int c);

int main (int argc, char * argv [ ])
{
FILE * ptrIn;
FILE * ptrOut;

char inFileName [STRING_MAX];
char outFileName [STRING_MAX];

int row;
int col;

int r;
int c_temp;
int c;

char spaces;
char lines;
char ch;

int i, j, k;

int ctrSpaces = 0;
int ctrLines = 0;
int ctrH = 0;
int ctrV = 0;
int charCount = 0;
int lineCount = 0;

double matrixVals;
double ** matrixIn;
double ** matrixOut;

//get a file name first

printf ("\nEnter a file name >> ");
scanf ("%s", inFileName);

//open the file

ptrIn = fopen (inFileName, "w");

//get matrix dimensions

printf ("\nEnter the row and column sizes >> ");
scanf ("%d", &row);
scanf ("%d", &col);
printf ("\nRows >> %d\nCols >> %d\n\n", row, col);

matrixIn = (double **) malloc (row * sizeof (double *));
if (matrixIn == NULL)
{
    printf ("\nSORRY, NOT ENOUGH MEMORY!\n\n");
    exit (0);
}
for (i = 0; i < row; i ++ )
{
    matrixIn [i] = (double *) malloc (col * sizeof (double));
    if (matrixIn [i] == NULL)
    {
        printf ("\nSORRY, NOT ENOUGH MEMORY!\n\n");
        exit (0);
    }
}

//input the values into the matrix

printf ("\nEnter the values into the matrix >> \n\n");

for (i = 0; i < row; i ++)
{
    for (j = 0; j < col; j ++)
    {
        printf ("\n\nEnter [%d][%d] of [%d][%d] >> ", (i + 1), (j + 1), row, col);
        scanf ("%lf", &matrixIn [i] [j]);
    }
}

//print out the matrix

printf ("\nElements in the matrix >> \n\n");
for (i = 0; i < row; i ++)
{
    for (j = 0; j < col; j ++)
    {
        printf ("%.3lf\t", matrixIn [i][j]);
        fprintf (ptrIn, "%.3lf\t", matrixIn [i][j]);
    }
    printf ("\n");
    fprintf (ptrIn, "\n");
}
printf ("\n");
fprintf (ptrIn, "\n");

for (i = 0; i < row; i ++)
{
    free (matrixIn [i]);
}
free (matrixIn);

fclose (ptrIn);

printf ("\n-----------------------------------------------------------------------------------------------------------------\n\n");

/**------------------------------------------------------------------------------------------------------------------------------------------------------------------------------**/

//now retrieve the file by first getting user input for file name and check that the file exists

FILE_CHECK:

printf ("\nEnter the name of file to access >> " );
scanf ("%s", outFileName);
printf ("\nThe name of the file is >> %s\n\n", outFileName);

//now open the file

ptrOut = fopen (outFileName, "r");
if (ptrOut == NULL)
{
    printf ("Sorry, the file does not exist. Re-enter another file-name.\n\n");
    goto FILE_CHECK;
}

//read lines first - total number of lines == total number of rows

for (lines = getc (ptrOut); lines != EOF; lines = getc (ptrOut))
{
    if (lines == '\n')
    {
        ctrLines ++ ;
    }
}

r = ctrLines - 1;

matrixOut = (double **) malloc (r * sizeof (double *));
if (matrixOut == NULL)
{
    printf ("\nSorry, not enough memory!\n\n");
    exit (0);
}

free (matrixOut);

printf ("\nRows >> %d\n\n", r);

/*******************************************************************************************************************************************/

//open the file again to count columns

ptrOut = fopen (outFileName, "r");

for (spaces = getc (ptrOut); spaces != EOF; spaces = getc (ptrOut))
{
    if (spaces == ' ' || spaces == '\t' || spaces == '\b')
    {
        ctrSpaces ++ ;
    }
}

c_temp = ctrSpaces;

c = c_temp / r;

printf ("\nCols >> %d\n\n", c);

for (i = 0; i < r; i ++)
{
    matrixOut [i] = (double *) malloc (c * sizeof (double));
    if (matrixOut [i] == NULL)
    {
        printf ("\nSorry, not enough memory!\n\n");
        exit (0);
    }
}

for (i = 0; i < r; i ++ )
{
    free (matrixOut [i]);
}
fclose (ptrOut);

//reopen the file again to read the matrix elements line by line

ptrOut = fopen (outFileName, "r");

matrixOut = (double **) malloc (r * sizeof (double *));

for (i = 0; i < r; i ++)
{
    matrixOut [i] = (double *) malloc (c * sizeof (double));
}

//print out the matrix here

printf ("\nThe matrix in the file %s of size %d x %d.\n\n", outFileName, r, c);

for (i = 0; i < r; i ++)
{
    for (j = 0; j < c; j++)
    {
        fscanf (ptrOut, "%lf", &matrixVals);
        matrixOut [i] [j] = matrixVals;
        printf ("%.3lf\t", matrixOut [i] [j]);
    }
    printf ("\n");
}
printf ("\n");

printMajorDiagonal (matrixOut, r, c);
printMinorDiagonal (matrixOut, r, c);

for (i = 0; i < r; i ++)
{
    free (matrixOut [i]);
}
free (matrixOut);

fclose (ptrOut);

return (0);
}

//function definitions

void printMajorDiagonal (double ** m, int r, int c)
{
//prints out the major diagonal
int i, j;
if (r == c)
{
    printf ("\nMajor Diagonal >> \n\n");
    for (i = 0; i < r; i ++)
    {
        printf ("%.3lf\t", m [i] [i]);
    }
    printf ("\n");
}
else if (r != c)
{
    printf ("\nThe major diagonal cannot be printed since the given matrix is not a square matrix.\n\n");
}
}

void printMinorDiagonal (double ** m, int r, int c)
{
//prints out the opposite "minor" diagonal
int i, j;
if (r == c)
{
    printf ("\nMinor Diagonal >> \n\n");
    for (i = 0; i < r; i ++)
    {
        printf ("%.3lf\t", m [r - i - 1] [i]);
    }
    printf ("\n");
}
else if (r != c)
{
    printf ("\nThe major diagonal cannot be printed since the given matrix is not a square matrix.\n\n");
}
}

再次感謝所有參與幫助我的人。 我的錯誤是在第一次進行行計數迭代后無法重新啟動文件。

非常感謝StackOverFlow!

暫無
暫無

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

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