开发者

How to read or store integers, by reading line by line in C?

开发者 https://www.devze.com 2023-03-10 21:32 出处:网络
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:

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,开发者_JAVA百科 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;
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消