C ++:读取数据和输出数据 [英] C++: Reading Data and Outputting Data

查看:231
本文介绍了C ++:读取数据和输出数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写两个方法。 One,ReadData(istream&)读取学生的ID号,姓名,10个程序分数,中期和考试分数(最后两个整数),如果所有数据都读取成功,则返回true,否则返回false, (ostream&)以上面列出的顺序写入一个新文件中的数据。



我是一个全新的文件读写器,所有的帮助是非常赞赏。
我使用的数据看起来像这样...(组成名称和分数)

  10601 ANDRES HYUN 88 91 94 94 89 84 94 84 89 87 89 91 
10611 THU ZECHER 83 79 89 87 88 88 86 81 84 80 89 81
10622 BEVERLEE WAMPOLE 95 92 91 96 99 97 99 89 94 96 90 97
10630 TRUMAN SOVIE 68 73 77 76 72 71 72 77 67 68 72 75

有...

  #include< iostream> 
#include< fstream>
#include< string>
using namespace std;

ifstream ReadData;
ReadData.open(filename.txt);
if ReadData ... ///不知道如何让它返回true或false
ReadData.close();

ostream WriteData;
for(k = 0,k <101,k ++)
//如何从这里输出到新文件?
WriteData.close();


解决方案

使用这些更好的控制WriteData等):

  if(handle.is_open()).. //检查文件是否打开或关闭
if(handle.good()).. //检查流是否准备好输入/输出
if(handle.bad()).. //检查读/写操作是否失败
if (handle.fail()).. //同样坏,但捕获格式错误
if(handle.eof()).. //如果打开的文件已经到达文件结束返回true

输出数据的可能方法:

  WriteData.write(buffer,size); // char * buffer,int size(of buffer)

另一个:

  for(int i = 0; i   

如果数据在字符串中,您可以执行:

  WriteData<< str; 






有一个很好的教程这里在c ++和文件上。


I'm attempting to write two methods. One, ReadData(istream&) to read in student's ID number, first and last names, 10 program scores, and midterm and exam scores(last two integers) and returns true if all data was read successfully, false otherwise, and one, WriteData(ostream&) to write the data that was read in to a new file in the same order listed above.

I am completely new to file reading and writing so any and all help is much appreciated. The data I am using looks like this...(made up names and scores)

10601   ANDRES HYUN 88 91 94 94 89 84 94 84 89 87 89 91 
10611   THU ZECHER 83 79 89 87 88 88 86 81 84 80 89 81 
10622   BEVERLEE WAMPOLE 95 92 91 96 99 97 99 89 94 96 90 97 
10630   TRUMAN SOVIE 68 73 77 76 72 71 72 77 67 68 72 75 

So far I have...

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

ifstream ReadData;
ReadData.open("filename.txt");
if ReadData... ///Not sure how to make it return true or false 
ReadData.close();

ostream WriteData;
for (k=0,k<101,k++)
//how do you output to a new file from here?
WriteData.close();

解决方案

Use these for better control (handle could be ReadData or WriteData e.t.c):

if( handle.is_open() ) .. // checks if file is open or closed
if( handle.good() ) .. // checks if stream is ready for input/output 
if( handle.bad() ) .. // checks if read/write operation failed
if( handle.fail() ) .. // same as bad, but catches format error
if( handle.eof() ) .. // returns true if opened file has reached the "end of file"

Possible way to output data:

WriteData.write(buffer, size); // char *buffer, int size (of buffer)

Another:

for(int i = 0; i<size; ++i) WriteData<<buffer[i];

If the data is in a string you can just do:

WriteData << str;


There is a great tutorial here on c++ and files.

这篇关于C ++:读取数据和输出数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆