C语言同时向不同的文件写入不同的数据
来源:互联网 发布:什么软件可以挣钱 编辑:程序博客网 时间:2024/05/21 19:26
C语言同时向不同的文件写入不同的数据
这个我写了好久才写出来的,之前不知道是什么原因总是不能同时一起写,而且写完一个程序就死了,后来在网上查到一篇文章 http://blog.csdn.net/feixiaoxing/article/details/7237649
通过修改变成以下代码。
#include <pthread.h>
#include <stdio.h>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
- C语言同时向不同的文件写入不同的数据
- c++:批量把数据写入同一文件夹下不同文件
- C#读取EXCEL 文件同时向文件中写入数据和Excel的Range对象
- c语言中format不同的数据匹配的不同数据格式
- 向存在的文件中写入数据
- Maven Log4j2 Marker 不同类型的日志写入不同的文件
- Android listView同时展示多种不同数据的item
- spark streaming 同时处理两个不同kafka集群的数据
- log4net 将不同级别的信息写入不同的日志文件
- 读取和写入不同编码格式的文件
- log4j 分级别写入不同的日志文件
- log4j 分级别写入不同的日志文件
- yii2 日志操作(log)以及写入不同的log文件
- log4j 不同的包(package)下的日志写入到不同的日志文件下
- C语言数组和指针的不同
- 不同的C语言闰年判断
- C语言求素数的不同解法
- C语言统计不同字符的字数
- cinder中的task
- 制作支付宝博客捐款页面
- Maven学习笔记之maven的聚合和继承问题
- ZOJ 3610 (The 9th Zhejiang Provincial Collegiate Programming Contest k)
- linux下vi编辑器命令入门
- C语言同时向不同的文件写入不同的数据
- C语言中存储标志(flag)效率最高的方法
- 性能测试之操作系统计数器分析方法
- Delphi Android 将Google ZXing 整合
- [20140209]人体比例练习
- Android NFC介绍
- oracle如何远程连接配置
- Memcached安装遇到的问题
- JAVA内省(Introspector)