在C语言中将输入文件传递到输出文件?

在C语言中将输入文件传递到输出文件?,c,file-io,fopen,freopen,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,"./"); st

目前我可以(我认为)使用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);

}
#包括
#包括
intmain(intargc,char*argv[]){//使用IO的实践
char文件路径[100];
char filepath2[100];
strcpy(文件路径,“./”;
strcpy(文件路径,“./”;
字符型[90];
char-msg[1024];
文件*新的_文件;
文件*旧文件;
//printf(“要创建的输入文件”);
printf(“要作为输入打开的文件:\n”);
printf(“-->”);
fgets(键入,90,标准输入);//读取文件名
strtok(键入“\n”);
strcat(文件路径,已键入);
old_file=fopen(文件路径,“r”);
printf(“要输出到:\n的文件”);
fgets(文件路径2100,标准输入);
strtok(filepath2,“\n”);
///尝试创建该文件
新的_文件=fopen(filepath2,“w”);
//printf(“%s\n”,msg);
}

非常感谢您的帮助。

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

由于文件已打开,因此需要从第一个文件读取数据并将其写入第二个文件。比如:

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

甚至是一次一个字符

int c;
while ( (c=fgetc(old_file)) != EOF) {
    fputc(c, new_file);
}
另外,您没有将“/”预加到第二个文件中。不确定这是否重要,但你对第一个文件做了

此外,您应该在新的\u文件上使用
fopen
freopen本身并没有错,但它很奇怪,会让其他人(包括我)感到困惑

函数的作用是:打开字符串所指向的文件名 并将流指向的流与其关联。这个 原始流(如果存在)已关闭。mode参数仅用于 如fopen()函数中所示。src:


因此,它会按照您的意愿打开流,但这样做时会破坏stdout。所以你的程序不能再正常输出了。这可能并不重要,但似乎也没有任何真正的优势。

我创建了一个类似于您的小c文件,希望它能帮助您更好地理解c中的I/o

代码:


我现在没有这样做吗?这里没有。你正在打开文件。现在您需要使用
fgets
fread
或其他方法从中读取数据。然后,用
fputs
fwrite
或其他东西将其写入新的_文件。嘿,这太棒了。我最终没有放弃do/while循环,只使用fgets和fput。我还使用strtok来帮助实现功能。谢谢编辑:strtok根本不需要。你的程序会打开旧的输入文件和新的输出文件(当然,尝试:
freopen
在这里是不合适的)。但是,该程序不包含从旧文件读取数据并将数据写入新文件的命令。顺便问一下,为什么要使用
freopen
?我仍在学习这个主题。我已将代码更改为fopen。我也会在这里进行调整。知道我缺少哪些命令吗?是的,正如@luserdroog和我提到的,您缺少将从第一个文件读取数据并将其写入第二个文件的命令。有几种方法可以做到这一点。@blutu我已经编辑了我的答案。你有三个选择。我喜欢这个。当我在我的最终代码中实现这一点时,我肯定会看到这一点+1.
int c;
while ( (c=fgetc(old_file)) != EOF) {
    fputc(c, new_file);
}
#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");
}
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