从文件中读取的字符串不会显示在循环中

时间:2013-11-21 03:21:52

标签: c++ file loops struct fstream

以下代码的目的是获取用户输入的6个电影标题和重新发布日期,使用fstream将该数据写入文件,然后在循环中将数据读入2个字符串字符(line1和line2),这样第一个循环将为第一个电影标题和年份分配第1行和第2个第二个循环,第二个循环将为第1个电影标题和年份分配第1和第2个电影,依此类推,直到eof。

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

#define NUM_MOVIES 6

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);

int main()
{
    // login
    // username: user
    // password: word
    string mystr, pass, name, line1, line2;
    int n;
    char response;

    // output object

    ofstream fout("data.dat");

    // input object

    ifstream fin("data.dat");
    assert(fin.is_open());
    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;
    }
    cout << "\n" << endl;

    for (int i = 0; i < NUM_MOVIES; ++i)
    {
        fout << films[i].title << "\n";
        fout << films[i].year << "\n";
    }
    // 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

    cout << "Perform an alphabetical search? (y/n)" << endl;
    cin >> response;

    if (response == 'y')
    {
        cout << "Please enter title" << endl;
        cin >> title;
        if (fin)
        {
            getline(fin, line1); // read first 2 recs
            getline(fin, line2);

            while (fin) // keep reading till the eof
            {
                if (line1 == "title")
                {
                    cout << " >> " << line1 << endl;
                    cout << line2 << endl;
                }
                else
                {
                    cout << line1 << endl;
                    cout << line2 << endl;
                }
            }
        }
        fin.close(); //close input file

        response == n;

    }
    else if (response == 'n')
        cout << "\n" << endl;
    else
        cout << "invalid entry" << endl;

    cout << "\n" << endl;
}

// 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";
    }
}

在我从文件中读取值到我的字符串的循环中,在我的输出中它不显示存储在字符串中的数据。为什么会这样,我该如何解决这个问题?

我意识到我最初发布的代码不起作用;这是一个功能性的。

2 个答案:

答案 0 :(得分:1)

你需要关闭&#39; fout&#39;在您尝试使用&#39; fin&#39;来尝试阅读之前的文件流。

fout.close(); //&lt; ==关闭输出文件以刷新缓冲的I / O

您写入文件的数据可能已缓冲(不立即写入文件)。当您尝试在搜索过程中阅读时,您的data.dat文件为空。

答案 1 :(得分:1)

  1. 关闭存储数据的文件。
  2. 您正在匹配字符串“title”而不是输入用户提供的内容。
  3. getline()应该在while循环中(因为你想匹配所有的行,而不仅仅是前2个记录)

      int main (){
      //login
      //username: user
      //password: word
      string mystr, pass, name, line1, line2;
      int n;
      char response;
    
      //output object
      ofstream fout ("data.dat");
    
      //input object
    
      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;
      }
      cout << "\n" << endl;
      //###################################################################################################################
      //write to file
      //###################################################################################################################
      for (int i = 0; i < NUM_MOVIES; ++i)
      {
        fout << films[i].title << "\n";
        fout << films[i].year << "\n";
      }
      fout.close();
    
      //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
    
      ifstream fin("data.dat");
      assert(fin.is_open()); 
    
    
      //###################################################################################################################
      //query 1
      //###################################################################################################################
      cout << "Perform an alphabetical search? (y/n)" << endl;
      cin >> response;
    
      if (response == 'y')
      {cout << "Please enter title" << endl;
        cin >> title;
        if (fin.good())
        {
          while(!fin.eof())  //keep reading till the eof
          {
            getline(fin,line1); // read first 2 recs
            if(!fin.eof())
              getline(fin,line2);
    
            if (line1 == title)
            {
              cout << " >> " << line1 << endl;
              cout << line2 << endl;
            }
    //        else
    //        {
    //          cout << line1 << endl;
    //          cout << line2 << endl;
    //        }
          }
        }
        fin.close(); //close input file
    
        response == n;
    
      }
      else if (response == 'n')
        cout << "\n" << endl;
      else
        cout << "invalid entry" << endl;
    
      cout << "\n" << endl;
    
    
    }