使用 ifstream 从文本文件读取
Reading from text file using ifstream
我正在尝试使用带有提取运算符(>>)的ifstream对象从文本文件中读取,当我执行以下代码时,它根本没有读取!
#include <iostream>
#include <fstream>
#include <iomanip>
void outputLine( int account, const char *const name , double balance )
{
cout << left << setw( 10 ) << account << setw( 13 ) << name
<< setw( 7 ) << setprecision( 2 ) << right << balance << endl;
} // end function outputLine
int main()
{
ofstream outfile("client.txt",ios::out);
if (!outfile)
{
cout << "the file is not opened .. " << endl ;
exit(1);
}
int account;
char name[30];
double balance;
while (cin >> account >> name >> balance)
{
outfile << account << ends << name << ends << balance << endl ;
cout << "? " ;
}
outfile.close();
ifstream inFile;
inFile.open( "client.txt",ios::in);
if ( !inFile )
{
cerr << "File could not be opened" << endl;
exit( 1 );
} // end if
cout << left << setw( 10 ) << "Account" << setw( 13 )
<< "Name" << "Balance" << endl << fixed << showpoint;
while (inFile >> account >> name >> balance )
{
outputLine( account, name, balance );
}
return 0 ;
}
此代码中是否有任何错误?
那些ends
操纵者在那里做什么?我猜你应该输出空格。调试它的明显方法是编写仅创建输出文件的代码,然后通过使用文本编辑器检查输出来查看它是否有效。
它可能不是在阅读,因为它可能不是在写作。当控制台窗口要求您输入时,您没有指定在控制台窗口中输入的内容,但是如果您没有以
integer string double
然后不会向文件写入任何内容,因为cin<<
将尝试获取某种类型并失败,因为数据与它想要的类型不匹配。
相关文章:
- 正在将csv文件读取为双精度矢量
- 将从格式化文本文件读取的文本数据存储到链表
- C++ 读取文件读取文件不正确
- 使用对象文件读取三角形数据网格
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 从文件读取,并写入函数C++中的数组
- 从对象C++中的文件读取内容时出现分段错误
- 从文件读取并通过矢量输出到控制台
- 尝试将字符串从文件读取到无符号字符向量中
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 简化文件读取的函数
- 如何将逗号分隔的文件读取为 2D int 数组?
- 在 c++ 中将数据从文件读取到队列中
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 文件读取C++包括空格键
- 从文件读取时未将值推回矢量
- 从文件 C++ 读取时的无限循环
- 从同一文件读取和写入?
- 从.txt文件读取到C++中的双精度向量