QT创建者和ifstream filename .c_str()
QT creator and ifstream filenam.c_str()
我用QT creator 4.1.0创建了一个c++控制台应用程序这个应用程序最初是用代码块创建的,工作得很好。
这是代码
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string nomeFile="";
cout << "nNome file: ";
cin >> nomeFile; // inserire nome file ROF.txt da leggere
//ifstream leggiROF(nomeFile.c_str()); // apre in lettura il file ROF.txt
ifstream leggiROF("C:\Users\Massimo Di Natale\Documents\Programmi C++ 11\Programmi_QT\prova\a.txt");
string riga="";
if(leggiROF) // se lo trova
{
while(!leggiROF.eof()) // finché non raggiunge la fine del file
{
getline(leggiROF, riga); // legge la riga
cout << "Riga: " << riga << endl;
} // end while
} // end if
else // altrimenti
{
cout << "nIl nome file inserito non e' stato trovato!!!" << endl;
cout << "Controllare che sia stato inserito nella cartella corretta.n" << endl;
exit (EXIT_SUCCESS); // termina il programma
} // end else
leggiROF.close();
return 0;
}
如果我在ifstream上指定文件名和它的路径,它会工作。如果我想通过cin插入文件名,然后使用ifstream filename .c_str(),它找不到文件。我想从项目目录或。exe目录中读取。txt文件
尝试getline(cin,nomeFile);
代替cin >> nomeFile
。在"std::cin"处回答。Getline()与std::cin"问题描述了它们之间的区别
相关文章:
- Python str to C++ to Python str
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- 字符串变量,比如说"字符串str",可以直接复制到数组中吗?
- 解释一下 for (char c : str) 的作用?
- 返回 str vs. str.substr(0,str.size()) 在 leetcode 中给了我不同的输出
- remove(str.begin(), str.end(), );无法正常工作(我正在使用视觉工作室 2012)
- Getline(cin,str)的奇怪输出
- 比使用 s.str().c_str() 更好的表达?
- 除以 str.length() 什么也没显示
- 没有'str'成员在 GCC 和 Clang 'std::basic_ostream<char>',但 MSVC 没有问题
- 我可以得到一个字符 * 到一个 std::sregex_iterator 匹配 str() 吗?
- C6001:使用单元化内存"str[i]"
- 而(getline(fin,str)){}即使在到达eof之后仍在处理数据
- 在给定的代码中,有人可以解释一下(int i = 0; i<len; i++)count[str[i]]++的代码片段;
- 在函数内部传递字符 *str
- rdbuf()->str()在哪里?
- std::smatch str() 未返回正确的字符串
- 这是 boost::filesystem 中的错误吗?为什么 boost::filesystem::p ath::str
- STR这个实现是否安全且可移植?
- 'for (auto c : str)' c到底是什么?