【发布时间】:2015-02-08 15:16:21
【问题描述】:
关于我以前的帖子不太成功,我想问一个问题,我希望我不会被永久禁止。 在我这学期的 C++ 中,我将与许多其他章节一起在文件中进行检查。 我的问题是假设我想处理一个文本文档
是:
#include <iostream>
#include <fstream>
using namespace std;
int main(){
ofstream thanosFile; //ofstream allows me to create and WRITE a file
thanosFile.open("thanos.txt");
thanosFile << "Hello how are you I'm fine!\n";
thanosFile.close();
}
同:
#include "stdafx.h"
#include <stdio.h>
main(){
FILE f;
printf("Give name of file");
scanf(%s,thanosFile);
f=fopen(thanosFile, "w"); // "w" defines that I want to write the file
scanf("Hello how are you I'm fine!\n");
fclose(f);
}
?? 此外,有人可以解释我如何处理二进制文件吗?方法是一样的吗?我可以用第一种方法吗,因为我发现它更容易,因为我对第二种方法的格式感到困惑!
【问题讨论】:
-
它们相差甚远。例如;后者比前者更可怕。
-
C++ 版本(第一个)是不必要的冗长。见this answer to a related question。
-
scanf("Hello how are you I'm fine!\n");这真的没有意义。你的意思可能是fprintf(f,"Hello how are you I'm fine!\n"); -
f=fopen(thanosFile, "w");如果fopen()失败怎么办? -
您可能应该在 google 中搜索二进制文件并自己学习它,因为有很多东西要学习。