在 C (xv6) 中访问 Char 数组的单个字符的问题
Problem with Accessing Individual Characters of a Char Array in C (xv6)
我在访问 C 字符数组中的每个单独字符时遇到问题。
我在程序开始时这样声明了 char 数组:char ddd[512];
作为全局常量。
然后,我使用 int n; n = read(fd, ddd, sizeof(ddd));
将包含一行随机字符(看起来像这样:acdgeud
)的文件读入 char 字符串。 fd
代表文件描述符,值为1。
例如,我想访问 char 字符串的第二个字符,我尝试了 ddd[1]
,因为这就是我在 C++ 中的做法。但是,它给了我第一个字符后的所有内容:cdgeud
.
现在,我怎样才能一次只获得一个字符?希望这很清楚,并提前感谢您的帮助!
更新:刚刚添加了部分代码:
char ddd[512];
void somefunc(int fd) {
char *another = malloc(512*sizeof(ddd));
int n = 0;
while ((n = read(fd, ddd, sizeof(ddd))) > 0) {
if ( n < 0 ) break;
for (int i = 0; i < n; ++i) {
/* I'm trying to copy values in ddd to another one by one */
another[i] = ddd[i]; /* This is not working */
}
}
}
(编辑新答案)
要逐步执行 for
,您必须在复制值后测试空字符。您也可以在“=”赋值后单独休息。
for (int i = 0; i < n && (another[i] = ddd[i]); ++i)
为简单起见,可以使用 strncpy
:
strncpy(another, ddd, sizeof another);
为确保 \0 终止:
while ((n = read(fd, ddd, sizeof(ddd) - 1)) > 0) {
ddd[(sizeof ddd)-1) = '\0'; //确保空终止
}
(原创)</p>
<p>根据您的评论,我只能猜测而不是正确回答。给你一些。</p>
<p><code>char something[2];
char ddd[512];
int n = read(fd, ddd, sizeof(ddd));
printf("c=%c\n", ddd[1]); // print one character (c)
printf("s=%s\n", 1+ddd ); // print cdgeud, and perhaps more
// (if memset(ddd,0,sizeof ddd) happened before read things would be better).
char another[sizeof(ddd)];
ddd[1] = another[1]; // copy whatever is in another[1] into d[1]. Unpredictable as I coded it as another[] is not initialized
printf("s=%s\n", something); // unpredictable...
// if something[0] and [1] not '[=14=]' AND ddd follows in memory (not guaranteed at all)
// then could print those followed by contentsof ddd.
ddd 不能保证被 \0 终止这一事实可能是意外超限的根源。这在没有 memset() 的情况下有效。
n = 读取( fd, ddd, sizeof ddd );
如果 ( n <= 0 ) 中断; // 任何适合检测读取错误的方法。
如果 ( n < sizeof(ddd) ) ddd[n] = '\0';
printf( "ddd=%.*s\n", n, ddd ); // 安全
我在访问 C 字符数组中的每个单独字符时遇到问题。
我在程序开始时这样声明了 char 数组:char ddd[512];
作为全局常量。
然后,我使用 int n; n = read(fd, ddd, sizeof(ddd));
将包含一行随机字符(看起来像这样:acdgeud
)的文件读入 char 字符串。 fd
代表文件描述符,值为1。
例如,我想访问 char 字符串的第二个字符,我尝试了 ddd[1]
,因为这就是我在 C++ 中的做法。但是,它给了我第一个字符后的所有内容:cdgeud
.
现在,我怎样才能一次只获得一个字符?希望这很清楚,并提前感谢您的帮助!
更新:刚刚添加了部分代码:
char ddd[512];
void somefunc(int fd) {
char *another = malloc(512*sizeof(ddd));
int n = 0;
while ((n = read(fd, ddd, sizeof(ddd))) > 0) {
if ( n < 0 ) break;
for (int i = 0; i < n; ++i) {
/* I'm trying to copy values in ddd to another one by one */
another[i] = ddd[i]; /* This is not working */
}
}
}
(编辑新答案)
要逐步执行 for
,您必须在复制值后测试空字符。您也可以在“=”赋值后单独休息。
for (int i = 0; i < n && (another[i] = ddd[i]); ++i)
为简单起见,可以使用 strncpy
:
strncpy(another, ddd, sizeof another);
为确保 \0 终止:
while ((n = read(fd, ddd, sizeof(ddd) - 1)) > 0) {
ddd[(sizeof ddd)-1) = '\0'; //确保空终止
}
(原创)</p>
<p>根据您的评论,我只能猜测而不是正确回答。给你一些。</p>
<p><code>char something[2];
char ddd[512];
int n = read(fd, ddd, sizeof(ddd));
printf("c=%c\n", ddd[1]); // print one character (c)
printf("s=%s\n", 1+ddd ); // print cdgeud, and perhaps more
// (if memset(ddd,0,sizeof ddd) happened before read things would be better).
char another[sizeof(ddd)];
ddd[1] = another[1]; // copy whatever is in another[1] into d[1]. Unpredictable as I coded it as another[] is not initialized
printf("s=%s\n", something); // unpredictable...
// if something[0] and [1] not '[=14=]' AND ddd follows in memory (not guaranteed at all)
// then could print those followed by contentsof ddd.
ddd 不能保证被 \0 终止这一事实可能是意外超限的根源。这在没有 memset() 的情况下有效。
n = 读取( fd, ddd, sizeof ddd ); 如果 ( n <= 0 ) 中断; // 任何适合检测读取错误的方法。 如果 ( n < sizeof(ddd) ) ddd[n] = '\0'; printf( "ddd=%.*s\n", n, ddd ); // 安全