Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检测C中的行结束_C_Arrays_Matrix_Eol - Fatal编程技术网

检测C中的行结束

检测C中的行结束,c,arrays,matrix,eol,C,Arrays,Matrix,Eol,这是从文件“F1.txt”中读取矩阵10x10的代码 它工作正常,但输出为: 12 343 34 544 43 32 124 52 212 3 12 343 34 544 43 32 124 52 212 .......... etc.... 我必须检测行尾,使我的输入与F1.txt中的输入相同 12 343 34 544 43 32 124 52 212 3 12 343 34 544 43 32 124 52 212 3 12 343 34 544 43 32 124 52 212 3 1

这是从文件“F1.txt”中读取矩阵10x10的代码

它工作正常,但输出为:

12
343
34
544
43
32
124
52
212
3
12
343
34
544
43
32
124
52
212
..........
etc....
我必须检测行尾,使我的输入与F1.txt中的输入相同

12 343 34 544 43 32 124 52 212 3
12 343 34 544 43 32 124 52 212 3
12 343 34 544 43 32 124 52 212 3
12 343 34 544 43 32 124 52 212 3
12 343 34 544 43 32 124 52 212 3
12 343 34 544 43 32 124 52 212 3
12 343 34 544 43 32 124 52 212 3
12 343 34 544 43 32 124 52 212 3
12 4 34 56 43 32 124 52 212 3
32 343 34 544 43 32 7 52 456 98

.

就您所知,EOL通常是
LF
字符或
CR-LF
字符的组合。在C中,它们分别由
\n
\r\n
表示

一种可能的解决方案是,您可以使用
fgets
一次读取完整的一行(
fgets
将只读取一行)。然后使用
sscanf
strtok
读取该字符串中的整数

如果您知道每行中的整数数,我建议您使用
sscanf

否则,如果一行可以包含任意数量的数字,则可以使用
strtok
(空格)作为定界符


您可以在此处阅读有关这些函数的更多信息:,

按以下方式重写循环

for(i = 0; i < 10; i++)
{
    for(j = 0; j < 10; j++)
    {
        fscanf(fr, "%d",&matrix[i][j]);
        printf("%3d ", matrix[i][j]);
    }
    printf( "\n" );
}
(i=0;i<10;i++)的

{
对于(j=0;j<10;j++)
{
fscanf(fr、%d、&matrix[i][j]);
printf(“%3d”,矩阵[i][j]);
}
printf(“\n”);
}

您正确读取了数据,但打印不正确。您的程序在每个字符后插入
'\n'
,这就是为什么会看到这么多行

按如下方式更改程序以查看预期的输出:

for(i = 0; i < 10; i++)
{
    for(j = 0; j < 10; j++)
    {
        fscanf(fr, "%d",&matrix[i][j]);
        printf("%d ", matrix[i][j]); // <<== Replace \n with a single space
    }
    printf("\n"); // <<== Add this line
}
(i=0;i<10;i++)的

{
对于(j=0;j<10;j++)
{
fscanf(fr、%d、&matrix[i][j]);
printf(“%d”,矩阵[i][j]);//
fscanf(fr,“%d”,矩阵[i][j]);
printf(“%d”,矩阵[i][j]);
如果(j<10-1)
printf(“”);
其他的
printf(“\n”);

或者,如果您只想格式化输入,可以在
i
上的外部循环中写入行尾

for(i = 0; i < 10; i++)
{
    for(j = 0; j < 10; j++)
    {
        fscanf(fr, "%d",&matrix[i][j]);
        printf("%d\t", matrix[i][j]);
    }
    printf("\n");
}
(i=0;i<10;i++)的

{
对于(j=0;j<10;j++)
{
fscanf(fr、%d、&matrix[i][j]);
printf(“%d\t”,矩阵[i][j]);
}
printf(“\n”);
}

int-matrix[10][10]={0.0};
你的意思是
int-matrix[10][10]={0};
,对吧?是的。它实际上是在没有={0.0}的情况下运行的;这个问题没有显示任何研究和努力。只需将
\n
字符替换为``将完成最大的工作。
for(i = 0; i < 10; i++)
{
    for(j = 0; j < 10; j++)
    {
        fscanf(fr, "%d",&matrix[i][j]);
        printf("%d ", matrix[i][j]); // <<== Replace \n with a single space
    }
    printf("\n"); // <<== Add this line
}
fscanf(fr, "%d",&matrix[i][j]);
printf("%d", matrix[i][j]);
if(j < 10-1)
    printf(" ");
else
    printf("\n");
for(i = 0; i < 10; i++)
{
    for(j = 0; j < 10; j++)
    {
        fscanf(fr, "%d",&matrix[i][j]);
        printf("%d\t", matrix[i][j]);
    }
    printf("\n");
}