无法正确输出文件
can't get file to output correctly
我正在编写一个非常简单的程序,它使用文件来组织hello world。请记住,我希望问候和世界是分开的。
这是以下代码:
int main()
{
std::ofstream someFile("file.dat");
someFile << "" << std::endl;
std::fstream someOtherFile("file.dat",ios::in | ios::out);
std::string content;
someOtherFile << "hello" << std::endl;
someOtherFile << "world" << std::endl;
someOtherFile.seekg(0, ios::beg);
std::getline(someOtherFile, content);
std::cout << content << std::endl;
return 0;
}
然而,每当我运行以下程序时,它只打印"hello"。
任何帮助都将不胜感激,请举一个使用fstream的例子,而不是使用ofstream或ifstream(我正在努力学习fstream是如何工作的,但发现有点麻烦(。
我的编译器是最新的VS.
getine
函数每次只读取一行,所以应该调用getline
直到文件结束。下面的代码可以帮助你。
#include <iostream>
#include <fstream>`
#include <string>
using namespace std;
int main()
{
std::ofstream someFile("file.dat");
someFile << "" << std::endl;
std::fstream someOtherFile("file.dat",ios::in | ios::out);
std::string content;
someOtherFile << "hello" << std::endl;
someOtherFile << "world" << std::endl;
someOtherFile.seekg(0, ios::beg);
while(std::getline(someOtherFile, content))
{
std::cout << content << std::endl;
}
return 0;
}
您有两行代码:
someOtherFile << "hello" << std::endl;
someOtherFile << "world" << std::endl;
他们将两行字符串放入文件.dat:
// file.dat
hello
world
函数"getline(("只从文件中获取1行。"seekg"函数将读取位置设置为文件的第一行:其中包含"hello"。
如果你想读到文件的末尾:然后替换:
std::getline(someOtherFile, content);
std::cout << content << std::endl;
带有:
while (!someOtherFile.eof())
{
std::getline(someOtherFile, content);
std::cout << content << std::endl;
}
如果只需要特定的行,也可以使用计数器变量。
顺便说一句,我只是假设你想把变量"content"放在"name"所在的地方。
std::getline只从特定文件中获取一行文本。像http://www.cplusplus.com/reference/string/string/getline/?kw=getline说:
istream& getline (istream& is, string& str);
从
is
中提取字符并将其存储到str
中,直到找到定界字符delim(或换行符'\n',表示(2((。
在第一组getline和cout之后添加另一个getline(..(和cout语句。你会得到世界作为输出。
someOtherFile << "hello" << std::endl;
someOtherFile << "world" << std::endl;
someOtherFile.seekg(0, ios::beg);
std::getline(someOtherFile, content);
std::cout << content << std::endl;
std::getline(someOtherFile, content);
std::cout << content << std::endl;
getline只获取文件中的一行。要接下一个电话,你需要再打一次。
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
std::ofstream someFile("file.dat");
someFile << "" << std::endl;
someFile.close();
std::fstream someOtherFile("file.dat",ios::in | ios::out);
std::string content;
someOtherFile << "hello ";
someOtherFile << "world" << std::endl;
someOtherFile.close();
someOtherFile.seekg(0, ios::beg);
std::getline(someFile1, content);
std::cout << content << std::endl;
someFile1.close();
return 0;
}
这将打印您想要的答案
相关文章:
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- VI工作室代码错误无法打开输出文件主.exe
- 在代码块中查找输出文件
- 将未签名的字符写入输出文件
- 为什么我的代码不写入输出文件?
- 完成搜索任务时为空输出文件
- 如何将地图<列表<string>、矢量<string>>输出到输出文件?
- 无法在代码块 IDE 中打开输出文件.exe
- C++:更改 IF 语句中的输出文件
- 编译 gsoap 输出文件 (onvif) 时出现问题
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- while 循环中的流输出文件() 通常生成空文件
- 如何使用 Docker 访问输出文件
- 提升进程间共享内存删除、权限和输出文件
- 查找素数,然后将质数放入输出文件中
- GDB / C++ - 根据循环条件将多个变量记录到输出文件中
- 带有文件结束函数的 while 循环重复输出文件中的最后一个数字两次
- 我的输出文件未显示解决方案
- 输入文件中的第一个字符是输出文件中的最后一个字符,反之亦然
- C++,我正在尝试创建一个有效的.bmp输出文件