c中for循环的分段错误

c中for循环的分段错误,c,for-loop,segmentation-fault,scanf,ram,C,For Loop,Segmentation Fault,Scanf,Ram,我有一个文本文件,其中包含如下排列的数字: 0.200 1.3300 1.2555 0.201 1.3300 1.2620 0.202 1.3300 1.2685 0.203 1.3300 1.2751 0.204 1.3248 1.2817 . . . . 0.899 0.17380 5.7153 0.900 0.17435 5.7227 这个文本文件中有700行。我编写了一个C程序,它将从另一个名为“fc.txt”的文本文件中读取行号

我有一个文本文件,其中包含如下排列的数字:

0.200   1.3300   1.2555
0.201   1.3300   1.2620
0.202   1.3300   1.2685
0.203   1.3300   1.2751
0.204   1.3248   1.2817
.
.
.
.
0.899   0.17380   5.7153
0.900   0.17435   5.7227
这个文本文件中有700行。我编写了一个C程序,它将从另一个名为“fc.txt”的文本文件中读取行号,然后将该行的三个不同数字保存到三个不同的文本文件中。该计划是:

#include<stdio.h>
int main()
{
    int l, i = 0, j;
    float a[100][100];
    float *p;
    FILE *fp;
    fp = fopen("fc.txt", "r");
    fscanf(fp, "%d", &l);
    fclose(fp);
    fp = fopen("Au.txt", "r");
    for (i = 0; i <= l; i++)
    {
        for (j = 0; j <= 2; j++)
        {
            fscanf(fp, "%f", &a[i][j]);
            if (i == l)
            {
                p = &a[i][j];
                p++;
            }
        }
    }
    fclose(fp);
    fp = fopen("i_ri.txt", "w");
    fprintf(fp, "%f", *(p - 1));
    fclose(fp);
    fp = fopen("r_ri.txt", "w");
    fprintf(fp, "%f", *(p - 2));
    fclose(fp);
    fp = fopen("w.txt", "w");
    fprintf(fp, "%f", *(p - 3));
    fclose(fp);
    return 0;
}
#包括
int main()
{
int l,i=0,j;
浮动a[100][100];
浮动*p;
文件*fp;
fp=fopen(“fc.txt”,“r”);
fscanf(fp、%d、&l);
fclose(fp);
fp=fopen(“Au.txt”,“r”);

对于(i=0;i我无法理解您的目的。我猜您想读取特定行的数据

未经测试的演示:

#include<stdio.h>

int main(int argc, char *argv[])
{
    char *src;
    int nr;
    int i;
    float a[3];
    FILE *fp;

    if (argc != 3)
    {
        printf("Usage: %s <file> <line_number>\n", argv[0]);
        return -1;
    }

    src = argv[1];
    nr = atoi(argv[2]);

    if ((fp = fopen(src, "r")) == NULL)
    {
        printf("Fail to open file %s\n", src);
        return -1;
    }

    for (i = 0; i < nr; i++)
    {
        if (fscanf(fp, "%f %f %f", a, a + 1, a + 2) != 3)
        {
            printf("Unknown format\n");
            return -1;
        }
    }

    fclose(fp);

    // Now float arrays at line nr is stored at a[]
    printf("Your expected float array: \n");
    printf("[1] %f\n[2] %f\n[3] %f\n", a[0], a[1], a[2]);

    // Do your own job with a[]

    return 0;
}
#包括
int main(int argc,char*argv[])
{
char*src;
国际天然气公司;
int i;
浮动a[3];
文件*fp;
如果(argc!=3)
{
printf(“用法:%s\n”,argv[0]);
返回-1;
}
src=argv[1];
nr=atoi(argv[2]);
如果((fp=fopen(src,“r”))==NULL)
{
printf(“无法打开文件%s\n”,src);
返回-1;
}
对于(i=0;i
你能正确缩进你的代码吗?你只能读取6个值……文件打开可以吗?你想将700个元素写入一个100个元素的数组中。这怎么合适?请回答你的问题,并显示每个输出文件的前2-3行。另外,请说明包含700行的文件名是什么
float a[100][100];
==>
浮点a[700][3];
你真的不应该存储你不需要的行。你只需要在一个100的数组中读取和使用3个值。内存浪费巨大,整体速度较慢。