C++中简单的文件写入函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8863505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple file write function in C++
提问by Master345
I have this code:
我有这个代码:
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
writeFile();
}
int writeFile ()
{
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile << "Writing this to a file.\n";
myfile << "Writing this to a file.\n";
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
Why is this not working? it gives me the error:
为什么这不起作用?它给了我错误:
1>------ Build started: Project: FileRead, Configuration: Debug Win32 ------
1> file.cpp
1>e:\documents and settings\row\my documents\visual studio 2010\projects\fileread\fileread\file.cpp(8): error C3861: 'writeFile': identifier not found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
and this is just a simple function. I'm using Visual Studio 2010.
这只是一个简单的功能。我正在使用 Visual Studio 2010。
回答by Tudor
There are two solutions to this. You can either place the method above the method that calls it:
对此有两种解决方案。您可以将方法置于调用它的方法之上:
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int writeFile ()
{
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile << "Writing this to a file.\n";
myfile << "Writing this to a file.\n";
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
int main()
{
writeFile();
}
Or declare a prototype:
或者声明一个原型:
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int writeFile();
int main()
{
writeFile();
}
int writeFile ()
{
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile << "Writing this to a file.\n";
myfile << "Writing this to a file.\n";
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
回答by Igor Oks
Your main
doesn't know about writeFile()
and can't call it.
你main
不知道writeFile()
也不能称之为。
Move writefile
to be before main
, or declare a function prototypeint writeFile();
before main
.
移动writefile
是之前main
,或声明函数原型int writeFile();
之前main
。
回答by Macmade
You need to declare the prototypeof your writeFile
function, before actually using it:
需要声明的原型你的writeFile
功能,在实际使用之前:
int writeFile( void );
int main( void )
{
...
回答by 6502
This is a place in which C++ has a strange rule. Before being able to compile a call to a function the compiler must know the function name, return value and all parameters.
This can be done by adding a "prototype". In your case this simply means adding before main
the following line:
这是C++有一个奇怪规则的地方。在能够编译对函数的调用之前,编译器必须知道函数名称、返回值和所有参数。这可以通过添加“原型”来完成。在您的情况下,这仅意味着在main
以下行之前添加:
int writeFile();
this tells the compiler that there exist a function named writeFile
that will be defined somewhere, that returns an int
and that accepts no parameters.
这告诉编译器存在一个writeFile
将在某处定义的名为的函数,该函数返回一个int
并且不接受任何参数。
Alternatively you can define first the function writeFile
and then main
because in this case when the compiler gets to main
already knows your function.
或者,您可以先定义函数writeFile
,然后main
因为在这种情况下编译器main
已经知道您的函数。
Note that this requirement of knowing in advance the functions being called is not always applied. For example for class members defined inline it's not required...
请注意,这种事先知道被调用函数的要求并不总是适用。例如,对于内联定义的类成员,它不是必需的......
struct Foo {
void bar() {
if (baz() != 99) {
std::cout << "Hey!";
}
}
int baz() {
return 42;
}
};
In this case the compiler has no problem analyzing the definition of bar
even if it depends on a function baz
that is declared later in the source code.
在这种情况下,编译器分析 的定义没有问题,bar
即使它依赖于baz
稍后在源代码中声明的函数。
回答by Sumeet
The function declaration int writeFile () ; seems to be missing in the code. Add int writeFile () ; before the function main()
函数声明 int writeFile() ; 代码中似乎缺少。添加 int writeFile() ; 在函数 main() 之前
回答by Mattias Nilsson
Switch the order of the functions or do a forward declaration of the writefiles function and it will work I think.
切换函数的顺序或对 writefiles 函数进行前向声明,我认为它会起作用。