在c ++中在桌面上创建文件 [英] create file on desktop in c++

查看:215
本文介绍了在c ++中在桌面上创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在c ++中创建文件我们使用下面的代码

  #include< iostream> 
#include< fstream>
using namespace std;
int main(){

ofstream out;
out.open(exemple.txt);



return 0;
}

我的问题是以下我想在桌面或其他目录为简化让我们拿桌面怎么样?请帮助
像这样?

  out.open(example.txt,C:\Users \\ \\ David\Desktop); 


解决方案

代码的主要问题是' \\'是C / C ++中的转义字符。



因此,当您输入字符串时:C:\Users\David\Desktop是转义下一个字符,因此它们实际上不是字符串的一部分,可执行文件是C:UsersDavidDesktop以补偿这有两个选择: p>


  • 使用斜杠'\\'的转义序列, C:\\Users\\David\\Desktop

  • 或使用'/'字符分隔路径中的目录。

    • 我个人更喜欢这个选项,因为它现在可以在所有现代操作系统之间移植。 (Win / Linux / MAC)




您的次要问题是您正在使用打开不正确。只需将路径名称指定为1个长字符串(这称为绝对路径)。我个人喜欢提供文件名给构造函数而不是显式调用open(但这只是一个个人偏好的东西)。

  #include< fstream> 
int main()
{
std :: ofstream out1(C:\\Users\\David\\Desktop\\\exemple1.txt) ;

std :: ofstream out2(C:/Users/David/Desktop/exemple2.txt);
}

一个小笔记。硬编码到桌面目录的路径不是好的做法。您将应用程序紧密耦合到该版本的操作系统如何构建文件系统。每个操作系统通常提供一种如何找到用户目录的技术,有关更多详细信息,请参阅操作系统文档(或在StackOverflow上提出另一个问题)。



系统类抽象所有主要操作系统的文件系统。它有用的是知道它是如何工作,而不是表示文件作为字符串;当构建复杂路径(例如,具有空格的路径)时,字符串表示可能会产生轻微的错误。字符嵌入其中。


i know that to create file in c++ we use following code

#include <iostream>
#include <fstream>
using namespace std;
int main(){

ofstream out;
out.open("exemple.txt");



 return 0;
}

my question is following i want create example.txt file on desktop or in other directory for simplify let's take desktop how? please help like this?

out.open("example.txt","C:\Users\David\Desktop");

解决方案

The main problem with your code is that the '\' is the escape character in C/C++.

So when you put the string: "C:\Users\David\Desktop" The slashes are escaping the next character and thus they are not actually part of the string and what the executable gets is "C:UsersDavidDesktop" to compensate for this there are two alternatives:

  • Use the escape sequence for the slash '\\' thus giving you: "C:\\Users\\David\\Desktop"
  • Or use the '/' character to separate directories in the path.
    • Personally I prefer this option as it is portable between all modern OS's now. (Win/Linux/MAC)

Your secondary problem is that you are using the open incorrectly. Just specify the path name as 1 long string (this is called an absolute path). Personally I prefer to provide the file name to the constructor rather than explicitly calling open (but that's just a personal preference thing).

#include <fstream>
int main()
{
    std::ofstream out1("C:\\Users\\David\\Desktop\\exemple1.txt");

    std::ofstream out2("C:/Users/David/Desktop/exemple2.txt");
}

A minor note. Hard coding the path to the desktop directory is not good practice. You are tightly coupling your application to how that version of the OS lays out the file system. Each OS usually provides a technique on how to find users directories please see your OS documentation for more details (or ask another question on StackOverflow).

Also note boost provides a file system class to abstract the file system across all major OS's. Its useful to get to know how it works rather than representing files as strings; the string representation can get slightly error prone when you build complex paths (eg paths with spaces). characters embedded into them.

这篇关于在c ++中在桌面上创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆