C ++文件流读取问题

时间:2015-01-21 18:56:40

标签: c++ filestream eof

所以我使用文件流来读取和写入3个二进制文件的结构向量,但是当我执行读写操作时,它会读取每个我不理解的重复项。

我认为这对我的代码来说只是一个微不足道的错误。

#include "fileh.h"
using namespace std;//create the variables used in the program
fstream filestream;
student_info temp_data;//even though its temporary its used a lot , more efficient than using multiple local declerations
void write_to_class(student_info data, int classnum){//create a function that writes to the main class data
        if (classnum == 1){//check the class numbers
                class1.push_back(data);//if the number is the one selected
        }
        else if (classnum == 2){
                class2.push_back(data);
        }
        else if (classnum == 3){
                class3.push_back(data);
        }
        else{//if all values dont work then call this
                cout << "Invalid class number!" << endl;
                return;
        }
}
bool write_to_file(){//function that writes data to a saved binary file
        int classcurrent = 1;//variable for detecting class during iteration
        vector<student_info>::iterator start;//create two iterators for the start and end positions of the class lists
        vector<student_info>::iterator end;
        while (classcurrent < 3){
                if (classcurrent == 1){//by using selection within a while loop makes the code cleaner , it also means there is less repetition with the code
                        filestream.open("class1.bin", ios::out | ios::binary | ios::trunc);//open the file for writing in a binary format trunc means all prior
                        //data is deleted
                        start = class1.begin();//set the iterators for the positions within the vector we are reading from
                        end = class1.end();
                }
                else if (classcurrent == 2){//repeat for other classes
                        filestream.open("class2.bin", ios::out | ios::binary | ios::trunc);
                        start = class2.begin();
                        end = class2.end();
                }
                else{
                        filestream.open("class3.bin", ios::out | ios::binary | ios::trunc);
                        start = class3.begin();
                        end = class3.end();
                }

                if (filestream.is_open()){//check to see if there is no error with the file
                        for (vector<student_info>::iterator position = start; position != end; position++){
                                cout << classcurrent << endl;
                                cout << position->name << endl;
                                temp_data = *position;
                                filestream.write((char *)(&temp_data), sizeof(student_info));
                        }

                }
                else{
                        return false;//if there is immediately cancel the operation and notify the calling function with a boolean
                }
                filestream.close();//close the filestream to save the operaions
                classcurrent += 1;//increase the current class to modify how the selection occurs
        }
        return true;//if nothing bad has occured then tell the calling function its completed
}
bool read_from_file(){//this function reads from a file and notifys the calling function the status afterward
        int classcurrent = 1;//same as the write function
        while (classcurrent <3){
                if (classcurrent == 1){

                        filestream.open("class1.bin", ios::in | ios::binary);//open the file for reading in binary

                }
                else if (classcurrent == 2){
                        filestream.open("class2.bin", ios::in | ios::binary);
                }

                else{
                        filestream.open("class3.bin", ios::in | ios::binary);
                }
                if (filestream.is_open()){//check if the file has been successfully opened

                        while (!filestream.eof()){


                                filestream.read((char *)(&temp_data), sizeof(student_info));//if there is still stuff to read then read the data
                                write_to_class(temp_data, classcurrent);//copy this data into the main class data
                        }
                }
                else{
                        return false;
                        //this allows for returning an error to the user if anything failed during this operation.
                }
                filestream.close();
                classcurrent += 1;

        }

        return true;
}

0 个答案:

没有答案