从一个文件拷贝数据到另外一个文件
各位大神们,我调试这个程序三天了,结果还没搞定,急死我了
总是不能正常的把数据从文件里面读取出来,还请各位帮帮忙,看一下
谢过了
int main(void)
{
int max=0;
int temp_max=0;
//only equle 0,1
int compare_result=0;
int loop_compare=0;
int loop_read_search=0;
int leng_of_geweishu_file=204552;
int leng_of_serach_file=210;
char *path_geweishu="result.txt";
char *path_search="search_data.txt";
//int geweishu[leng_of_geweishu_file];
char *search_number=(char *)malloc(sizeof(char)*9);
FILE *search_file;
FILE *result_file;
int *test_number = 56;
printf("Test number is: %d", test_number);
printf("start open file \n");
search_file = fopen(path_search, "r");
result_file = fopen(path_geweishu, "w+");
int temp_result[210];
if((search_file && result_file) == NULL)
{
printf("Can't open file\n");
}
printf("start LOOP to read file \n");
for(loop_read_search=0;loop_read_search<leng_of_serach_file;loop_read_search++)
{
//read_file_search(search_number,search_file);
printf("aready in the loop! \n");
fseek(search_file,(sizeof(int)*4+2),0);
fgets(search_number,16 ,search_file);
//fscanf(search_file,"%d",search_number);
//fscanf(search_file,"%4d",search_number);
temp_result[loop_read_search]=(*search_number);
printf("%d,\n", (*search_number));
fprintf(result_file,"%d \n", temp_result[loop_read_search]);
}
printf("start close file \n");
fclose(search_file);
fclose(result_file);
free(search_number);
}
++++++++++++++++++++++++++++
search_data.txt 数据格式如下(一共210行,我没有全部贴出来):
0123
0124
0125
0126
0127
0128
0129
0134
0135
0136
0137
0138
0139
0145
0146
0147
0148
0149
0156
0157
0158
0159
0167
0168
0169
0178
0179
0189
0234
[解决办法]
search_data.txt的内容我只写了8行数据
以下是修改后的代码
[root@localhost csdn]# cat a.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int max=0;
int temp_max=0;
//only equle 0,1
int compare_result=0;
int loop_compare=0;
int loop_read_search=0;
int leng_of_geweishu_file=204552;
int leng_of_serach_file=8;
char *path_geweishu="result.txt";
char *path_search="search_data.txt";
//int geweishu[leng_of_geweishu_file];
char *search_number=(char *)malloc(sizeof(char)*9);
FILE *search_file;
FILE *result_file;
//int *test_number = 56;
//printf("Test number is: %d", test_number);
printf("start open file \n");
search_file = fopen(path_search, "r");
result_file = fopen(path_geweishu, "w+");
int temp_result[210];
//if((search_file && result_file) == NULL){
// printf("Can't open file\n");
//}
printf("start LOOP to read file \n");
for(loop_read_search=0;loop_read_search<leng_of_serach_file;loop_read_search++)
{
//read_file_search(search_number,search_file);
printf("aready in the loop! \n");
//fseek(search_file,(sizeof(int)*4+2),0);
fgets(search_number,16 ,search_file);
//fscanf(search_file,"%d",search_number);
//fscanf(search_file,"%4d",search_number);
//temp_result[loop_read_search]=(*search_number);
//printf("%d,\n", (*search_number));
//fprintf(result_file,"%d \n", temp_result[loop_read_search]);
fprintf(result_file,"%s", search_number);
}
printf("start close file \n");
fclose(search_file);
fclose(result_file);
free(search_number);
return 0;
}
运行结果
[root@localhost csdn]# gcc -o a a.c
[root@localhost csdn]# ./a
start open file
start LOOP to read file
aready in the loop!
aready in the loop!
aready in the loop!
aready in the loop!
aready in the loop!
aready in the loop!
aready in the loop!
aready in the loop!
start close file
[root@localhost csdn]# diff result.txt search_data.txt //对比两个文件,完全相同
[root@localhost csdn]# cat result.txt //result.txt内容
0123
0124
0125
0126
0127
0128
0129
0134