当写入 txt 的输入数据大于 16kb 时应用程序崩溃(C 编程)
App crashes when the input data to write on txt is bigger than 16kb (C programming)
我正在尝试制作一个程序,它从 txt 文件(坐标如 x1、x2)获取输入并将其存储在元素上以 SVG 格式写入。我在下面编写的代码实际上有效。问题是当我输入大于 16kb 时,在 svg 文件上写入应用程序崩溃。所以我实际上找不到问题。它应该是关于 "strcat(str_joined, svg[i].xy);" 这个,因为我将这么多字符串连接到一个字符串变量中。
输入示例:
339, 52
339, 52
339, 52
338, 53
338, 53
337, 54
337, 54
337, 54
337, 55
337, 55
336, 56
336, 56
336, 56
336, 57
335, 57
335, 56
334, 56
334, 56
333, 56
332, 56
332, 56
331, 56
331, 56
330, 56
330, 56
329, 56
输出示例:<svg><polyline points='339,52 339,52 339,52 338,53 338,53 337,54 '/></svg>
和代码:我只是放了代码的 generate_svg 部分。
void generate_svg(char *input, char *output)
{
//INPUT OPERATİONS //
FILE *fp;
int array_size = 0, i=0, max_lines = 0; // Line counters
char c; // To store a character read from file to check whether newline or not
char *str_joined = NULL;
// Open the file
fp = fopen(input, "r");
//Count the number of lines for the array size
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n')
array_size = array_size + 1;
fclose(fp);
// read the file into an array of coordinates
Coord *coord = malloc(array_size * sizeof *coord); //preallocation for performance
fp = fopen(input, "r");
while(!feof(fp))
{
//check whether input getting correctly
if(fscanf(fp, "%d, %d", &coord[i].x, &coord[i].y)==2)
i++;
}
fclose(fp);
//OUTPUT OPERATIONS
max_lines = i;
SVG *svg = malloc(array_size * sizeof *svg); // allocate memory
size_t total_length=0, length=0; //total length and length of string
for(i=0; i<max_lines; i++)
{
sprintf(svg[i].xy , "%d,%d ", coord[i].x, coord[i].y);
total_length += strlen(svg[i].xy);
//printf("%s\n", svg[i].xy);
}
str_joined = (char*)malloc(total_length * sizeof *str_joined); // allocate memory for joined strings
str_joined[0] = '[=11=]'; // empty string we can append to
for(i=0; i<max_lines; i++)
{
strcat(str_joined, svg[i].xy);
length = strlen(str_joined);
str_joined[length+1] = '[=11=]'; /* followed by terminator */
}
FILE *fp_out;
fp_out = fopen(output,"w+"); //erase the content and write on it if exists or create the file and write on it
if(fp_out == NULL)
{
printf("Error");
}
else
{
fprintf(fp_out, "<svg><polyline points='%s'/></svg>" , str_joined);
printf("Operation successful.\n");
}
//printf("%s\n", str_joined);
}
因此,我们将不胜感激。提前致谢。
//更新
Header:
//definitions
#define MAX_FILE_NAME 100
#define OUTPUT_FILE_NAME "svg_output.svg"
void generate_svg(char *input, char *output);
//storage for coordinates
typedef struct Coord
{
int x;
int y;
}Coord;
typedef struct SVG
{
char xy[20];
}SVG;
好的,非常感谢您的帮助。我解决了这个问题。我为 C 编译配置了 Visual Studio,并将我的代码标准化为 C99。所以我的代码的最终形状如下,它可以工作。
void generate_svg(char *input, char *output)
{
//INPUT OPERATİONS //
FILE *fp, *fp_out; SVG *svg; Coord *coord;
int array_size = 0, i=0, max_lines = 0; // Line counters
char c; // To store a character read from file to check whether newline or not
char *str_joined = NULL;
size_t total_length=0, length=0;
// Open the file
fp = fopen(input, "r");
if (fp == NULL)
{
perror("Error");
}
else
{
//Count the number of lines for the array size
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n')
array_size = array_size + 1;
fclose(fp);
}
// read the file into an array of coordinates
coord = (Coord*)malloc(array_size * sizeof(Coord)); //preallocation for performance
fp = fopen(input, "r");
if (fp == NULL)
{
perror("Error");
}
else
{
while(!feof(fp))
{
//check whether input getting correctly
if(fscanf(fp, "%d, %d", &coord[i].x, &coord[i].y)==2)
i++;
}
fclose(fp);
}
//OUTPUT OPERATIONS
max_lines = i;
svg = (SVG*)malloc(array_size * sizeof(SVG)); // allocate memory
//total length and length of string
for(i=0; i<max_lines; i++)
{
sprintf(svg[i].xy , "%d,%d ", coord[i].x, coord[i].y);
total_length += strlen(svg[i].xy);
//printf("%s\n", svg[i].xy);
}
str_joined = (char*)malloc(total_length * 2); // allocate memory for joined strings
str_joined[0] = '[=10=]'; // empty string we can append to
for(i=0; i<max_lines; i++)
{
strcat(str_joined, svg[i].xy);
}
fp_out = fopen(output,"w+"); //erase the content and write on it if exists or create the file and write on it
if (fp_out==NULL)
{
perror("Error");
}
else
{
fprintf(fp_out, "<svg><polyline points='%s'/></svg>" , str_joined);
printf("Operation successful.\n");
}
//printf("%s\n", str_joined);
}
我正在尝试制作一个程序,它从 txt 文件(坐标如 x1、x2)获取输入并将其存储在元素上以 SVG 格式写入。我在下面编写的代码实际上有效。问题是当我输入大于 16kb 时,在 svg 文件上写入应用程序崩溃。所以我实际上找不到问题。它应该是关于 "strcat(str_joined, svg[i].xy);" 这个,因为我将这么多字符串连接到一个字符串变量中。
输入示例:
339, 52
339, 52
339, 52
338, 53
338, 53
337, 54
337, 54
337, 54
337, 55
337, 55
336, 56
336, 56
336, 56
336, 57
335, 57
335, 56
334, 56
334, 56
333, 56
332, 56
332, 56
331, 56
331, 56
330, 56
330, 56
329, 56
输出示例:<svg><polyline points='339,52 339,52 339,52 338,53 338,53 337,54 '/></svg>
和代码:我只是放了代码的 generate_svg 部分。
void generate_svg(char *input, char *output)
{
//INPUT OPERATİONS //
FILE *fp;
int array_size = 0, i=0, max_lines = 0; // Line counters
char c; // To store a character read from file to check whether newline or not
char *str_joined = NULL;
// Open the file
fp = fopen(input, "r");
//Count the number of lines for the array size
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n')
array_size = array_size + 1;
fclose(fp);
// read the file into an array of coordinates
Coord *coord = malloc(array_size * sizeof *coord); //preallocation for performance
fp = fopen(input, "r");
while(!feof(fp))
{
//check whether input getting correctly
if(fscanf(fp, "%d, %d", &coord[i].x, &coord[i].y)==2)
i++;
}
fclose(fp);
//OUTPUT OPERATIONS
max_lines = i;
SVG *svg = malloc(array_size * sizeof *svg); // allocate memory
size_t total_length=0, length=0; //total length and length of string
for(i=0; i<max_lines; i++)
{
sprintf(svg[i].xy , "%d,%d ", coord[i].x, coord[i].y);
total_length += strlen(svg[i].xy);
//printf("%s\n", svg[i].xy);
}
str_joined = (char*)malloc(total_length * sizeof *str_joined); // allocate memory for joined strings
str_joined[0] = '[=11=]'; // empty string we can append to
for(i=0; i<max_lines; i++)
{
strcat(str_joined, svg[i].xy);
length = strlen(str_joined);
str_joined[length+1] = '[=11=]'; /* followed by terminator */
}
FILE *fp_out;
fp_out = fopen(output,"w+"); //erase the content and write on it if exists or create the file and write on it
if(fp_out == NULL)
{
printf("Error");
}
else
{
fprintf(fp_out, "<svg><polyline points='%s'/></svg>" , str_joined);
printf("Operation successful.\n");
}
//printf("%s\n", str_joined);
}
因此,我们将不胜感激。提前致谢。
//更新
Header:
//definitions
#define MAX_FILE_NAME 100
#define OUTPUT_FILE_NAME "svg_output.svg"
void generate_svg(char *input, char *output);
//storage for coordinates
typedef struct Coord
{
int x;
int y;
}Coord;
typedef struct SVG
{
char xy[20];
}SVG;
好的,非常感谢您的帮助。我解决了这个问题。我为 C 编译配置了 Visual Studio,并将我的代码标准化为 C99。所以我的代码的最终形状如下,它可以工作。
void generate_svg(char *input, char *output)
{
//INPUT OPERATİONS //
FILE *fp, *fp_out; SVG *svg; Coord *coord;
int array_size = 0, i=0, max_lines = 0; // Line counters
char c; // To store a character read from file to check whether newline or not
char *str_joined = NULL;
size_t total_length=0, length=0;
// Open the file
fp = fopen(input, "r");
if (fp == NULL)
{
perror("Error");
}
else
{
//Count the number of lines for the array size
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n')
array_size = array_size + 1;
fclose(fp);
}
// read the file into an array of coordinates
coord = (Coord*)malloc(array_size * sizeof(Coord)); //preallocation for performance
fp = fopen(input, "r");
if (fp == NULL)
{
perror("Error");
}
else
{
while(!feof(fp))
{
//check whether input getting correctly
if(fscanf(fp, "%d, %d", &coord[i].x, &coord[i].y)==2)
i++;
}
fclose(fp);
}
//OUTPUT OPERATIONS
max_lines = i;
svg = (SVG*)malloc(array_size * sizeof(SVG)); // allocate memory
//total length and length of string
for(i=0; i<max_lines; i++)
{
sprintf(svg[i].xy , "%d,%d ", coord[i].x, coord[i].y);
total_length += strlen(svg[i].xy);
//printf("%s\n", svg[i].xy);
}
str_joined = (char*)malloc(total_length * 2); // allocate memory for joined strings
str_joined[0] = '[=10=]'; // empty string we can append to
for(i=0; i<max_lines; i++)
{
strcat(str_joined, svg[i].xy);
}
fp_out = fopen(output,"w+"); //erase the content and write on it if exists or create the file and write on it
if (fp_out==NULL)
{
perror("Error");
}
else
{
fprintf(fp_out, "<svg><polyline points='%s'/></svg>" , str_joined);
printf("Operation successful.\n");
}
//printf("%s\n", str_joined);
}