/************************************************************************
*功能: 给指定文件中的所有行加上行号后保存为指定文件
*作者: onlinewan
*参数: const char *pstr_src_file [in] 源文件路径
*参数: const char *pstr_out_file [in] 输出文件路径
*返回: 成功返回 ture, 失败返回 false.
************************************************************************/
bool add_serial_number_to_everyline(const char *pstr_src_file,
const char *pstr_out_file)
{
if ( pstr_src_file == NULL || pstr_out_file == NULL )
{
return false;
}
FILE *fp_src_file;
if ( (fp_src_file = fopen(pstr_src_file, "rt")) == NULL )
{
return false;
}
FILE *fp_out_file;
if ( (fp_out_file = fopen(pstr_out_file, "wt")) == NULL )
{
fclose(fp_src_file);
return false;
}
int i_line_number = 0;
char str_line_number[10];
memset(str_line_number, 0, 10);
char str_buffer[128];
memset(str_buffer, 0, 128);
bool b_insert_line_number = true;
while ( fgets(str_buffer, 128, fp_src_file) != NULL )
{
char *pstr_temp = str_buffer;
while ( *pstr_temp != '/0' )
{
if ( b_insert_line_number )
{
sprintf(str_line_number, "#%04d ", ++i_line_number);
fputs(str_line_number, fp_out_file);
}
char *pstr_pos = strchr(pstr_temp, '/n');
if ( pstr_pos == NULL )
{
b_insert_line_number = false;
fputs(pstr_temp, fp_out_file);
break;
}
else
{
b_insert_line_number = true;
*pstr_pos = '/0';
fputs(pstr_temp, fp_out_file);
fputs("/n", fp_out_file);
pstr_temp = pstr_pos + 1;
}
}
}
fclose(fp_src_file);
fclose(fp_out_file);
return true;
}
*功能: 给指定文件中的所有行加上行号后保存为指定文件
*作者: onlinewan
*参数: const char *pstr_src_file [in] 源文件路径
*参数: const char *pstr_out_file [in] 输出文件路径
*返回: 成功返回 ture, 失败返回 false.
************************************************************************/
bool add_serial_number_to_everyline(const char *pstr_src_file,
const char *pstr_out_file)
{
if ( pstr_src_file == NULL || pstr_out_file == NULL )
{
return false;
}
FILE *fp_src_file;
if ( (fp_src_file = fopen(pstr_src_file, "rt")) == NULL )
{
return false;
}
FILE *fp_out_file;
if ( (fp_out_file = fopen(pstr_out_file, "wt")) == NULL )
{
fclose(fp_src_file);
return false;
}
int i_line_number = 0;
char str_line_number[10];
memset(str_line_number, 0, 10);
char str_buffer[128];
memset(str_buffer, 0, 128);
bool b_insert_line_number = true;
while ( fgets(str_buffer, 128, fp_src_file) != NULL )
{
char *pstr_temp = str_buffer;
while ( *pstr_temp != '/0' )
{
if ( b_insert_line_number )
{
sprintf(str_line_number, "#%04d ", ++i_line_number);
fputs(str_line_number, fp_out_file);
}
char *pstr_pos = strchr(pstr_temp, '/n');
if ( pstr_pos == NULL )
{
b_insert_line_number = false;
fputs(pstr_temp, fp_out_file);
break;
}
else
{
b_insert_line_number = true;
*pstr_pos = '/0';
fputs(pstr_temp, fp_out_file);
fputs("/n", fp_out_file);
pstr_temp = pstr_pos + 1;
}
}
}
fclose(fp_src_file);
fclose(fp_out_file);
return true;
}