C语言同时向不同的文件写入不同的数据
2014-03-01 22:25
274 查看
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
这个我写了好久才写出来的,之前不知道是什么原因总是不能同时一起写,而且写完一个程序就死了,后来在网上查到一篇文章 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语言创建写入和读取TXT文件数据
- c语言实现大小端判断和二进制模式写入各种数据到文件
- c语言将数据写入文件(用链表实现)
- 将不同的数据写入不同的文件中
- C语言输出数据到指定文件,要求长度相同,不同时前面补零
- 初次使用流迭代器从标准输入读取整型数据,根据奇、偶性写入不同文件
- C语言文件读写操作,写入数据到文件
- 文件磁盘相关函数[22]-文本文件读写-不同数据类型的写入
- C语言创建一个文件,写入数据并换行,再依次读取每行的数据,将读取的行数据以一定的格式命名为另一文件的文件名
- C语言中链表任意位置怎么插入数据?然后写入文件中?
- c++:批量把数据写入同一文件夹下不同文件
- C语言中链表任意位置怎么插入数据?然后写入文件中?
- 通过调用同一个函数(参数不同)将数据写入文件和显示到屏幕上
- 使用PHP文件锁写一个多个请求同时并发写入一个文件,要求不脏读、数据不丢失
- c语言创建写入和读取TXT文件数据
- 关于C语言fprintf()函数将纯数据写入文件速度慢的解决方法
- C#读取EXCEL 文件同时向文件中写入数据和Excel的Range对象
- 第一次用C语言把数据写入文件中
- C语言中把结构内容写入二进制文件及从二进制文件中读取出数据
- c语言 实现按行读取文件。把内容按照第三种内存模型打包数据传出,把行数通过函数参数传出