用c++处理文件的问题

vOLCANO 2001-09-22 05:19:41
最近一直在做c++处理文件的工作。一直不得法,那位大侠有这个方面的例子,请给一个。
最近这些程序还是用c语言做的。现在我不想再用c了,想改到c++上,想用流或者其它方式也可以,不过希望用c++而不是c的文件。
...全文
107 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
阎罗 2001-09-23
  • 打赏
  • 举报
回复
setw(n)是设域宽为n个字符,setprecision(n)设置显示小数精度为n位关于这些你可以去看看i/o流的常用控制符
vOLCANO 2001-09-23
  • 打赏
  • 举报
回复
哦。这个看得懂,对我帮助不大。我是要做读后处理在输出!谢谢你的程序!!!
对了那个setw(20) 可以控制读入的个数么?如果可以好像用的上。其实我的程序就是
对文件的某一列进行统计,该列位置因该可以变动,格式不一定是文本的。可能是bcd等格式!简单的应用。不过对流的输入输出不懂。如果象c语言中struct的方式读写文件应该如何做?
vOLCANO 2001-09-23
  • 打赏
  • 举报
回复
谢谢,用在输入和可以?是吧。我看看!
阎罗 2001-09-22
  • 打赏
  • 举报
回复
给你一个例子吧,是关于输出三个学生对象的
//................................
// bob.prj
//................................
bob.cpp
student.cpp
master.cpp

//*******************************
//****** student.h ********
//*******************************

#include <iostream.h>
#include <string.h>
#ifndef STUDENT
#define STUDENT


class Student
{
public:
Student(char* pS, unsigned num, float g)
{
strcpy(pName,pS);
uID=num;
grade=g;
}
virtual void display(ostream& out);
protected:
char pName[20];
unsigned int uID;
float grade;
};
ostream& operator<<(ostream& out, Student& st);
#endif

//********************************
//***** student.cpp *******
//********************************

#include "student.h"
#include <iomanip.h>
#include <iostream.h>

void Student::display(ostream& out)
{
out <<set iosflags(ios::left)<< setw(20) <<pName
<<uID <<","
<<set iosflags(ios::right)<< setw(4) <<grade;
}

ostream& operator<< (ostream& out, Student& st)
{
st.display(out);
out <<endl;
teturn out;
}

//*********************************
//***** master.h *********
//*********************************


#include "student.h"
#include <iostream.h>


class MasterStudent: public Student
{
public:
MasterStudent(char* pS, unsigned num, float g, char t)
:Student(pS,num,g),type(t){}
void display(ostream& out);
protected:
char type;
};



//*********************************
//****** master.cpp *******
//*********************************

#include <iostream.h>
#include "master.h"

void MasterStudent::display(ostream& out)
{
Student::display(out);
out<<"," <<type;
}

//********************************
//******* bob.cpp *********
//********************************

#include <fstream.h>
#include "student.h"
#include "master.h"

void main()
{
ofstream out("e:\\bctemp \\abc.txt");

Student s1("bob fallen",12345,3.3);
MasterStudent s2("welkin sun",12667, 4.3, 'A');
MasterStudent s3("jeeps xu ", 12456, 3.7,'B');

out <<s1;
out <<s2;
out <<s3;
}

结果可以在打开的文件中看到:
e:\>type abc.txt
bob fallen 12345,3.3
welkin sun 12667,4.3,a
jeeps xu 12456,3.7,b
vOLCANO 2001-09-22
  • 打赏
  • 举报
回复
哦。只要是c++处理文件比如
这个
1231 123123 123123 1313
1231 32423 32424 432
3244 432 242342 4234

我想用c++对第三列做一下统计如何做?如果是二进制的又如何做?
阎罗 2001-09-22
  • 打赏
  • 举报
回复
你想要什么例子啊
vOLCANO 2001-09-22
  • 打赏
  • 举报
回复
有没有帮着看一下?连看的都没有啊?怎么能啊?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧