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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:14:57  来源:igfitidea点击:

Simple file write function in C++

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 maindoesn't know about writeFile()and can't call it.

main不知道writeFile()也不能称之为。

Move writefileto 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 writeFilefunction, 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 mainthe following line:

这是C++有一个奇怪规则的地方。在能够编译对函数的调用之前,编译器必须知道函数名称、返回值和所有参数。这可以通过添加“原型”来完成。在您的情况下,这仅意味着在main以下行之前添加:

int writeFile();

this tells the compiler that there exist a function named writeFilethat will be defined somewhere, that returns an intand that accepts no parameters.

这告诉编译器存在一个writeFile将在某处定义的名为的函数,该函数返回一个int并且不接受任何参数。

Alternatively you can define first the function writeFileand then mainbecause in this case when the compiler gets to mainalready 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 bareven if it depends on a function bazthat 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 函数进行前向声明,我认为它会起作用。