将输入文件传递给C中的输出文件?

时间:2013-04-26 03:21:10

标签: c file-io fopen freopen

目前我能够(我认为)使用fopen打开文件。出于测试目的,我希望能够将文件的内容传递给输出文件,但是我没有得到预期的结果。这是一些代码:

#include <stdio.h>
#include <string.h>

int main(int argc, char* argv[]){ //practice on utilizing IO
char filepath[100];
char filepath2[100];
strcpy(filepath,"./");
strcpy(filepath,"./");
char typed[90];
char msg[1024];
FILE * new_file;
FILE * old_file;

// printf("Input file to be created\n");
printf("File to be opened as input: \n");
printf("--->");

fgets(typed,90,stdin); //read in file name
strtok(typed, "\n");
strcat(filepath,typed);
old_file =  fopen(filepath, "r");

printf("file to output to: \n");
fgets(filepath2,100, stdin);  
strtok(filepath2, "\n");
///attempt to create that file
new_file = fopen(filepath2,"w");
//printf("%s\n", msg);

}

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

在程序中打开文件句柄与在字处理器中打开文档略有不同。这更像是开书。要读或写,你必须用眼睛(消耗数据)或铅笔(产生数据)。

由于您打开了文件,因此需要从第一个文件中读取数据并将其写入第二个文件。类似的东西:

size_t nread;
do {
    nread = fread(msg, 1, 1024, old_file);
    fwrite(msg, 1, nread, new_file);
} while(nread != 0);

或者

int nread;
do {
    nread = fgets(msg, 1023, old_file);
    fputs(msg, new_file);
} while (nread > 0);

或者甚至一次只是一个字母。

int c;
while ( (c=fgetc(old_file)) != EOF) {
    fputc(c, new_file);
}

此外,您没有将“./”添加到第二个文件中。不确定这是否重要,但你是在第一个文件中做到的。

此外,您应该在new_file上使用fopenfreopen本身不是错误的,但它很奇怪会让其他人(包括我)感到困惑。

  

freopen()函数打开名称为指向的字符串的文件        通过路径并将流指向的流与它相关联。该        原始流(如果存在)已关闭。 mode参数仅用于        就像在fopen()函数中一样。 SRC:manpage

因此,它会根据您的意愿打开流,但它会在执行此操作时销毁stdout。所以你的程序不能再正常输出了。这可能并不重要,但似乎也没有任何真正的优势。

答案 1 :(得分:1)

我创建了一个类似于你的小c文件,希望它可以帮助你更好地理解C中的i / o。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {

char fileNameOne[] = "test.txt";
char fileNameTwo[] = "new.txt";

FILE* oldFile;
FILE* newFile;

printf("The file being read from is: %s\n", fileNameOne);
//get a File pointer to the old file(file descriptor)
oldFile = fopen(fileNameOne, "r");

//get a File pointer to the new file(file descriptor)
//a+ is to open for read and writing and create if it doesnt exist
//if we did not specify a+, but w+ for writing, we would get an invalid file descriptor because the file does not exist
newFile = fopen(fileNameTwo, "a+");
if (newFile == 0) {
    printf("error opening file\n");
    exit(1);
}
//read everything from the old file into a buffer
char buffer[1024];

printf("Contents of old file:\n");
while (fread(buffer, 1, 1024, oldFile) != 0) {
    //if fread returns zero, and end of file has occured
     printf("%s", buffer);
     //directly write the contents of fileone to filetwo
     fwrite(buffer, 1, 1024, newFile);
}

printf("The file %s has been read and written to %s\n", fileNameOne, fileNameTwo);

printf("Verification: Contents of file two:\n");

//move the offset back to the beginning of the file
rewind(newFile);

while (fread(buffer, 1, 1024, newFile)!= 0) {
    printf("%s", buffer);
}

printf("\nEnd of file 2\n");
}

我在同一个目录中创建了一个名为test的文本文件,只是给它写了一些垃圾。这是输出。

输出:

The file being read from is: test.txt
Contents of old file:
Hi, my name is Jack!

HAHAHAH

YOU DON"T even know!


The file test.txt has been read and written to new.txt
Verification: Contents of file two:
Hi, my name is Jack!

HAHAHAH

YOU DON"T even know!



End of file 2