简体   繁体   English

我如何使用 bool 创建文件

[英]How can i use bool to create a file

Hi can someone help me with this function:嗨,有人可以帮我解决这个 function:

bool createfile (string path);

It is supposed to create a file but my problem is: What exactly the true or false have to do with creating a file??它应该创建一个文件,但我的问题是: truefalse与创建文件有什么关系? How can I use it?我该如何使用它?

The bool is the return type of the function createfile() . bool是 function createfile()的返回类型。 Without the definition it is impossible to tell for sure what exactly this value is supposed to be, but often it is used to return if the function was successful in doing what it is supposed to do, in this case, create a file.如果没有定义,就不可能确定这个值到底应该是什么,但通常它用于返回 function 是否成功完成了它应该做的事情,在这种情况下,创建一个文件。

What exactly the true or false have to do with creating a file?!对或错与创建文件到底有什么关系?!

You might want to return true if the file was successfully created or false otherwise.如果文件已成功创建,您可能希望返回true ,否则返回false

How can I use it?我该如何使用它?

This depends on the body of the function and the purpose that you want to use the function for.这取决于 function 的主体以及您要使用 function 的目的。

Quick answer快速回答

To directly answer the "How can I use it" part of your question:要直接回答您问题的“我如何使用它”部分:
You call it this way:你这样称呼它:

string path = "/path/to/my/file.txt";
bool returnedValue = createfile(path);

As for "What exactly the true or false have to do with creating a file?!"至于“创建文件的真假到底有什么关系?!” , like mentionned in the other answers, it might indicate the success or failure of the operation, but you might want to double-check that, because the actual value will depend on the implementation of bool createfile(string path) ... ,就像其他答案中提到的那样,它可能表示操作成功或失败,但您可能需要仔细检查,因为实际值将取决于bool createfile(string path) ...

Comprehensive answer综合答案

It seems you need some help interpreting the syntax of bool createfile(string path);看来您需要一些帮助来解释bool createfile(string path);

What we need to clarify here is that in c++ (and many other languages), the first word used in the function declaration is the return type .这里我们需要澄清的是,在c++ (以及许多其他语言)中,function 声明中使用的第一个词是返回类型
You could compare this to some arbitrary mathematical function of the following form: here您可以将其与以下形式的一些任意数学 function 进行比较:here

x = a + b 

In this case, x is the result of the addition function.在这种情况下, x是加法 function 的结果。
Assuming all the elements above are numbers, we could translate this in c++ , like so:假设上面的所有元素都是数字,我们可以在c++中翻译它,如下所示:

int a = 0;
int b = 5;
int x = a + b;

We could extract the example above in a function (to reuse the addition), like so:我们可以在 function 中提取上面的示例(以重用添加),如下所示:

int add(int a, int b)
{
  return a + b;
}

and use it in the following way (with a main to put some execution context around it):并以下列方式使用它(使用main在其周围放置一些执行上下文):

int main()
{
  int x = add(0,5);
  return 0;
}

Here are some other examples of functions:以下是其他一些函数示例:

// simple non-member function returning int
int f1()
{
    return 42;
}
 
// function that returns a boolean
bool f2(std::string str)
{ 
    return std::stoi(str) > 0;
}

You'll find more details here .您将在此处找到更多详细信息。 It might seem like a lot to take in (the page is dense with information), but it is a true reference.可能看起来有很多内容(页面信息密集),但它是一个真正的参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM