C使用动态分配从文件中读取行和空格

C使用动态分配从文件中读取行和空格,c,dynamic-memory-allocation,C,Dynamic Memory Allocation,我试图通过使用反向工程过程从文件中读取矩阵。我尝试做的第一部分是使用malloc或calloc创建一个文件。我使用malloc,因为提示用户输入行数或列数。我还制作了最终的矩阵并打印在屏幕上,还有一个用户定义的文件名 我现在遇到的问题是,我能够获取第二部分的文件名,但是一旦找到文件名,程序就会冻结并停止执行下一个操作 下面是我的代码: //preprocessors #include <stdio.h> #include <stdlib.h> #include <

我试图通过使用反向工程过程从文件中读取矩阵。我尝试做的第一部分是使用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);
}
//预处理器
#包括
#包括
#包括
#包括
//宏
#定义字符串_MAX 50
//功能原型
无效打印对角线(双**m,整数r,整数c);
int main(int argc,char*argv[])
{
文件*ptrIn;
文件*ptrOut;
字符填充名[STRING_MAX];
字符输出文件名[STRING_MAX];
int行;
int col;
INTR;
内部温度;
INTC;
字符空间;
字符线;
int i,j,k;
int=0;
int ctrLines=0;
int ctrH=0;
int ctrV=0;
双矩阵;
双**苦参素;
双**矩阵输出;
//首先获取文件名
printf(“\n输入文件名>>”;
scanf(“%s”,填充名);
//打开文件
ptrIn=fopen(填充名,“w”);
//获取矩阵维数
printf(“\n输入行和列大小>>”;
scanf(“%d”行和第行);
scanf(“%d”列和列);
printf(“\n行>>%d\n列>>%d\n\n”,行,列);
matrixIn=(双**)malloc(行*大小(双*);
if(matrixIn==NULL)
{
printf(“\n内存不足!\n\n”);
出口(0);
}
对于(i=0;i>\n\n”);
对于(i=0;i>”中的[%d][%d],(i+1),(j+1),行,列);
scanf(“%lf”、&matrixIn[i][j]);
}
}
//打印出矩阵
printf(“\n矩阵中的元素>>\n\n”);
对于(i=0;i>”;
scanf(“%s”,outFileName);
printf(“\n文件名>>%s\n\n”,outFileName);
//现在打开文件
ptrOut=fopen(出料口名称,“r”);
//检查文件是否存在
如果(ptrOut==NULL)
{
printf(“\n实际上,该文件不存在。请输入有效的文件名。\n\n”);
转到文件名;
}
//获取行数和列数
对于(行=fgetc(ptrOut);行!=EOF;行==fgetc(ptrOut))
{
如果(行=='\t'| |行=='')
{
ctrLines++;
如果(行==EOF)
{
打破
}
}
}
c_温度=中心线;
对于(空格=fgetc(ptrOut);空格!=EOF;空格=fgetc(ptrOut))
{
如果(空格=='\n')
{
ctrSpaces++;
if(空格==EOF)
{
打破
}
}
}
r=空间;
//总行数为:
c=c_温度/r;
printf(“\n行数>>%d\n\n列数>>%d\n\n”,r,c);
matrixOut=(双**)malloc(r*sizeof(双*);
if(matrixOut==NULL)
{
printf(“\n内存不足!\n\n”);
出口(0);
}
对于(i=0;i>\n\n”,输出文件名,r,c);
对于(i=0;i
这是我的输出: .

为了澄清我的问题


谢谢

多亏了用户3629249,我终于做到了。我只需要将读取行号和读取行号这两个操作分开。对于行号,我必须从总行数中减去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");
}
}
//预处理器
#包括
#包括
#包括
#包括
//宏
#定义字符串_MAX 50
//功能原型
无效打印主对角线(双**m,整数r,整数c);
无效打印格式(双**m,整数r,整数c);
int main(int argc,char*argv[])
{
文件*ptrIn;
文件*ptrOut;
字符填充名[STRING_MAX];
字符输出文件名[STRING_MAX];
int行;
int col;
INTR;
内部温度;
INTC;
字符空间;
字符线;
char ch;
int i,j,k;
int=0;
int ctrLines=0;
int ctrH=0;
int ctrV=0;
int charCount=0;
在里面