如何在一个文件中并排两个并排两列的文件? [英] How do I merge two files with two columns side by side in one file?

查看:195
本文介绍了如何在一个文件中并排两个并排两列的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨;



如何在一个文件中复制两个文件,假设每个文件包含一列数据,放入一个文件而不读取变量并放入它们在另一个文件中,普通程序是每个文件的两个读取内容并将它们放入另一个文件中,这在处理大数据时耗费了大量时间,我的意思是在一个文件中并排合并两列,任何创意。



示例输入:文件1



 1 
2
3
4
5
$ 6





示例输入:文件2



 6 
5
4
3
2
1





所需输出:结果文件



 1 6 
2 5
3 4
4 3
5 2
6 1





谢谢



我的尝试:



< pre lang =c ++> FILE * fptr,* fptr1,* fptr2;
char string1 [ 2000 ],string2 [ 2000 ];


fn1.Format( d:\\ file_1.txt );
fn2.Format( d:\\file_2.txt);
fn.Format( C:\\file_3.txt);

fopen_s(& fptr,fn1, rb);
fopen_s(& fptr1,fn2, rb);
fopen_s(& fptr2,fn, wb);

while (fgets(string1, sizeof (string1),fptr)! = NULL)
{
fgets(string2, sizeof (string2),fptr1);
fputs(string1,fptr2);
fputs(s \t,fptr2);
fputs(string2,fptr2);
}

fclose(fptr);
fclose(fptr1);
fclose(fptr2);





注意:这两个文件的长度相同

解决方案

你已经标记了你的解决方案'c ++','c','MFC' - 但你的代码显然是'c'



对于这类问题有一些担忧 - 你在每个文件中说超过'50000' - 我们假设它们都是像你的例子一样的'单个'字符行(在这种情况下为什么你定义了2000个长字符数组?)



可能需要一段时间/实验才能让它更快 - 你定义为'快'还是'慢'? (你必须意识到很多I / O正在发生)



#一种方法(我会使用)可能是使用c ++,std :: ifstream和std :: list,将文件读入内存,然后使用迭代器和std :: ofstream输出它们 - 意图是ifstream和ofstream提供一些缓冲以减少来自磁盘的i / o锁定



#作为上述方法的'后退',我想知道ifstream / ofstream是否比fgets / fput更快......你可以简单地将你的代码'c'升级为'c ++'并且尝试它



引用'更快'/'慢'是没有意义的,除非你测量时间,所以你需要获得你的进程的开始和结束时间/尝试并比较它们


hi;

how can i copy two files in one file, supposed that each file contain one column of data, into one file without reading variables and put them in another file, ordinary procedure is two read content of each file and put them into another one, which consume much time when dealing with large data, i mean merge two columns side by side in one file, any creative ideas.

Example Inputs : file 1

1
2
3
4
5
6



Example Inputs : file 2

6
5
4
3
2
1



Desired Output : "resulted file"

1  6
2  5
3  4
4  3
5  2
6  1



thanks

What I have tried:

FILE *fptr,*fptr1,*fptr2;
char string1[2000],string2[2000];


fn1.Format("d:\\file_1.txt");
fn2.Format("d:\\file_2.txt");
fn.Format("C:\\file_3.txt");

fopen_s(&fptr,fn1,"rb");
fopen_s(&fptr1,fn2,"rb");
fopen_s(&fptr2,fn,"wb");

while(fgets(string1,sizeof(string1),fptr)!=NULL)
{
    fgets(string2,sizeof(string2),fptr1); 
    fputs (string1,fptr2);
    fputs (s"\t",fptr2);
    fputs (string2,fptr2);
}

fclose(fptr);	
fclose(fptr1);
fclose(fptr2);



note: the two files are the same in length

解决方案

you've tagged your solution 'c++', 'c', 'MFC' - yet your code is clearly 'c'

There are a couple of worries with this sort of question - you say more than '50000' in each file - are we to assume they are all 'single' character lines like your examples (in which case why have you defined 2000 long char arrays ?)

It could take a while/experimentation to 'make it faster' - and what do you define as 'fast' or 'slow' ? (you must realise a lot of I/O is happening)

# one approach (that I would use) may be to use c++, std::ifstream and std::list, read the files into memory and then output them using iterators and std::ofstream - the intent being that ifstream and ofstream provide some buffering as to reduce the locks in i/o from disk

# as a 'step back' from the approach above, I wonder if ifstream/ofstream are faster than fgets/fputs ... you could simple upgrade your code in 'c' to 'c++' and try it

There's no point in quoting 'faster' / 'slower' unless you're measuring times though, so you need to get start and end times for your processes/attempts and compare them


这篇关于如何在一个文件中并排两个并排两列的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆