创建要写入的文件时是否需要malloc?
该文件将基于其他两个文件的内容,因此我是否需要为sizeof( file a ) + sizeof( file b) + 1
的可写文件分配malloc空间?
抱歉,如果这没有意义;如果没有,我想我需要去读更多的:D
基本上,我有两个txt文件和一个字符串序列-我将每个文件的每一行并排写入,由字符串序列分隔开。
txt file a
hello stack over
flow this
is a test
txt file b
jump the
gun i am
a novice
seperator == xx
output ==
hello stack overxxjump the
flow thisxxgun i am
is a testxxa novice
最佳答案
如果你是按顺序写的,难道你不能在需要写东西的时候使用fprintf()
或fwrite()
而不是一次写整个文件吗?
编辑:根据你的更新,基本上你要做的是(可能不是有效的C,因为我不是一个C程序员):
编辑2:在msw的帮助下:
const int BUFSIZE = 200;
FILE *firstFile = fopen("file1.txt", "r");
FILE *secondFile = fopen("file2.txt", "r");
FILE *outputFile = fopen("output.txt", "w");
char* seperator = "xx";
char firstLine[BUFSIZE], secondLine[BUFSIZE];
// start a loop here
fgets(firstLine, 200, firstFile);
fgets(secondLine, 200, secondFile);
// Remove '\n's from each line
fprintf(outputFile, "%s%s%s", firstLine, seperator, secondLine);
// end a loop here
fclose(outputFile);
fclose(firstFile);
fclose(secondFile);
关于c - 是否分配malloc,这就是问题所在!,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2389221/