我在C++中遇到了一些奇怪的反转布尔值的问题。:')我想检查传递给main方法的参数是否不是"file.txt“。如果不是,我想创建一个新的文件,文件名作为主参数,并写入几行随机数据。否则,我想从"file.txt“中读取数据。一切都如我所愿,但当我在终端输入g++ sourcefile.cpp header.h,然后./a.out file.txt程序将这个随机数据输入到"file.txt“中,而不是从它读取。另一方面,当我输入:./a.out someRandomData.txt时,它是从"file.txt“读取的。有什么想法吗?
int m
我正在写一个C++程序,使用的是ifstream函数。我询问用户要打开哪个文件,然后询问他们如何命名该文件。请看下面的代码,帮我弄清楚如何正确使用append函数。
cout << "Please type the new name for your file ( File must end in '.txt'): ";
cin>>outf; // stores name of requested txt file
if (!outf.rfind(".txt"))
{
outf.append(".txt"
如何使用C++ >>操作符读取新行?
ifstream input("doc.txt".c_str());
vector<string> contents;
while (input >> word) {
contents.push_back(word);
}
关于档案:
hello
world
C++ is the best tool
应该回来
hello
\n
world
\n
C++
is
the
best
tool
P/S:这是一个从更大的问题减少。我解析文件的方式导致了这个问题。
在Lua中,我有这样一个函数将文件读入数组:
function readFile(file)
local output = {}
local f = io.open(file)
for each in f:lines() do
output[#output+1] = each
end
f:close()
return output
end
在C++中,我试着这样写:
string * readFile(file) {
string line;
static string output[] = {};
ifstream stream(file);
我想在C++中读取一个文本文件,我已经使用ifstream完成了。文件:
// The range of 'horizontal' indices, inclusive
// E.g. if the range is 0-4, then the indices are 0, 1, 2, 3, 4
GridX_IdxRange=0-8
// The range of 'vertical' indices, inclusive
// E.g. if the range is 0-3, then the indices are 0, 1, 2, 3
GridY_Id
例如,我有一个test.txt,包含:
apple
computer
glass
mouse
blue
ground
然后,我想从文本文件中检索一条随机行。这是我的密码:
ifstream file;
file.open("test.txt", ios::in);
char word[21];
int line = rand()%6 + 1;
for (int x = 0 ; x < line ; x++)
test.getline (word, 21);
cout << word;
问题是变量'word‘总是包含第一行,无论给定多少随机数.
我在这段代码中有一个错误:在“in>>point”中的'>>‘的while循环中。
void FileRead(const int cap,const string point,int counter)
{
ifstream in;
in.open("sample_strings.txt"); //ifstream in; in.open("sample_data.txt");
if (in.fail())
cout<<"sample_data not opened
我已经写了一个程序,用它的第一个论点和颠倒字母。因此,例如:
revstr cat
将产生tac。
现在,当文件被重定向时,我希望它能够工作。因此,如果filler.txt是一个包含“现在是所有好人帮助他们的国家的时候了!”的文件,那么:
revstr < filler.txt
应产生:
!yrtnuoc rieht fo dia eht ot emoc ot nem doog lla rof emit eht si woN
但我不知道如何发现这种重定向正在发生!
这就是我试过的-很明显,这是没用的。我哪里出问题了?
int main(int argc, char* argv[]) {