从一个文件拷贝数据到另外一个文件

Kinges 2013-01-31 11:26:17

各位大神们,我调试这个程序三天了,结果还没搞定,急死我了
总是不能正常的把数据从文件里面读取出来,还请各位帮帮忙,看一下

谢过了
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
...全文
411 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
yiyamzen 2013-02-05
  • 打赏
  • 举报
回复
位操作是对二进制进行操作的。 因为十进制和二进制的每个位不是一一对应的,所以这个貌似不好实现。
Kinges 2013-02-04
  • 打赏
  • 举报
回复
引用 8 楼 yiyamzen 的回复:
引用 6 楼 kinges 的回复: C/C++ code?123456789101112131415161718192021222324252627//每次只返回一行,4个数字int split_abcde_number(int array[5],char *p_search, FILE *fp_search_temp){ int temp_get_number; int……
真是好眼里 还弱弱的请教一下 怎么样通过移位的算法,得到一个数字的个位,十位,百位,千位,万位的数字? >> << && ||只用这些运算符。
yiyamzen 2013-02-04
  • 打赏
  • 举报
回复
引用 6 楼 kinges 的回复:
C/C++ code?123456789101112131415161718192021222324252627//每次只返回一行,4个数字int split_abcde_number(int array[5],char *p_search, FILE *fp_search_temp){ int temp_get_number; int g,s,b,……
是不是要把 w = temp_get_number/10000&10; 改成 w = temp_get_number/10000%10; 呀?
Kinges 2013-02-01
  • 打赏
  • 举报
回复
char *p_search 被下面这个主函数申请中的变量赋值: char *search_number = malloc(sizeof(char)*40);
Kinges 2013-02-01
  • 打赏
  • 举报
回复
//每次只返回一行,4个数字
int split_abcde_number(int array[5],char *p_search, FILE *fp_search_temp)
{
	
	int temp_get_number;
	int g,s,b,q,w;
	//p_search = (char *)malloc(sizeof(char)*400);
	
	fgets(p_search,40 ,fp_search_temp);
	temp_get_number=atoi(p_search);
	g = temp_get_number%10;
	s = temp_get_number/10%10;
	b = temp_get_number/100%10;
	q = temp_get_number/1000%10;
	w = temp_get_number/10000&10;
	
	array[0]=g;
	array[1]=s;
	array[2]=b;
	array[3]=q;
	array[4]=w;
	
	printf("Sub loop: %d %d %d %d %d \n",array[4],array[3],array[2],array[1],array[0]);
	
	//free(p_search);
	return 1;
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++ 还麻烦楼上的兄台帮忙看一下,真是要晕死了 原始文件是下面这样: 55237 37183 29135 71101 87747 61105 78918 69044 88571 83811 28308 05650 66871 42625 43931 38815 65422 21901 41622 87522 经过上面那段小程序处理之后,就全部变样了,还麻烦兄台帮忙分析一下 输出如下: start open file start LOOP to read file aready in the loop! Sub loop: 0 5 2 3 7 0 5 2 3 7 aready in the loop! Sub loop: 2 7 1 8 3 2 7 1 8 3 aready in the loop! Sub loop: 2 9 1 3 5 2 9 1 3 5 aready in the loop! Sub loop: 2 1 1 0 1 2 1 1 0 1 aready in the loop! Sub loop: 8 7 7 4 7 8 7 7 4 7 aready in the loop! Sub loop: 2 1 1 0 5 2 1 1 0 5 aready in the loop! Sub loop: 2 8 9 1 8 2 8 9 1 8 aready in the loop! Sub loop: 2 9 0 4 4 2 9 0 4 4 aready in the loop! Sub loop: 8 8 5 7 1 8 8 5 7 1 aready in the loop! Sub loop: 8 3 8 1 1 8 3 8 1 1 aready in the loop! Sub loop: 2 8 3 0 8 2 8 3 0 8 aready in the loop! Sub loop: 0 5 6 5 0 0 5 6 5 0 aready in the loop! Sub loop: 2 6 8 7 1 2 6 8 7 1 aready in the loop! Sub loop: 0 2 6 2 5 0 2 6 2 5 aready in the loop! Sub loop: 0 3 9 3 1 0 3 9 3 1 aready in the loop! Sub loop: 2 8 8 1 5 2 8 8 1 5 aready in the loop! Sub loop: 2 5 4 2 2 2 5 4 2 2 aready in the loop! Sub loop: 2 1 9 0 1 2 1 9 0 1 aready in the loop! Sub loop: 0 1 6 2 2 0 1 6 2 2 aready in the loop! Sub loop: 8 7 5 2 2 8 7 5 2 2 aready in the loop! Sub loop: 2 1 2 3 1 2 1 2 3 1
yiyamzen 2013-01-31
  • 打赏
  • 举报
回复
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
yiyamzen 2013-01-31
  • 打赏
  • 举报
回复
如果你想把读出来的一串字符串的首地址存到数组temp_result的每个元素里的话,你需要把temp_result定义成指向指针的指针数组。
yiyamzen 2013-01-31
  • 打赏
  • 举报
回复
fgets(search_number,16 ,search_file); //读16个字符 temp_result[loop_read_search]=(*search_number); //这句有问题 search_number是读出来的16个字符,temp_result[loop_read_search]这里只能存一个 应该改成 temp_result[loop_read_search]=search_number[loop_read_search]; temp_result[loop_read_search]里可以存一个字符 search_number[loop_read_search]是读出来的16个字符中的一个,具体是哪个由索引loop_read_search决定
Kinges 2013-01-31
  • 打赏
  • 举报
回复
引用 1 楼 yiyamzen 的回复:
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……
+++++++++++++++++++++++++++++ 非常感谢您的回复,再咨询一下,怎么把每行的数据放入到数组里面呢? 我尝试之后,发现输出全部都是地址 temp_result[loop_read_search]=(*search_number); printf("%d,\n", temp_result[loop_read_search]); 输出结果: aready in the loop! 959919668, aready in the loop! 959985204, aready in the loop! 959985460, aready in the loop! 943142453, aready in the loop! 959919669, aready in the loop! 959985205, aready in the loop! 959985461, aready in the loop! 959985462, start close file
赵4老师 2013-01-31
  • 打赏
  • 举报
回复
不要把 fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待 弄混了!

70,022

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧