在 C 中将 char 数组写入文件时出现分段错误
Segmentation fault when writing char array to file in C
当我 运行 下面的代码时,我在 fprintf(outfile, "%s", inputline[j]);
.
得到一个 "Segmentation fault"
我无法理解错误的原因。我是 C 的新手,有人可以帮我解决这个错误吗?
void test(char *inputline) {
FILE *outfile = fopen("results.txt", "w");
if (!outfile) {
perror("Error while opening file: ");
} else {
for (int j = 0; j < 20; ++j) { // I only want to be write the first 20 characters to the file that is why I have the iteration till only 20 and added [j], is that correct way to do it?
fprintf(outfile, "%s", inputline[j]);
}
}
}
//Function call
...
char inputline[40] = "hello world 123 456"; //passed to the function above
test(inputline);
中的格式说明符 %s
fprintf(outfile, "%s", inputline[j]);
需要一个 char *
变量,但您实际上传递的是 char
(inputline
数组的第 j th 个元素)。
出现段错误的原因是fprintf
试图"access" 传递字符指向的内存位置。而且由于很可能是 无效地址 OS 会抱怨试图访问 space[=39 之外的内存=] 分配给您的应用程序。
您可以通过 char 打印到文件 char,保留 for 循环并使用 %c
格式
for(int j=0; j<20; ++j)
{
fprintf(outfile, "%c", inputline[j]);
}
或打印整个字符串保持 %s
格式,传递整个数组并摆脱 for 循环:
fprintf(outfile, "%s", inputline);
注意:无论如何,第一种情况将写入20个字符。在第二种情况下 "length+1" 个字符,因为字符串终止符 '[=20=]'
.
代码中导致分段错误的错误是您将 char
值 inputline[j]
传递给 printf
作为 %s
参数,它需要一个字符串指针.这有未定义的行为。
要最多写入字符串的前 20 个字符,可以使用 %.20s
作为格式说明符。也不要忘记关闭文件:
void test(const char *inputline) {
FILE *outfile = fopen("results.txt", "w");
if (outfile == NULL) {
perror("Error while opening file: ");
} else {
// print at most 20 bytes from inputline
fprintf(outfile, "%.20s\n", inputline);
fclose(outfile);
}
}
请注意,如果需要,此 maximum 计数可以是具有 %.*s
格式的变量:
int limit = 20;
fprintf(outfile, "%.*s\n", limit, inputline);
当我 运行 下面的代码时,我在 fprintf(outfile, "%s", inputline[j]);
.
我无法理解错误的原因。我是 C 的新手,有人可以帮我解决这个错误吗?
void test(char *inputline) {
FILE *outfile = fopen("results.txt", "w");
if (!outfile) {
perror("Error while opening file: ");
} else {
for (int j = 0; j < 20; ++j) { // I only want to be write the first 20 characters to the file that is why I have the iteration till only 20 and added [j], is that correct way to do it?
fprintf(outfile, "%s", inputline[j]);
}
}
}
//Function call
...
char inputline[40] = "hello world 123 456"; //passed to the function above
test(inputline);
%s
fprintf(outfile, "%s", inputline[j]);
需要一个 char *
变量,但您实际上传递的是 char
(inputline
数组的第 j th 个元素)。
出现段错误的原因是fprintf
试图"access" 传递字符指向的内存位置。而且由于很可能是 无效地址 OS 会抱怨试图访问 space[=39 之外的内存=] 分配给您的应用程序。
您可以通过 char 打印到文件 char,保留 for 循环并使用 %c
格式
for(int j=0; j<20; ++j)
{
fprintf(outfile, "%c", inputline[j]);
}
或打印整个字符串保持 %s
格式,传递整个数组并摆脱 for 循环:
fprintf(outfile, "%s", inputline);
注意:无论如何,第一种情况将写入20个字符。在第二种情况下 "length+1" 个字符,因为字符串终止符 '[=20=]'
.
代码中导致分段错误的错误是您将 char
值 inputline[j]
传递给 printf
作为 %s
参数,它需要一个字符串指针.这有未定义的行为。
要最多写入字符串的前 20 个字符,可以使用 %.20s
作为格式说明符。也不要忘记关闭文件:
void test(const char *inputline) {
FILE *outfile = fopen("results.txt", "w");
if (outfile == NULL) {
perror("Error while opening file: ");
} else {
// print at most 20 bytes from inputline
fprintf(outfile, "%.20s\n", inputline);
fclose(outfile);
}
}
请注意,如果需要,此 maximum 计数可以是具有 %.*s
格式的变量:
int limit = 20;
fprintf(outfile, "%.*s\n", limit, inputline);