strncpy 函数产生错误的文件名
strncpy functions produces wrong file names
我是 C 语言的新手,正在编写代码来帮助我进行数据分析。它的一部分打开了预定的文件。
这段代码给我带来了问题,我不明白为什么。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLOGGERS 26
// Declare the input files
char inputfile[];
char inputfile_hum[MAXLOGGERS][8];
// Declare the output files
char newfile[];
char newfile_hum[MAXLOGGERS][8];
int main()
{
int n = 2;
while (n > MAXLOGGERS)
{
printf("n error, n must be < %d: ", MAXLOGGERS);
scanf("%d", &n);
}
// Initialize the input and output file names
strncpy(inputfile_hum[1], "Ahum.csv", 8);
strncpy(inputfile_hum[2], "Bhum.csv", 8);
strncpy(newfile_hum[1], "Ahum.txt", 8);
strncpy(newfile_hum[2], "Bhum.txt", 8);
for (int i = 1; i < n + 1; i++)
{
strncpy(inputfile, inputfile_hum[i], 8);
FILE* file1 = fopen(inputfile, "r");
// Safety check
while (file1 == NULL)
{
printf("\nError: %s == NULL\n", inputfile);
printf("\nPress enter to exit:");
getchar();
return 0;
}
strncpy(newfile, newfile_hum[i], 8);
FILE* file2 = fopen(newfile, "w");
// Safety check
if (file2 == NULL)
{
printf("Error: file2 == NULL\n");
getchar();
return 0;
}
for (int c = fgetc(file1); c != EOF; c = fgetc(file1))
{
fprintf(file2, "%c", c);
}
fclose(file1);
fclose(file2);
}
// system("Ahum.txt");
// system("Bhum.txt");
}
此代码生成两个文件,但不是名称:
Ahum.txt
Bhum.txt
文件名为:
Ahum.txtv
Bhum.txtv
我在 for 循环中使用 strncpy
的原因是因为稍后用户实际上会输入 n。
我至少看到三个问题。
第一个问题是你的字符数组对于你的字符串来说太小了。
"ahum.txt"等就需要取九个字符。 8 个用于实际文本,另外 1 个用于空终止字符。
第二个问题是您将字符数组 "newfile" 和 "inputfile" 声明为空数组。这些还需要是一个能够包含字符串的数字(至少 9 个)。
你很幸运没有因为覆盖程序内存而崩溃 space.
第三个也是最后一个问题是您对 strcpy() 的使用。
strncpy(dest, src, n) 会将 n 个字符从 src 复制到 dest,但如果 n 等于或小于 src 字符串的大小,它不会复制最终的空终止符。
来自 strncpy() 联机帮助页:https://linux.die.net/man/3/strncpy
The strncpy() function ... at most n bytes of src are copied.
Warning: If there is no null byte among the first n bytes of src,
the string placed in dest will not be null-terminated.
通常您想要做的是 "n" 目标缓冲区的大小减去 1 以允许空字符。
例如:
strncpy(目标,源代码,sizeof(目标)- 1); // 假设 dest 是 char array
您的代码有几个问题。
inputfile_hum、newfile_hum 需要比字符串尾部的 '\0' 大一个字符。
char inputfile_hum[MAXLOGGERS][9];
...
char newfile_hum[MAXLOGGERS][9];
strncpy 期望第一个参数是一个 char *
区域,足以容纳预期的结果,因此需要声明 inputfile[] 和 outputfile[]:
字符输入文件[9];
字符输出文件[9];
我是 C 语言的新手,正在编写代码来帮助我进行数据分析。它的一部分打开了预定的文件。
这段代码给我带来了问题,我不明白为什么。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLOGGERS 26
// Declare the input files
char inputfile[];
char inputfile_hum[MAXLOGGERS][8];
// Declare the output files
char newfile[];
char newfile_hum[MAXLOGGERS][8];
int main()
{
int n = 2;
while (n > MAXLOGGERS)
{
printf("n error, n must be < %d: ", MAXLOGGERS);
scanf("%d", &n);
}
// Initialize the input and output file names
strncpy(inputfile_hum[1], "Ahum.csv", 8);
strncpy(inputfile_hum[2], "Bhum.csv", 8);
strncpy(newfile_hum[1], "Ahum.txt", 8);
strncpy(newfile_hum[2], "Bhum.txt", 8);
for (int i = 1; i < n + 1; i++)
{
strncpy(inputfile, inputfile_hum[i], 8);
FILE* file1 = fopen(inputfile, "r");
// Safety check
while (file1 == NULL)
{
printf("\nError: %s == NULL\n", inputfile);
printf("\nPress enter to exit:");
getchar();
return 0;
}
strncpy(newfile, newfile_hum[i], 8);
FILE* file2 = fopen(newfile, "w");
// Safety check
if (file2 == NULL)
{
printf("Error: file2 == NULL\n");
getchar();
return 0;
}
for (int c = fgetc(file1); c != EOF; c = fgetc(file1))
{
fprintf(file2, "%c", c);
}
fclose(file1);
fclose(file2);
}
// system("Ahum.txt");
// system("Bhum.txt");
}
此代码生成两个文件,但不是名称:
Ahum.txt
Bhum.txt
文件名为:
Ahum.txtv
Bhum.txtv
我在 for 循环中使用 strncpy
的原因是因为稍后用户实际上会输入 n。
我至少看到三个问题。
第一个问题是你的字符数组对于你的字符串来说太小了。 "ahum.txt"等就需要取九个字符。 8 个用于实际文本,另外 1 个用于空终止字符。
第二个问题是您将字符数组 "newfile" 和 "inputfile" 声明为空数组。这些还需要是一个能够包含字符串的数字(至少 9 个)。 你很幸运没有因为覆盖程序内存而崩溃 space.
第三个也是最后一个问题是您对 strcpy() 的使用。 strncpy(dest, src, n) 会将 n 个字符从 src 复制到 dest,但如果 n 等于或小于 src 字符串的大小,它不会复制最终的空终止符。
来自 strncpy() 联机帮助页:https://linux.die.net/man/3/strncpy
The strncpy() function ... at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.
通常您想要做的是 "n" 目标缓冲区的大小减去 1 以允许空字符。
例如: strncpy(目标,源代码,sizeof(目标)- 1); // 假设 dest 是 char array
您的代码有几个问题。
inputfile_hum、newfile_hum 需要比字符串尾部的 '\0' 大一个字符。
char inputfile_hum[MAXLOGGERS][9]; ... char newfile_hum[MAXLOGGERS][9];
strncpy 期望第一个参数是一个
char *
区域,足以容纳预期的结果,因此需要声明 inputfile[] 和 outputfile[]:字符输入文件[9]; 字符输出文件[9];