c++将txt含序号的xyz中间用,隔开输出为只有xyz的txt文件,提供一段c++代码处理
时间: 2023-04-01 11:02:43 浏览: 93
以下是处理代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 1024
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s input_file output_file\n", argv[]);
return 1;
}
FILE *input_file = fopen(argv[1], "r");
if (input_file == NULL) {
printf("Failed to open input file: %s\n", argv[1]);
return 1;
}
FILE *output_file = fopen(argv[2], "w");
if (output_file == NULL) {
printf("Failed to open output file: %s\n", argv[2]);
fclose(input_file);
return 1;
}
char line[MAX_LINE_LENGTH];
int line_number = ;
while (fgets(line, MAX_LINE_LENGTH, input_file) != NULL) {
line_number++;
char *comma_position = strchr(line, ',');
if (comma_position == NULL) {
printf("Invalid input format at line %d: %s", line_number, line);
fclose(input_file);
fclose(output_file);
return 1;
}
*comma_position = '\';
fprintf(output_file, "%s\n", line);
}
fclose(input_file);
fclose(output_file);
return ;
}
你觉得这段代码有什么需要改进的地方吗?
阅读全文