如何将信息从二进制文件发送到屏幕和外部文件?

时间:2014-10-31 18:43:05

标签: c++ function struct binary binaryfiles

在我的一个项目中,我必须从二进制文件中读取4个单独的结构,然后将信息发送到屏幕和外部文件。有人可以告诉我如何做到这一点?我相信我已经阅读过这些文件了。

另外,当我编译并运行程序时,我给用户和选项继续(简单的做'Y')。如果用户输入无效字符,我想要无效的响应。请回答“Y”表示“是”,回答“N”表示没有“出现。确实这样做但是在输出之后,系统(”暂停“)将采取行动并按任何继续将显示表示终止我很困惑为什么有人也帮助我。如果我输入Y或N,它将退出程序。

非常感谢

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <cctype>

using namespace std;

const int SIZE = 12;


struct Division
{
char divName [SIZE];
double sales [4];
};

int main()
{
Division Div_E; 
Division Div_W; 
Division Div_N; 
Division Div_S;

void Describe_Program ();
void Create_Corporate_File (Division& mainDiv);
void Display_Corporate_Sales (Division& Div_E, Division& Div_W, Division& Div_N, Division& Div_S);


Describe_Program();
         string Div; 
         char ans;
do
{  
    cout << "Enter the name of the division: \n";
    getline (cin,Div); 
     cin.ignore();
        if (Div == "East")
        {  
                Create_Corporate_File (Div_E);
                }      

        else if (Div == "West")
        { 
             Create_Corporate_File (Div_W);
             }
        else if (Div == "North")
        {
             Create_Corporate_File (Div_N);
             }
        else if (Div == "South")
        {
             Create_Corporate_File (Div_S);
             }
             else 
             {

              }

             cout << "\nWould you like to continue? (Y/N)--\n";  

              cin>>ans; 
              ans = toupper (ans);
               if (ans != 'Y' && ans != 'N')
               {
                       cout << "Invalid response. Please answer 'Y' for \n"; 
                       cout << "Yes and 'N' for no.\n" <<endl; 
                       } 

//cin.ignore();


}
        while (ans == 'Y');      

//Display_Corporate_Sales(Div_E, Div_W, Div_N, Div_S); 
system ("PAUSE");
return 0; 
}


void Describe_Program()
{ 
    cout << "This program will prompt you to enter the sales amount\n";
    cout << "for each quarter of the North, South, West and East Division.\n";
    cout << "After you enter the sales amount for each quarter, the program\n";
    cout << "will be able to calculate the total quarterly sales for that";
    cout << "division"; 
    cout << endl<<endl; 
         return; 
} 

void Create_Corporate_File (Division& mainDiv)
{
     fstream quarterly_sales; 
     quarterly_sales.open ("quarterly_sales.dat" , ios::out|ios::binary);

          for(int sales = 0; sales < 4; sales++)
          {         
    cout << "Please enter the sales amount for quarter " << sales + 1 <<":\n";
    cin >> mainDiv.sales [sales]; 
        }


     quarterly_sales.write((char*)(&mainDiv), sizeof(mainDiv));
     quarterly_sales.close (); 
}

void Display_Corporate_Sales (Division& Div_E, Division& Div_W, Division& Div_N, Division& Div_S)
{
     fstream quarterly_sales; 
     quarterly_sales.open ("quarterly_sales.dat" , ios::in|ios::binary);      


     quarterly_sales.read ((char *)(&Div_E), sizeof(&Div_E));
     cout << Div_E.divName ;
     cout << Div_E.sales[0];  

     quarterly_sales.read (reinterpret_cast<char *>(&Div_W), sizeof(&Div_W))
     cout << Div_W.divName ;
     cout << Div_W.sales[0];   

     quarterly_sales.read (reinterpret_cast<char *>(&Div_N), sizeof(&Div_N))
     cout << Div_N.divName ;
     cout << Div_N.sales[0];   

     quarterly_sales.read (reinterpret_cast<char *>(&Div_S), sizeof(&Div_S))
     cout << Div_S.divName ;
     cout << Div_S.sales[0];   


    quarterly_sales.close(); 
    return; 
     }

1 个答案:

答案 0 :(得分:1)

虽然我不打算给你的作业代码答案,但你正在寻找的命令是cout(写入屏幕)和文件IO命令(参见以下链接和#34下显示的示例) ;文本文件&#34;):

http://www.cplusplus.com/doc/tutorial/files/