如何在 C 中通过逐行读取来读取或存储整数?
我正在尝试读取数字行并对它们进行一些计算。但是,我需要以某种方式将它们逐行分开,但我不知道该怎么做。这是我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的二维数组想法可能是正确的,特别是如果您想将数据点分开。使用
fgets
将每一行作为字符串引入,然后使用带有sscanf
的循环将各个数字解析为数组的单行。可以使用strtol
等函数代替sscanf
步骤来直接获取数字。例如*(您需要调整缓冲区的大小和数组的尺寸,但对于您提供的数据文件):
(针对标准输入方法进行的编辑)
该 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 withsscanf
to parse out the individual numbers into a single row of the array. A function likestrtol
can be used in place of thesscanf
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)
The exe is named
rowdata2
and the text file isrowdata.txt
, so I ran it asrowdata2 < rowdata.txt
and got the correct results.[*] It won't win any beauty contests