如何在 C 中通过逐行读取来读取或存储整数?

发布于 2024-11-14 05:58:06 字数 1470 浏览 2 评论 0原文

我正在尝试读取数字行并对它们进行一些计算。但是,我需要以某种方式将它们逐行分开,但我不知道该怎么做。这是我的代码:

int main()
{
    int infor[1024]; //2-d array perhaps??
    int n, i;

    i=0;

    int imgWidth, imgHeight, safeRegionStart, safeRegionWidth;
    FILE *fp;

    fp = stdin;

    while (!feof(fp))
    {
        fscanf(fp, "%d", &infor[i++]);
    }
}

输入看起来像这样:

4 3 1 2 -16777216 -16711936 -65536 -16777216 -1 -65536 -65536 -16711936 -16777216 -65536 -16711936 -16777216     
3 4 1 1 -16777216 -16711936 -1 -1 -65536 -16777216 -16777216 -65536 -1 -1 -65536 -16711936 

谁能解释如何从一行移动到另一行?


编辑:

int main()
{
    FILE * fp = stdin;
    char buffer[1024];
    long arr[2][16];

    int i = 0,
        j = 0;

    char * pEnd;

    while(fgets(buffer, sizeof(buffer), fp))
    {
        j = 0;
        if(buffer[0] == '\n')
            continue;

        pEnd = buffer;
        while(*pEnd != '\0')
        {
            arr[i][j++]=strtol(pEnd,&pEnd,10);
        }

        i++;
    }

    int imgWidth,
        imgHeight,
        safeRegionStart,
        safeRegionWidth;

    imgWidth = arr[1][0];
    imgHeight = arr[1][1];
    safeRegionStart = arr[1][2];
    safeRegionWidth = arr[1][3];

    printf("Value of i is %d\n", i);
    printf("%d %d %d %d ",
           imgWidth,
           imgHeight,
           safeRegionStart,
           safeRegionWidth);

    return 0;
}

I'm trying to read line of numbers and do some calculations on them. However, I need to them to be separated line by line somehow, but I can't figure out how to do that. Here's my code:

int main()
{
    int infor[1024]; //2-d array perhaps??
    int n, i;

    i=0;

    int imgWidth, imgHeight, safeRegionStart, safeRegionWidth;
    FILE *fp;

    fp = stdin;

    while (!feof(fp))
    {
        fscanf(fp, "%d", &infor[i++]);
    }
}

The input looks something like this:

4 3 1 2 -16777216 -16711936 -65536 -16777216 -1 -65536 -65536 -16711936 -16777216 -65536 -16711936 -16777216     
3 4 1 1 -16777216 -16711936 -1 -1 -65536 -16777216 -16777216 -65536 -1 -1 -65536 -16711936 

Can anyone explain how to move from line to line?


EDIT:

int main()
{
    FILE * fp = stdin;
    char buffer[1024];
    long arr[2][16];

    int i = 0,
        j = 0;

    char * pEnd;

    while(fgets(buffer, sizeof(buffer), fp))
    {
        j = 0;
        if(buffer[0] == '\n')
            continue;

        pEnd = buffer;
        while(*pEnd != '\0')
        {
            arr[i][j++]=strtol(pEnd,&pEnd,10);
        }

        i++;
    }

    int imgWidth,
        imgHeight,
        safeRegionStart,
        safeRegionWidth;

    imgWidth = arr[1][0];
    imgHeight = arr[1][1];
    safeRegionStart = arr[1][2];
    safeRegionWidth = arr[1][3];

    printf("Value of i is %d\n", i);
    printf("%d %d %d %d ",
           imgWidth,
           imgHeight,
           safeRegionStart,
           safeRegionWidth);

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱殇璃 2024-11-21 05:58:06

我认为您的二维数组想法可能是正确的,特别是如果您想将数据点分开。使用 fgets 将每一行作为字符串引入,然后使用带有 sscanf 的循环将各个数字解析为数组的单行。可以使用 strtol 等函数代替 sscanf 步骤来直接获取数字。

例如*(您需要调整缓冲区的大小和数组的尺寸,但对于您提供的数据文件):
(针对标准输入方法进行的编辑)

 #include <stdio.h>

int main(){

    char buffer[1024];
    long arr[2][16];
    int i = 0,j=0;
    char * pEnd;
    FILE *fp = stdin;
    while(fgets(buffer,sizeof(buffer),fp))
    {
        j=0;
        if(buffer[0]=='\n')
            continue;

        pEnd = buffer;
        while(*pEnd !='\0')
        {
            arr[i][j++]=strtol(pEnd,&pEnd,10);

        }

        i++;
    }

fclose(fp);
printf("arr[0][0]=%d  arr[0][1]=%d  arr[0][2]=%d\n",arr[0][0],arr[0][1],arr[0][2]);
printf("arr[1][0]=%d  arr[1][1]=%d  arr[1][2]=%d\n",arr[1][0],arr[1][1],arr[1][2]);


}

该 exe 名为 rowdata2,文本文件为 rowdata.txt,因此我将其运行为 rowdata2 rowdata2 rowdata2 rowdata2。 rowdata.txt 并得到了正确的结果。

[*] 它不会赢得任何选美比赛

I think your 2D array idea is probably correct, especially if you want to keep the data points separate. Use fgets to bring in each line as a string, then use a loop with sscanf to parse out the individual numbers into a single row of the array. A function like strtol can be used in place of the sscanf step to get the numbers directly.

For example* (you'll need to adjust the size of the buffer and the dimensions of the array, but for the data file you gave):
(edits made for the stdin approach)

 #include <stdio.h>

int main(){

    char buffer[1024];
    long arr[2][16];
    int i = 0,j=0;
    char * pEnd;
    FILE *fp = stdin;
    while(fgets(buffer,sizeof(buffer),fp))
    {
        j=0;
        if(buffer[0]=='\n')
            continue;

        pEnd = buffer;
        while(*pEnd !='\0')
        {
            arr[i][j++]=strtol(pEnd,&pEnd,10);

        }

        i++;
    }

fclose(fp);
printf("arr[0][0]=%d  arr[0][1]=%d  arr[0][2]=%d\n",arr[0][0],arr[0][1],arr[0][2]);
printf("arr[1][0]=%d  arr[1][1]=%d  arr[1][2]=%d\n",arr[1][0],arr[1][1],arr[1][2]);


}

The exe is named rowdata2 and the text file is rowdata.txt, so I ran it as rowdata2 < rowdata.txt and got the correct results.

[*] It won't win any beauty contests

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文