2013-11-20 32 views
-1

我想将movies_iit结构的6部电影的内容写入我的文件fout(“data.dat”)中。通过fstream将结构中的文本写入文件

// array of structures 
#include <iostream> 
#include <string> 
#include <sstream> 
#include <istream> 
#include <stdio.h> 
#include <cctype> 
#include <fstream> 
using namespace std; 

#define NUM_MOVIES 6 
//################################################################################################### ################ 
//structure 
//################################################################################################### ################ 
struct movies_iit{ 
    string title; 
    int year; 
} films [NUM_MOVIES]; 
//global variables 
char title [20], y, n; 
int year; 
string search; 
//function 1 
void sort_on_title(movies_iit films[], int n) 
{ 
//Local struct variable used to swap records 
movies_iit temp;  

for(int i=0; i<n-1; i++) 
    { 
for(int i=0; i<n-1; i++) 
     { 
/*If s[i].title is later in alphabet than 
s[i+1].title, swap the two records*/ 
     if(films[i].title>films[i+1].title) 
      { 
       temp = films[i]; 
       films[i] = films[i+1]; 
       films[i+1] = temp; 
      } 
     } 
    } 
} 
//end function 1 
//function query1 prototype 
void query1 (movies_iit movie); 
//function query2 prototype 
void query2 (movies_iit movie); 
//function 2 prototype 
void printmovie (movies_iit movie); 
//################################################################################################### ################ 
//beginning of main 
//################################################################################################### ################ 
int main() 
{ 
//login 
//username: user 
//password: word 
string mystr; 
int n; 
char response; 
string pass; 
string name; 
//output object 
ofstream fout ("data.dat"); 
cout << "enter your username "<<endl; 
cin >> name; 
cout << "enter your password "<<endl; 
cin >> pass; 

cout << "\n" << endl; 

if (name == "user" && pass == "word") 
    cout << "Welcome, user." << endl; 
else 
{cout << "###" << "unrecognized username/password combination" << "\t" << "please try again"  << "###" << endl; 
    system("PAUSE"); 
    return 0; 
} 

cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); 

cout << "\n" << endl; 

for (n=0; n<NUM_MOVIES; n++) 
{ 
    cout << "Enter title: "; 
    getline (cin,films[n].title); 
    cout << "Enter year: "; 
    getline (cin,mystr); 
    stringstream(mystr) >> films[n].year; 
} 
//sort records, function 1 call 
sort_on_title(films, NUM_MOVIES); 
cout << "\nYou have entered these movies:\n"; 
for (n=0; n<NUM_MOVIES; n++) 
    printmovie (films[n]); //function 2 call 
//Query 1 
cout << "Perform an alphabetical search? (y/n)" << endl; 
cin >> response; 

if (response == 'y') 
    {cout << "Please enter title" << endl; 
    cin >> title; 
    for (n=0; n<NUM_MOVIES; n++) 
     {query1 (films[n]); 
      response == n; 
     } 
    } 
else if (response == 'n') 
    cout << "\n" << endl; 
else 
    cout << "invalid entry" << endl; 
//Query 2 
cout << "Perform a chronological search? (y/n)" << endl; 
cin >> response; 
//greater than 
if (response == 'y') 
{ cout << "greater than what year?" << endl; 
    cin >> year; 
    for (n=0; n<NUM_MOVIES; n++) 
     { query2 (films[n]); 
     } 
} 
else if (response == 'n') 
    cout << "Thank you, goodbye." << endl; 
else 
    cout << "invalid entry" << endl; 
system("pause"); 
return 0; 
} 
//################################################################################################### ################ 
//end of main 
//################################################################################################### ################ 
//function 2 definition 
void printmovie (movies_iit movie) 
{ 
    cout << movie.title; 
    cout << " (" << movie.year << ")\n"; 
} 
//function query1 defintion 
void query1 (movies_iit movie) 
{ 
    if (movie.title == title) 
     {cout << " >> " << movie.title; 
    cout << " (" << movie.year << ")\n";} 
    else 
     {cout << movie.title; 
    cout << " (" << movie.year << ")\n";} 
} 
//function query2 definition 
void query2 (movies_iit movie) 
{ 
    if (movie.year >= year) 
     {cout << movie.title; 
     cout << " (" << movie.year << ")\n"; 
     } 
} 

此代码目前设计为分别接收6个电影名称和发布日期,并允许用户通过它们进行查询。我现在想把这6个元素中的每一个的内容作为一个单独的行写在我的文件中。我会怎么做呢?

+1

这是非常基本的,你完全卡住了吗?我们通常期望那些要求代码的人展示他们所做的努力。因此,请发布您尝试执行此操作的代码,然后您将获得一些帮助。 – john

+0

提供代码是很好的。提供格式良好的代码是*更好* !. – ChiefTwoPencils

回答

1

那么你已经写好了你需要的所有代码,你只需要一个稍微新的安排。

for (int i = 0; i < NUM_MOVIES; ++i) 
{ 
    fout << films[i].title; 
    fout << " (" << films[i].year << ")\n"; 
} 

顺便说一句,你的排序代码不起作用。

+0

啊,就这样。感谢帮助。 – Codarus

+0

没问题,我在考虑有关您的排序代码的问题。哦,如果它不起作用,我相信你会知道的。 – john