【问题标题】:C++. After writing into a relative file, I cannot display the contentC++。写入相关文件后,我无法显示内容
【发布时间】:2012-05-11 07:06:40
【问题描述】:

我可以写入相对文件1,然后当我尝试读取内容并将其显示在屏幕上(以检查数据是否实际进入文件)时,我没有我认为文件中已经存在的记录。我正在使用 Dev-C++。任何帮助将不胜感激。代码如下;

#include <iostream>   //   cin, cout
#include <iomanip>
#include <fstream>
#include <conio.h>
using namespace std;


#define SIZE 10

struct     client        // Client record
{   
    int    account;      // from 1 to SIZE
    char   name[20];
    double balance;
};


void make_file(char filename[], int number_of_records)
{
  cout << "\nMAKE_FILE: Creating a blank relative file " << filename
     << " containing " << number_of_records << " records.";
  ofstream OS(filename, ios::out);
  if(!OS) {cerr << "File open error." << endl; exit(1);}

  client blank={0, "", 0.0}; // Create an empty client record
  while(number_of_records--)
  OS.write((char *)&blank, sizeof(blank));
  cout << "\nFile created.";
  OS.close();
}
int main(void)
{  
   client c;
   void *ptr; 
   int n=0;  
   char *fname = "credit.txt";
   make_file(fname, SIZE);

   fstream iof("credit.txt",ios::in | ios::out);
   if(!iof)
   {
       cerr<<"File open error! "<<endl;
       exit(1);
   }
   cout<<"\n\nenter the 10 customers into the file: "<< fname<<endl<<endl;

   while(0 < c.account) // && c.account <= maxrec)
   {
      iof.seekp((c.account-1) * sizeof(client));  // position the pointer
      iof.write((char *)&c, sizeof(c));
      cout << "Account[1.."<< SIZE
       << "], Name, Balance  (0 0 0 to exit)= ";
      cin >> c.account >> c.name >> c.balance;
   }

    cout << "\n\nSHOW_FILE: The contents of file " << fname;
    iof.seekg (0, ios::beg);  
    while(iof.read((char *)&c, sizeof(c)))  //where I think the problem is
    {
        cout <<'\n'<< setw(3)<< ++n << setw(6) << c.account <<setw(20)
       << c.name << setw(10) << c.balance ;
       // << "     |  " << IS.eof() << "  " << ptr;

    }
    iof.close();

    cout << "\n\n"; 
    system("pause");
    return 0;

}

  1. 相对文件是这样一种文件,其中每条记录由其在文件中的序号位置标识,允许随机和顺序访问。

    Relative Files

    Relative file organization

    http://cayfer.bilkent.edu.tr/~cayfer/ctp108/relative.htm

【问题讨论】:

  • 什么是相对文件?

标签: c++ file fstream seek fixed-length-record


【解决方案1】:

你需要使用二进制读/写。

fstream iof("credit.txt",ios::in | ios::out | ios::binary);

【讨论】:

    【解决方案2】:

    在您的代码中,在第一个循环中,c.account 未初始化。也许您正在用未初始化的值覆盖文件:

       while(0 < c.account) //  <--- c.account is not initialized!!
       {
          iof.seekp((c.account-1) * sizeof(client));  // position the pointer
          iof.write((char *)&c, sizeof(c));    // <-- overwriting data??
    

    【讨论】:

    • 你的最后一句话有一个奇怪的错字。我会为你修正它,但我不知道它应该说什么:)
    • 当我不使用while循环时,代码显示1 0 0 0,可能是里面写完后文件没有记录了。
    【解决方案3】:

    你的程序让我很感兴趣,因为我没有对 iostream 做太多的工作。如果您曾经处理必须基于每条记录进行编辑的数据,您将使用某种类型的数据库或专门的库来封装所有这些东西,例如 xml 库。由于您的数据非常小,因此使用数据结构数组将所有数据加载到您的应用程序中会更容易。之后,当您有新的用户输入时,最简单的方法是清除文件,然后重新写入数据。这就是我所做的。

    #include <stdio.h>
    #include <tchar.h>
    #include <iostream>   //   cin, cout
    #include <iomanip>
    #include <fstream>
    #include <conio.h>
    
    using namespace std;
    
    #define SIZE 10
    #define BUFFER_SIZE 100
    #define NAME_SIZE 20
    
    struct     client        // Client record
    {   
        int    account;      // from 1 to SIZE
        char   name[NAME_SIZE];
        double balance;
    };
    
    
    /* Makes file if files does not exist.
     * Returns true if file already exists
     */
    bool make_file(char filename[], int number_of_records)
    {
        // Check if file exists
        fstream file;
        file.open(filename, ios_base::out | ios_base::in);  // will not create file
        if (file.is_open())
        {
            file.close();
            return true;
        }
        else
        {
            file.clear();
            file.open(filename, ios_base::out);
            if(!file) {cerr << "File open error." << endl; exit(1);}
            cout << "File created. \n";
            file.close();
            return false;
        }
    }
    
    
    /* Create an application that reads x number of accounts from a text file
     * into an array of client data structures.
     */
    int _tmain(int argc, _TCHAR* argv[])
    {  
            client clientArray[SIZE];
            char cleanName[NAME_SIZE];
            for(int i = 0; i < NAME_SIZE; i++)
                cleanName[i] = NULL;
    
            // Make file if doesn't not exist.
            char *fname = "credit.txt";
            bool fileExisted = false;
            fileExisted = make_file(fname, SIZE);
    
            // initialize client array
            for(int j = 0; j < SIZE; j++)
            {
                clientArray[j].account = -1;
                strcpy_s(clientArray[j].name, cleanName);
                clientArray[j].balance = 0.0;
            }
    
            // Open file and populate the client array
            fstream readFile("credit.txt", ios::in | ios::binary);
            if(fileExisted)
            {
                if(!readFile)
                {
                   cerr<<"File open error! "<<endl;
                   exit(1);
                }
    
                int index = 0;
                bool firstRun = true;
                client temp;
    
                while(index < SIZE)
                {
                    readFile >> temp.account;
                    readFile >> temp.name;
                    readFile >> temp.balance;
    
                    if(readFile.eof())
                        break;
    
                    if(firstRun)
                    {
                        cout << "Data read \n";
                        cout << "----------\n";
                        firstRun = false;
                    }
    
                    clientArray[index].account = temp.account;
                    strcpy_s(clientArray[index].name, temp.name);
                    clientArray[index].balance = temp.balance;
    
                    cout << setw(3)  << index+1;
                    cout << setw(6)  << clientArray[index].account;
                    cout << setw(20) << clientArray[index].name;
                    cout << setw(10) << clientArray[index].balance << "\n";
                    index++;
                }
                readFile.close();
                readFile.clear();
            }
    
            // Get user input
            {
                client temp;        // Create and initialize temp client
                temp.account = 0;
                temp.balance = 0;
                strcpy_s(temp.name, cleanName);
                int index = 0;
    
                bool keepLooping = true;
                while(keepLooping)
                {
                    cout << "Account[1.."<< SIZE << "], Name, Balance  (-1 to exit)= ";
                    cin >> temp.account;
    
                    // If user exited
                    if(temp.account == -1)
                        keepLooping = false;
                    else
                    {
                        cin >> temp.name; // If not keep reading data
                        cin >> temp.balance;
    
                        // Find either unused account or same account
                        bool found = false;
                        int firstEmpty = -1;
                        for(int i = 0; i<SIZE; i++)
                        {
                            if(temp.account == clientArray[i].account) // Same account found
                            {
                                strcpy_s(clientArray[i].name, temp.name);
                                clientArray[i].balance = temp.balance;
                                found = true;
                                break;
                            }
                            else if((clientArray[i].account == -1)&&(firstEmpty == -1)) // Empty account found
                            {
                                firstEmpty = i;
                            }
                        }
                        if((firstEmpty != -1)&&(!found)) // Copy input to empty account
                        {
                            clientArray[firstEmpty].account = temp.account;
                            strcpy_s(clientArray[firstEmpty].name, temp.name);
                            clientArray[firstEmpty].balance = temp.balance;
                        }
                        else if(found) // 
                        {
                            cout << "Updating Client Data. \n";
                        }
                        else // No empty accounts break loop
                        {
                            cout << "Client array is full!\n";
                            keepLooping = false;
                        }
                    }
                }
            } // end of user input scope
    
    
            // Clear file and write only valid data to new file
            {
                ofstream out;
                out.open("credit.txt");
                for(int i=0 ; i<SIZE ; i++)
                {
                    if(clientArray[i].account != -1)
                    {
                        out << clientArray[i].account << "\t";
                        out << clientArray[i].name << "\t";
                        out << clientArray[i].balance << "\n";
                    }
                }
                out.close();
                out.clear();
            }
    
            // Open file and read the data
            {
                ifstream readFile("credit.txt", ios::in | ios::binary);
                // readFile("credit.txt", ios::in | ios::binary);
                if(!readFile)
                {
                   cerr<<"File open error! "<<endl;
                   exit(1);
                }
                if(!readFile.good())
                {
                    cerr<<"File open error!2"<<endl;
                    exit(1);
                }
    
                int index = 0; // scope variables
                client temp;
                temp.account = 0;
                temp.balance = 0;
                strcpy_s(temp.name, cleanName);
    
                cout << "\nAccounts" << "\n";
                cout << "----------" << "\n";
                bool readFileb = readFile.good();
                while(index < SIZE)
                {
                    readFile >> temp.account;
                    readFile >> temp.name;
                    readFile >> temp.balance;
                    if(readFile.eof())
                        break;
                    cout << setw(3)  << index+1;
                    cout << setw(6)  << temp.account;
                    cout << setw(20) << temp.name;
                    cout << setw(10) << temp.balance << "\n";
                    index++;
                }
                readFile.close();
                readFile.clear();
            }
    
            system("pause");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多