1

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

}

任何帮助表示赞赏。

4

2 回答 2

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);
}

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

此外,您应该fopen在 new_file 上使用。本身freopen并没有错,但它很奇怪并且会混淆其他人(包括我)。

freopen() 函数打开名称为 path 指向的字符串的文件,并将 stream 指向的流与它相关联。原始流(如果存在)已关闭。mode 参数的使用与 fopen() 函数中一样。来源:手册页

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

于 2013-04-26T03:38:23.623 回答
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
于 2013-04-26T04:16:34.110 回答