这对你来说可能是一个非常无聊的问题:如何(如果可能的话)我可以从函数中返回ifstream吗?
基本上,我需要从用户那里获取数据库的文件名,如果不存在具有该文件名的数据库,那么我需要为用户创建该文件.我知道怎么做,但只是要求用户在创建文件后重新启动程序.我希望尽可能避免给用户带来不便,但下面的函数不能在gcc中编译:
ifstream getFile() {
string fileName;
cout << "Please enter in the name of the file you'd like to open: ";
cin >> fileName;
ifstream first(fileName.c_str());
if(first.fail()) {
cout << "File " << fileName << " not found.\n";
first.close();
ofstream second(fileName.c_str());
cout << "File created.\n";
second.close();
ifstream third(fileName.c_str());
return third; //compiler error here
}
else
return first;
}
Run Code Online (Sandbox Code Playgroud)
编辑:对不起,忘了告诉你编译器错误在哪里以及是什么:
main.cpp:45: note: synthesized method ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)’ first required here
Run Code Online (Sandbox Code Playgroud)
编辑:我更改了函数以返回指针而不是Remus建议,并将main()中的行更改为"ifstream database =*getFile()"; 现在我再次收到此错误,但这次是在main()中的行:
main.cpp:27: note: synthesized …Run Code Online (Sandbox Code Playgroud) 每当我通过fstream阅读时,我最后会得到1个额外的字符,我该如何避免这种情况?
编辑:
ifstream readfile(inputFile);
ofstream writefile(outputFile);
char c;
while(!readfile.eof()){
readfile >> c;
//c = shiftChar(c, RIGHT, shift);
writefile << c;
}
readfile.close();
writefile.close();
Run Code Online (Sandbox Code Playgroud) 正如标题所说,你如何使用fstream?读取十六进制值?
我有这个代码:(假设我们在文件中有"FF".)
fstream infile;
infile.open(filename, fstream::in|fstream::out|fstream::app);
int a;
infile >> std::hex;
infile >> a;
cout << hex << a;
Run Code Online (Sandbox Code Playgroud)
但这并没有给我任何输出而不是ff.我知道有一个,fscanf(fp, "%x", val)但我很好奇有没有办法使用流库来做到这一点.
更新:
我的代码一直都是正确的,事实证明我的错误是我无法阅读"FFF"并将其放在变量a,b,c中
while (infile >> hex >> a >> b >> c)
{
cout << hex << a << b << c << "\n";
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮我这个吗?我是否必须将我想要阅读的每个HEX值与空格分开?因为infile >> hex >> setw(1)不起作用..
Linux中用于C++中的低级磁盘操作有哪些方法?我试图在磁盘上编写自己的数据管理器.例如,我想在Linux环境中创建一个C++程序,在磁盘上分配一定量(连续),然后自由地允许我读/写该数据块.我不认为我想使用该标准,fstream::open因为该文件由操作系统管理,我可能无法在磁盘上获得连续的部分.
谢谢.
如何确定天气ostream是文件或控制台流.在下面的程序中我想打印"Hello file!" 写入文件和"Hello console!"时 在写入控制台时.我应该在第17行指定什么条件?
#include <fstream>
#include<iostream>
#include <string>
using namespace std;
class A{
public:
A(string msg):_str(msg){}
string str()const {return _str;};
private:
string _str;
};
ostream & operator << (ostream & os, const A & a)
{
if (os is ofstream) //this is line 17
os << "Hello file! " << a.str() << endl;
else
os << "Hello console! " << a.str() << endl;
return os;
}
int main()
{
A a("message");
ofstream ofile("test.txt");
if (!ofile)
cerr << "Unable …Run Code Online (Sandbox Code Playgroud) 我正在使用C++ fstream来读取配置文件.
#include <fstream>
std::ifstream my_file(my_filename);
Run Code Online (Sandbox Code Playgroud)
现在,如果我传递一个目录的路径,它会默默地忽略它.my_file.good()即使my_filename是目录,Eg 也会返回true .由于这是我的程序的意外输入,我喜欢检查它,并抛出异常.
如何检查刚打开的fstream是否是常规文件,目录或流?
我似乎无法找到一种方法:
在一些论坛讨论中,有人认为这两者都不可能,因为这是依赖于操作系统的,因此永远不会成为fstream C++标准的一部分.
我能想到的唯一选择是重写我的代码以完全摆脱ifstream并使用文件描述符(*fp)的C方法,以及fstat():
#include <stdio.h>
#include <sys/stat.h>
FILE *fp = fopen(my_filename.c_str(), "r");
// skip code to check if fp is not NULL, and if fstat() returns != -1
struct stat fileInfo;
fstat(fileno(fp), &fileInfo);
if (!S_ISREG(fileInfo.st_mode)) {
fclose(fp);
throw std::invalid_argument(std::string("Not a regular file ") + my_filename);
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢fstream.因此,我的问题.
我正在使用fstream和C++,我希望我的程序要做的就是将终止的.txt文件的内容打印到终端.这可能很简单,但我在网上看了很多东西,我找不到任何可以帮助我的东西.我怎样才能做到这一点?这是我到目前为止的代码:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
string output;
ifstream myfile;
ofstream myfile2;
string STRING;
myfile.open ("/Volumes/LFARLEIGH/Lucas.txt");
myfile2 << "Lucas, It Worked";
myfile >> STRING;
cout << STRING << endl;
myfile.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
先感谢您.如果这很简单,请原谅我,因为我对C++很新
#include <iostream>
#include <fstream>
using namespace std;
class info {
private:
char name[15];
char surname[15];
int age;
public:
void input(){
cout<<"Your name:"<<endl;
cin.getline(name,15);
cout<<"Your surname:"<<endl;
cin.getline(surname,15);
cout<<"Your age:"<<endl;
cin>>age;
to_file(name,surname,age);
}
void to_file(char name[15], char surname[15], int age){
fstream File ("example.bin", ios::out | ios::binary | ios::app);
// I doesn't know how to fill all variables(name,surname,age) in 1 variable (memblock)
//example File.write ( memory_block, size );
File.close();
}
};
int main(){
info ob;
ob.input();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何将1个以上的变量写入文件,请帮助,我包含了一个例子;)也许有更好的方法来写一个文件,请帮我这个,这对我来说很难解决.
最近我决定优化我正在做的一些文件读取,因为正如大家所说,将大量数据读取到缓冲区然后使用它比使用大量小读取更快.而且我的代码现在肯定要快得多,但在进行一些分析之后,似乎memcpy占用了大量的时间.
我的代码的要点是......
ifstream file("some huge file");
char buffer[0x1000000];
for (yada yada) {
int size = some arbitrary size usually around a megabyte;
file.read(buffer, size);
//Do stuff with buffer
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Visual Studio 11,在对我的代码进行分析后,它ifstream::read()最终会调用xsgetn()从内部缓冲区到我的缓冲区的副本.此操作占用超过80%的时间!排在第二位的是uflow()10%的时间.
有什么方法可以绕过这个复制吗?我可以以某种方式告诉我ifstream将我需要的大小缓冲到我的缓冲区吗?C风格FILE*也使用这样的内部缓冲区吗?
更新:由于人们告诉我使用cstdio ...我做了一个基准测试.
编辑:不幸的是旧的代码充满了失败(它甚至没有读取整个文件!).你可以在这里看到它:http://pastebin.com/4dGEQ6S7
这是我的新基准:
const int MAX = 0x10000;
char buf[MAX];
string fpath = "largefile";
int main() {
{
clock_t start = clock();
ifstream file(fpath, ios::binary);
while (!file.eof()) {
file.read(buf, MAX);
}
clock_t end …Run Code Online (Sandbox Code Playgroud) 我正在使用googlemockstd::fstream在我的单元测试中模拟出一个对象,如下所示:
TEST_F(SomeTest, SomethingIsDoneCorrectly)
{
class MockFstream : public std::fstream {};
MockFstream lMockFstream;
// Expectations and assertions here
}
Run Code Online (Sandbox Code Playgroud)
当我编译时,我得到以下警告:
警告1警告C4250:'SomeTest_SomethingIsDoneCorrectly_Test :: TestBody :: MockFstream':通过优势继承'std :: basic_istream <_Elem,_Traits> :: std :: basic_istream <_Elem,_Traits> :: _ Add_vtordisp1'
警告2警告C4250:'SomeTest_SomethingIsDoneCorrectly_Test :: TestBody :: MockFstream':通过优势继承'std :: basic_ostream <_Elem,_Traits> :: std :: basic_ostream <_Elem,_Traits> :: _ Add_vtordisp2'
我更喜欢干净的构建输出,所以我想要抑制这些特定的警告,但我正在编写跨平台代码,所以我也更愿意避免使用特定于编译器的#pragmas.
我可以在googlemock对象中做些什么来隐藏这些警告吗?