fstream不解析路径 [英] fstream doesn't resolve path

查看:271
本文介绍了fstream不解析路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的到c + +,并试图写一个简单的函数,将一个字符串保存到文件。
该函数工作,当我通过完整的路径到fstream,但它不解析相对路径。



这是我的代码的相关部分

  #include< iostream> 
#include< fstream>

void writeToFile()
{
std :: fstream fs;
fs.open(/home/blabla/Documents/test.txt,std :: fstream :: in | std :: fstream :: out | std :: fstream :: app);
fs<< 测试内容;

fs.close();
}



这个工作正常,但我想在文件夹中创建文件,所以我试过这个

  fs.open(./test.txt,std :: fstream :: in | std :: fstream :: out | std :: fstream :: app); 

我也试过了

  fs.open(〜/ Documents / test.txt,std :: fstream :: in | std :: fstream :: out | std :: fstream :: app); 

他们都没有创建新文件,我没有得到任何错误信息。



我发现这个帖子,这表明,我可以传递相对路径到fstream,但只给出了窗口的例子。
fstream 的相对路径



我在Linux Mint上工作,目标环境是debian。



我非常感谢任何提示或建议,
Michael

$ b您的计划

  #include< iostream> ; 
#include< fstream>

void writeToFile(){
std :: fstream fs;
fs.open(./test.txt,std :: fstream :: in | std :: fstream :: out | std :: fstream :: app);
fs<< 测试内容;

fs.close();
}

int main(){
writeToFile();
}

对我来说在 Coliru



正如你可以从 ls 输出,文件已创建

  a.out<可执行程序
main.cpp<<源
test.txt<<创建的文本文件

cat 转储文件的内容

 测试内容

如我在注释中提到的,〜/ Documents / test.txt 的路径由shell评估,而 ./ test.txt 如果你在同一个文件所在的目录下运行你的程序,应该可以使用现有的文件。


他们都没有创建新文件,我没有收到任何错误信息。


如果错误讯息 / openrel =nofollow> std :: fstream :: open() 由于某种原因失败。

检查该调用之后的流状态,例如

  if(!fs){std :: cerr< 无法打开文件<< std :: endl; } 



另一个选择是使用 std :: basic_ios :: exceptions() std :: fstream()的函数。


i am new to c++ and trying to write a simple function, that saves a string to a file. The function works, when i pass the full path to fstream, but it doesn't resolve relative paths.

Here is the relevant part of my code

#include <iostream>
#include <fstream>

void writeToFile ()
{
    std::fstream fs;
    fs.open ("/home/blabla/Documents/test.txt", std::fstream::in | std::fstream::out | std::fstream::app);
    fs << " test content";

    fs.close();
}

This works fine, but i would like to create the file in the folder, where my program is executed, so i tried this

fs.open ("./test.txt", std::fstream::in | std::fstream::out | std::fstream::app);

I also tried

fs.open ("~/Documents/test.txt", std::fstream::in | std::fstream::out | std::fstream::app);

Neither of them created a new file and i did not get any error message.

I found this post, which suggests, that i can pass relative paths to fstream but only gives windows examples. Relative path for fstream

I work on Linux Mint, the target environment is debian.

I am thankful for any hints or suggestions, Michael

解决方案

Your program

#include <iostream>
#include <fstream>

void writeToFile () {
    std::fstream fs;
    fs.open ("./test.txt", std::fstream::in | std::fstream::out | std::fstream::app);
    fs << " test content";

    fs.close();
}

int main() {
    writeToFile();
}

works perfectly fine for me on Coliru.

As you can see from the ls output, the file was created

a.out      << The executable program
main.cpp   << The source
test.txt   << The created text file

and cat at least dumps the content of the file

 test content

As mentioned in my comment, a path like ~/Documents/test.txt is evaluated by the shell, while ./test.txt should work for openting an existing file, if you're running your program in the same directory where the file exists.

"Neither of them created a new file and i did not get any error message."

You'll never get any kind of error message, if std::fstream::open() failed for some reason.
You have to check for the stream state after that call, like e.g.

 if(!fs) { std::cerr << "Could not open file" << std::endl; }

You may e.g. not have rights to create a file in this directory.

Another option is to use the std::basic_ios::exceptions() function of std::fstream(), to trigger an exception when the stream state runs into an error condition.

这篇关于fstream不解析路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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