社区
C语言
帖子详情
急问,怎样从文件读数据
sunyuanzeng
2003-10-28 10:36:22
void main()
{
char a[100];
ifstream inPut("sun.txt");
inPut.getline(a,10,'\n');
for(int i=0;i<10;i++)
cout<<a[i]<<endl;
}
从文件中读入十个字符,为什么输出10个'?'啊,我晕
...全文
51
6
打赏
收藏
急问,怎样从文件读数据
void main() { char a[100]; ifstream inPut("sun.txt"); inPut.getline(a,10,'\n'); for(int i=0;i<10;i++) cout<<a[i]<<endl; } 从文件中读入十个字符,为什么输出10个'?'啊,我晕
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
6 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
Andy84920
2003-10-28
打赏
举报
回复
看看你的文件中有什么内容!
CoolKin9
2003-10-28
打赏
举报
回复
你的sun.txt中有非法字符,
打开写字板,新建文本文件,
保存为xxx.txt,重新执行以上程序,
把sun.txt改为xxx.txt。
sunyuanzeng
2003-10-28
打赏
举报
回复
大哥,我怎么看不懂啊,能不能简单点,我只想知道我的程序哪里错了
Andy84920
2003-10-28
打赏
举报
回复
要看你的样子的?
Andy84920
2003-10-28
打赏
举报
回复
//
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
string ifile;
cout << "Please enter file to sort: ";
cin >> ifile;
// constructor a ifstream input file object
ifstream infile( ifile.c_str());
if( !infile)
{
cerr << "error: unable to open input file: "
<< ifile << endl;
return -1;
}
string ofile = ifile + ".sort";
// constructor a ofstream ouput file object
ofstream outfile( ofile.c_str());
if( !outfile)
{
cerr << "error: unable to open output file: "
<< ofile << endl;
return -2;
}
string buffer;
vector< string> text;
int cnt = 1;
while( infile >> buffer )
{
text.push_back(buffer);
cout << buffer << (cnt++ % 8 ? " " : "\n");
}
sort( text.begin(), text.end());
// ok: print sorted words to outfile
vector< string>::iterator iter = text.begin();
for( cnt = 1; iter != text.end(); ++iter, ++cnt)
outfile << *iter
<< (cnt % 8 ? " " : "\n");
return 0;
}
sunyuanzeng
2003-10-28
打赏
举报
回复
谢谢各位
Qt tableWidget的使用:
从文件
读数据
,动态添加到表格中显示
该博客介绍了Qt tableWidget的使用,主要是从有序文件读取数据并动态添加到表格中显示。代码实现部分提到建立表格要先设置列数,文件操作后需关闭,还解释了动态添加行的原理。最后展示了结果并给出参考。
从文件
中
读数据
并显示到控制台
该博客演示了如何使用Java的FileReader类
从文件
中读取数据并将其显示到控制台。通过创建FileReader对象,调用read()方法读取字符,并在循环中检查返回值是否为-1来判断是否达到文件末尾,实现数据的逐字符读取。示例代码展示了读取'FileWriterDemo.java'文件的过程。
fgets()函数
从文件
中
读数据
本文介绍如何使用fgets
从文件
读取数据到字符数组。通过一个简单的C语言程序示例,展示了fgets函数的三个参数及其用法,并强调了fopen函数中模式参数的重要性。
C++
从文件
中
读数据
及向文件中写数据
这篇博客介绍了如何在C++中进行文件操作,包括
从文件
中读取数据的三种方法(getline、流、istream_iterator)以及向文件写入数据的基本步骤。示例代码详细展示了读写过程,适用于基础的文件I/O学习。
十三周OJ2
从文件
中
读数据
本文介绍了一个C语言程序,用于
从文件
中读取学生信息,并计算每个班级的平均成绩。程序首先打开文件,然后逐行读取学生信息,按班级进行成绩累加,最后输出每个班级的平均成绩。
C语言
70,038
社区成员
243,247
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章