- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是读取此 csv 文件的骨架代码的一部分:
year,month,day,location,mintemp,maxtemp
2015,4,28,18,6.7,12.9
2015,4,28,22,12.7,19.1
2015,4,29,18,7.6,15.3
2015,4,29,22,13.4,21.9
2015,4,30,18,7.3,21.8
2015,4,30,22,13.2,23.2
2015,5,1,18,9.4,15.9
2015,5,1,22,16.1,27.2
2015,5,2,18,8.7,16.3
2015,5,2,22,14.2,21.4
此函数将文件作为字符串读取,并以某种方式将其分开,并将其放入名为“D”、类型为 csv_t 的结构中。
结构及其他:
typedef char input_line_t[LINELEN+1];
typedef struct {
input_line_t labelstring;
char *labs[MAXCOLS+1];
int nrows;
int ncols;
double vals[MAXROWS][MAXCOLS];
} csv_t;
功能:
void
read_csv_file(char *fname, csv_t *D) {
FILE *fp; /* used to read from a named file */
input_line_t line;
int cols=0, rows=0, bytes=0;
int c, i, j, chr, ncommas, empties=0;
double x;
double nan = 0.0/0.0;
/* first argument on commandline should the data file name */
if (fname==NULL) {
/* and it wasn't there... */
printf("No csv file specified on commandline\n");
exit(EXIT_FAILURE);
}
/* try and open the named file for reading */
if ((fp=fopen(fname,"r")) == NULL) {
printf("Error: unable to open %s\n", fname);
exit(EXIT_FAILURE);
}
/* file is open, can now use fp to access CSV data,
start by reading the bytes of the header row */
while ((c=getc(fp)) != '\n') {
D->labelstring[bytes++] = c;
}
D->labelstring[bytes] = '\0';
/* now process line again, breaking in to separate labels by
replacing commas by nulls, and tracking the start of each of
the column headings */
D->labs[cols++] = D->labelstring;
for (i=1; i<bytes; i++) {
if (D->labelstring[i]==COMMA) {
D->labelstring[i] = '\0';
D->labs[cols++] = D->labelstring+i+1;
}
if (cols>MAXCOLS && i<bytes) {
printf("Too many columns, limit is %d\n",
MAXCOLS);
exit(EXIT_FAILURE);
}
}
D->labs[cols] = NULL;
/* ok, that's the labels sorted, now for the data */
while ((chr=getc(fp)) != EOF) {
/* there is another row, because first character of it
just got read, next step is to get the rest of them */
i = 0;
line[i++] = chr;
ncommas = (chr==COMMA) ;
while (((chr=getc(fp))!=EOF) && (chr!='\n')) {
line[i++] = chr;
ncommas += (chr==COMMA) ;
}
line[i] = '\0';
if (ncommas!=cols-1) {
printf("Data input error line %d\n", rows+2);
exit(EXIT_FAILURE);
}
/* then process the line from the right end */
j = i-1;
for (c=cols-1; c>=0; c--) {
/* look for next previous comma */
while (j>=0 && line[j]!=COMMA) {
j--;
}
/* access the value */
if (sscanf(line+j+1, "%lf", &x) == 1) {
D->vals[rows][c] = x;
} else {
D->vals[rows][c] = nan;
empties++;
}
/* mark the new end of the string */
line[j--] = '\0';
}
rows++;
/* check to make sure don't overflow array */
if (rows==MAXROWS) {
/* time to stop reading data */
printf("Too many rows, truncated at %d\n", MAXROWS);
break;
}
/* if not full, go round and see if there is another data row */
}
/* either input has all been read or array is full */
printf("file %s:\n %d columns and %d rows of data\n",
fname, cols, rows);
if (empties) {
printf(" %d entries were empty or non-numeric\n",
empties);
}
/* finish building the structure */
D->nrows = rows;
D->ncols = cols;
return;
}
我基本上了解发生了什么,除了从这一点开始,特别是这一点:
D->labs[cols++] = D->labelstring;
for (i=1; i<bytes; i++) {
if (D->labelstring[i]==COMMA/*=','*/) {
D->labelstring[i] = '\0';
D->labs[cols++] = D->labelstring+i+1;
}
最后一段代码非常困惑。 labelstring 是一个字符串,但可以添加数字。它有什么作用?这如何给出列数。如果我明白了这一点,我就能理解整个代码。感谢帮助。非常感谢!!
最佳答案
其实很简单。假设您有 labelstring
包含字符串 "Foo"
,那么它在内存中看起来像这样
+-----+-----+-----+------+| 'F' | 'o' | 'o' | '\0' |+-----+-----+-----+------+^|+-------------+| labelstring |+-------------+
换句话说,labelstring
指向字符串中的第一个字符。
如果您执行labelstring + 1
,您将得到*第二个字符:
+-----+-----+-----+------+| 'F' | 'o' | 'o' | '\0' |+-----+-----+-----+------+ ^ | +-----------------+ | labelstring + 1 | +-----------------+
这称为指针算术。
是的,您也可以在数组上使用它,因为数组会衰减为指向其第一个元素的指针。
了解了上述内容后,也很容易理解 array[X]
(对于任何数组或指针 array
以及对于任何 X
) 与 *(array + X)
相同。
真正让您大吃一惊的是,感谢 commutative property的addition ,最后一位表示 *(array + X)
等于 *(X + array)
这导致 array[X]
相等到X[数组]
。
关于c - 骨架代码 : character pointers struggles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30152457/
我是一名新手 Haskell 程序员,我正在尝试编写一些 Haskell cgi,它将从 MySQL DB 读取并输出 JSON。我能够生成正确的 JSON,但无法正确获取数据类型以便能够正确输出 J
想象一下,您的妻子让您去买一些坎皮恩花。因为它是一种稀有的花,你不确定你是否可以在隔壁的花店找到它。所以你必须检查镇上的每一家花店,并向他们索要野营花。但事实是有些花店有公用电话,你可以打电话问问。其
这个问题已经有答案了: How does JavaScript .prototype work? (26 个回答) 已关闭 7 年前。 我以为我已经弄清楚了 JavaScript 的继承问题,直到这个
这是读取此 csv 文件的骨架代码的一部分: year,month,day,location,mintemp,maxtemp 2015,4,28,18,6.7,12.9 2015,4,28,22,12
首先,非常感谢 StackOverflow 社区。我是 C# 的新手,这个小组多次将我从火中拉出来! 问题:我在使用 C# FindAll 方法时遇到了一些问题。具体来说,我不能让它工作,我知道是我.
我在一些模板代码中遇到了一些烦人的常量正确性问题,最终归结为以下观察结果:出于某种原因,给定一个 STL-ish 容器类型 T,const typename T::pointer 实际上似乎并不产生常
我只是想使用 ffmpeg 将静音音轨添加到视频文件中: ffmpeg.exe -i video.mkv -i anullsrc -c:v copy -c:a aac -shortest test.m
我想要一个管道,它可以从一个名为“Input”的文件夹中接受这样的输入。文件的格式类似于“gene_KOs1.tsv gene_KOs2.tsv Mutations1.tsv Mutations2.t
我正在尝试为类型之间的转换实现一个接口(interface),但自 typing.Type 以来我一直在努力使其保持一致。是协变的 U = TypeVar('U') class Into(Protoc
我正在尝试更好地理解 MVC。我找不到太多我真正理解并且可以重新设计以更好地理解内部运作的内容。 据我所知,模型是数据(带有对象或其他内容的 JavaScript), View 是 HTML, Con
我正在处理一个联系表单,但我很难确定发送按钮的宽度。我希望它与整个表单具有相同的宽度,但它不起作用。 我错过了什么? 直播:http://baskra.com/teste/teste.html CSS
这里是第一次海报。抱歉,如果这是一个明显的修复,但我对 Nodejs 和一般编程世界非常陌生。 我目前正在尝试创建一个 Discord 机器人,允许任何用户使用 !vote 命令发起“喜欢它或讨厌它”
我是 Berkeley DB Java Ed 的新手。我正在尝试使用它。我有一个非常简单的问题。我无法为数据库创建环境。我正在使用这段代码: EnvironmentConfig envConf = n
我正在设计一种命令式语言,但在实现循环功能时遇到了问题。循环将采用以下形式: Do (n Loop Body 我可以正确地解析循环,但是在评估循环时,会出现两个问题。第一个是我不
我已经编程相当长的时间了,但是整个数据库的事情已经完全被我忽视了。我现在正在尝试了解其中的一些内容,但我正在为如何建立关系而苦苦挣扎。 目的: 我在一家摄像店工作,因此我们有大量需要用元数据标记的文件
我开发了一个应用程序,它使用特定的蓝牙模块(HC-06)与其通信,但首先它需要与之配对,它使用以下代码来实现…… BluetoothAdapter mBluetoothAdapter = Blueto
我有我的主数据库,其中有一个表,产品(稍后会有更多,这只是开发)。 操作系统:CentOS 6.4 我已将其设置为使用此复制的主服务器: 在/etc/my.cnf 中: server-id = 1 b
我有一个场景,我需要在一列中验证嵌套实体。实际上它是这样的。我有两个实体之间的映射,例如用户和公司实体。我在用户表中有一个公司表的外键。在我的创建用户表单中,我有一个用于选择公司的下拉列表,当用户提交
我从玩 Haskell 中休息了很长时间,现在我开始重新投入其中。我肯定还在学习这门语言。我已经意识到,在编写 Haskell 时总是让我紧张/不舒服的一件事是,我对如何制作既惯用的算法没有很强的把握
I have tried numerous ways to install PyTorch:我尝试了许多安装PyTorch的方法: !pip install torch --index
我是一名优秀的程序员,十分优秀!