日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言向指定文件写入程序,C语言同时向不同的文件写入不同的数据

發(fā)布時間:2024/7/23 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言向指定文件写入程序,C语言同时向不同的文件写入不同的数据 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

C語言同時向不同的文件寫入不同的數(shù)據(jù)

這個我寫了好久才寫出來的,之前不知道是什么原因總是不能同時一起寫,而且寫完一個程序就死了,后來在網(wǎng)上查到一篇文章 http://blog.csdn.net/feixiaoxing/article/details/7237649

通過修改變成以下代碼。

#include #include

FILE * tp1; //指向存儲吞吐量的指針

FILE * tp2; //指向存儲吞吐量的指針

FILE * temp1; //存數(shù)數(shù)據(jù)臨時文件

FILE * temp2; //存數(shù)數(shù)據(jù)臨時文件

int i;

int test_time;//測試時間

struct punto{

float x;

float y;

} pto1, pto2;

void* tprocess1(void* args){

//打開臨時文件并存儲數(shù)據(jù)

temp1 = fopen("temp1.txt", "w+");

for(i = 0; i < test_time; i++){

pto1.x = i;

pto1.y = 0;

fwrite(&pto1, sizeof(pto1), 1, temp1);

}

//打開目標(biāo)文件

if((tp1 = fopen("test1.dat","w+")) == NULL){

fprintf(stdout, "Error opening file test\n");

return 0;

}

//重定位臨時文件指針

rewind(temp1);

fread(&pto1, sizeof(pto1), 1, temp1);//讀取臨時文件第一行數(shù)據(jù)存在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);//將結(jié)構(gòu)體的x y 寫入到文件指針 tp1 上

fseek(temp1, -sizeof(pto1), SEEK_CUR);//將指針重定位到當(dāng)前行的首部

fwrite(&pto1, sizeof(pto1), 1, temp1);//將數(shù)據(jù)寫入到當(dāng)前行

fprintf(stdout, "向test1.dat當(dāng)前行寫入: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){

//打開臨時文件并存儲數(shù)據(jù)

temp2 = fopen("temp2.txt", "w+");

for(i = 0; i < test_time; i++){

pto2.x = i;

pto2.y = 0;

fwrite(&pto2, sizeof(pto2), 1, temp2);

}

//打開目標(biāo)文件

if((tp2 = fopen("test2.dat","w+")) == NULL){

fprintf(stdout, "Error opening file test\n");

return 0;

}

rewind(temp2);//重定位臨時文件指針

fread(&pto2, sizeof(pto2), 1, temp2);//讀取臨時文件第一行數(shù)據(jù)存在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);//將結(jié)構(gòu)體的x y 寫入到文件指針 tp 上

fseek(temp2, -sizeof(pto2), SEEK_CUR);//將指針重定位到當(dāng)前行的首部

fwrite(&pto2, sizeof(pto2), 1, temp2);//將數(shù)據(jù)寫入到當(dāng)前行

fprintf(stdout, "向test2.dat當(dāng)前行寫入: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);//創(chuàng)建線程并啟動

pthread_create(&t2,NULL,tprocess2,NULL);//創(chuàng)建線程并啟動

sleep(1);

pthread_join(t1,NULL);//等待線程結(jié)束,要等的線程就是第一個參數(shù),程序會在這個地方停下來,直到線程結(jié)束,第二個參數(shù)用來接受線程函數(shù)的返回值

pthread_join(t2,NULL);

fprintf(stdout, "Test end...\n");

return 0;

}

輸出結(jié)果: 向test1.dat當(dāng)前行寫入:X = 0.000000 Y = 83.000000 從temp1.txt讀出下一行:X = 1.000000 Y = 0.000000 向test2.dat當(dāng)前行寫入:X = 0.000000 Y = 86.000000 從temp2.txt讀出下一行:X = 1.000000 Y = 0.000000 向test2.dat當(dāng)前行寫入:X = 1.000000 Y = 77.000000 向test1.dat當(dāng)前行寫入:X = 1.000000 Y = 15.000000 從temp1.txt讀出下一行:X = 2.000000 Y = 0.000000 從temp2.txt讀出下一行:X = 2.000000 Y = 0.000000 向test2.dat當(dāng)前行寫入:X = 2.000000 Y = 35.000000 向test1.dat當(dāng)前行寫入:X = 2.000000 Y = 93.000000 從temp2.txt讀出下一行:X = 3.000000 Y = 0.000000 從temp1.txt讀出下一行:X = 3.000000 Y = 0.000000 向test2.dat當(dāng)前行寫入:X = 3.000000 Y = 86.000000 從temp2.txt讀出下一行:X = 4.000000 Y = 0.000000 向test1.dat當(dāng)前行寫入:X = 3.000000 Y = 92.000000 從temp1.txt讀出下一行:X = 4.000000 Y = 0.000000 向test2.dat當(dāng)前行寫入:X = 4.000000 Y = 49.000000 從temp2.txt讀出下一行:X = 5.000000 Y = 0.000000 向test1.dat當(dāng)前行寫入:X = 4.000000 Y = 21.000000 從temp1.txt讀出下一行:X = 5.000000 Y = 0.000000 向test2.dat當(dāng)前行寫入:X = 5.000000 Y = 62.000000 從temp2.txt讀出下一行:X = 6.000000 Y = 0.000000 向test1.dat當(dāng)前行寫入:X = 5.000000 Y = 27.000000 從temp1.txt讀出下一行:X = 6.000000 Y = 0.000000 向test2.dat當(dāng)前行寫入:X = 6.000000 Y = 90.000000 從temp2.txt讀出下一行:X = 7.000000 Y = 0.000000 向test1.dat當(dāng)前行寫入:X = 6.000000 Y = 59.000000 從temp1.txt讀出下一行:X = 7.000000 Y = 0.000000 向test2.dat當(dāng)前行寫入:X = 7.000000 Y = 63.000000 從temp2.txt讀出下一行:X = 8.000000 Y = 0.000000 向test1.dat當(dāng)前行寫入:X = 7.000000 Y = 26.000000 從temp1.txt讀出下一行:X = 8.000000 Y = 0.000000

總結(jié)

以上是生活随笔為你收集整理的c语言向指定文件写入程序,C语言同时向不同的文件写入不同的数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。