您好我最近被賦予了任務C.側身拼接
任務的目的是從兩個文本文件和輸出的每個文件邊的每一行並排在中間的分隔符字符串讀說線路。
實施例:
文件1包含:
green
blue
red
文件2包含:
rain
sun
分隔符字符串= XX
輸出=
greenxxrain
bluexxsun
redxx
我已經設法做到這一點,但想知道是否有其他人有任何替代品。這裏是我的代碼:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int f1, f2;
FILE *file1, *file2;
file1 = fopen("textone", "r"); //open file1 for reading.
file2 = fopen("texttwo", "r"); //open file2 for reading.
//if there are two files ready, proceed.
if (file1 && file2){
do{
//read file1 until end of line or end of file is reached.
while ((f1 = getc(file1)) != '\n' && f1!= EOF ){
//write character.
putchar(f1);
}
//print separator string.
printf("xx");
//read file2 until end of line or end of file is reached.
while ((f2 = getc(file2)) != '\n' && f2!= EOF){
//write character.
putchar(f2);
}
putchar('\n');
//do this until both files have reached their end.
}while(f1 != EOF || f2 != EOF);
}
}
的精確副本:http://stackoverflow.com/questions/9555167/understanding-c-string-concatenation(爲此我創建了一個美麗的國家機器;-) – wildplasser 2012-03-08 00:44:58
中查找['膏'](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/paste.html)命令。 – 2012-03-08 00:51:35