C语言同时向不同的文件写入不同的数据
这个我写了好久才写出来的,之前不知道是什么原因总是不能同时一起写,而且写完一个程序就死了,后来在网上查到一篇文章 /feixiaoxing/article/details/7237649
通过修改变成以下代码。
#include #include
FILE * tp1; //指向存储吞吐量的指针
FILE * tp2; //指向存储吞吐量的指针
FILE * temp1; //存数数据临时文件
FILE * temp2; //存数数据临时文件
int i;
int test_time;//测试时间
struct punto{
float x;
float y;
} pto1, pto2;
void* tprocess1(void* args){
//打开临时文件并存储数据
temp1 = fopen("temp1.txt", "w+");
for(i = 0; i < test_time; i++){
pto1.x = i;
pto1.y = 0;
fwrite(&pto1, sizeof(pto1), 1, temp1);
}
//打开目标文件
if((tp1 = fopen("test1.dat","w+")) == NULL){
fprintf(stdout, "Error opening file test\n");
return 0;
}
//重定位临时文件指针
rewind(temp1);
fread(&pto1, sizeof(pto1), 1, temp1);//读取临时文件第一行数据存在pto1地址上
while(!feof(temp1)){
if (ferror(tp1)){
fprintf(stderr, "Error in file tp2\n");
break;
}
pto1.y = rand()%100;//给y赋值
fprintf(tp1, "%f %f\n", pto1.x, pto1.y);//将结构体的x y 写入到文件指针 tp1 上
fseek(temp1, -sizeof(pto1), SEEK_CUR);//将指针重定位到当前行的首部
fwrite(&pto1, sizeof(pto1), 1, temp1);//将数据写入到当前行
fprintf(stdout, "向test1.dat当前行写入:X = %f Y = %f\n", pto1.x, pto1.y);
fread(&pto1, sizeof(pto1), 1, temp1);//读取下一行
fprintf(stdout, "从temp1.txt读出下一行:X = %f Y = %f\n", pto1.x, pto1.y);
sleep(1);
}
fclose(tp1);
fclose(temp1);
remove("temp1.txt");
}
void* tprocess2(void* args){
//打开临时文件并存储数据
temp2 = fopen("temp2.txt", "w+");
for(i = 0; i < test_time; i++){
pto2.x = i;
pto2.y = 0;
fwrite(&pto2, sizeof(pto2), 1, temp2);
}
//打开目标文件
if((tp2 = fopen("test2.dat","w+")) == NULL){
fprintf(stdout, "Error opening file test\n");
return 0;
}
rewind(temp2);//重定位临时文件指针
fread(&pto2, sizeof(pto2), 1, temp2);//读取临时文件第一行数据存在pto2地址上
while(!feof(temp2)){
if (ferror(tp2)){
fprintf(stderr, "Error in file tp2\n");
break;
}
pto2.y = rand()%100;//给y赋值
fprintf(tp2, "%f %f\n", pto2.x, pto2.y);//将结构体的x y 写入到文件指针 tp 上
fseek(temp2, -sizeof(pto2), SEEK_CUR);//将指针重定位到当前行的首部
fwrite(&pto2, sizeof(pto2), 1, temp2);//将数据写入到当前行
fprintf(stdout, "向test2.dat当前行写入:X = %f Y = %f\n", pto2.x, pto2.y);
fread(&pto2, sizeof(pto2), 1, temp2);//读取下一行
fprintf(stdout, "从temp2.txt读出下一行:X = %f Y = %f\n", pto2.x, pto2.y);
sleep(1);
}
fclose(tp2);
fclose(temp2);
remove("temp2.txt");
}
int main(){
pthread_t t1;
pthread_t t2;
//输入测试时间
fprintf(stdout, "Please input test time (minutes):");
fscanf(stdin, "%d", &test_time);
getchar();
test_time = test_time * 60;
//alarm(test_time*2);
pthread_create(&t1,NULL,tprocess1,NULL);//创建线程并启动
pthread_create(&t2,NULL,tprocess2,NULL);//创建线程并启动
sleep(1);
pthread_join(t1,NULL);//等待线程结束,要等的线程就是第一个参数,程序会在这个地方停下来,直到线程结束,第二个参数用来接受线程函数的返回值
pthread_join(t2,NULL);
fprintf(stdout, "Test end...\n");
return 0;
}
输出结果: 向test1.dat当前行写入:X = 0.000000 Y = 83.000000 从temp1.txt读出下一行:X = 1.000000 Y = 0.000000 向test2.dat当前行写入:X = 0.000000 Y = 86.000000 从temp2.txt读出下一行:X = 1.000000 Y = 0.000000 向test2.dat当前行写入:X = 1.000000 Y = 77.000000 向test1.dat当前行写入:X = 1.000000 Y = 15.000000 从temp1.txt读出下一行:X = 2.000000 Y = 0.000000 从temp2.txt读出下一行:X = 2.000000 Y = 0.000000 向test2.dat当前行写入:X = 2.000000 Y = 35.000000 向test1.dat当前行写入:X = 2.000000 Y = 93.000000 从temp2.txt读出下一行:X = 3.000000 Y = 0.000000 从temp1.txt读出下一行:X = 3.000000 Y = 0.000000 向test2.dat当前行写入:X = 3.000000 Y = 86.000000 从temp2.txt读出下一行:X = 4.000000 Y = 0.000000 向test1.dat当前行写入:X = 3.000000 Y = 92.000000 从temp1.txt读出下一行:X = 4.000000 Y = 0.000000 向test2.dat当前行写入:X = 4.000000 Y = 49.000000 从temp2.txt读出下一行:X = 5.000000 Y = 0.000000 向test1.dat当前行写入:X = 4.000000 Y = 21.000000 从temp1.txt读出下一行:X = 5.000000 Y = 0.000000 向test2.dat当前行写入:X = 5.000000 Y = 62.000000 从temp2.txt读出下一行:X = 6.000000 Y = 0.000000 向test1.dat当前行写入:X = 5.000000 Y = 27.000000 从temp1.txt读出下一行:X = 6.000000 Y = 0.000000 向test2.dat当前行写入:X = 6.000000 Y = 90.000000 从temp2.txt读出下一行:X = 7.000000 Y = 0.000000 向test1.dat当前行写入:X = 6.000000 Y = 59.000000 从temp1.txt读出下一行:X = 7.000000 Y = 0.000000 向test2.dat当前行写入:X = 7.000000 Y = 63.000000 从temp2.txt读出下一行:X = 8.000000 Y = 0.000000 向test1.dat当前行写入:X = 7.000000 Y = 26.000000 从temp1.txt读出下一行:X = 8.000000 Y = 0.000000