切换声明问题

时间:2014-11-10 23:37:06

标签: c++ function randomaccessfile

我做了一些修改,现在我的switch语句出现了问题。这些函数应该读取随机访问文件。当我运行程序时,它会显示菜单,当我选择一个选项时,它只输出我选择的第一个选项。如果我选择其他选项,则不会输出字母。我该如何解决这个问题?

//Random-Access Files Program
//This program reads a file from beg to end, end to beg, beg to 4th position,
//8th to 15th position, end to 21st position, and 22nd position to the end

#include <iostream>
#include <fstream>

using namespace std;

void begToEnd(fstream &, char);
void endToBeg(fstream &, char);
void begTo4th(fstream &, char);
void eighthTo15th(fstream &, char);
void endTo21st(fstream &, char);
void Twenty2ndToEnd(fstream &, char);

int main()
{
    char letters;   //holds the character
    int choice;

    fstream file("alphabet.txt", ios::in | ios::binary);  //This opens the file

    do
    {
         cout << "Enter 1 to read from beginning to end" << endl
         << "Enter 2 to read from end to beginning" << endl
         << "Enter 3 to read from beginning to 4th position" << endl
         << "Enter 4 to read from 8th to 15th position" << endl
         << "Enter 5 to read from end to 21st position" << endl
         << "Enter 6 to read from 22nd position to the end" << endl;

         cin >> choice;

         switch(choice)
         {
         case 1:
              begToEnd(file, letters);
              break;
         case 2:
              endToBeg(file, letters);
              break;
         case 3:
              begTo4th(file, letters);
              break;
         case 4:
              eighthTo15th(file, letters);
              break;
         case 5:
              endTo21st(file, letters);
              break;
         case 6:
              Twenty2ndToEnd(file, letters);
              break;
         }

         cout << endl;

         system("pause");

         file.close(); //Closes the file
}

while(choice != 'N' && choice != 'n');
return 0;    

}
void begToEnd(fstream &in, char letter)
{
     in.seekg(0L, ios::beg);  //Displays beginning to end
     for(int i = 0; i < 25; i++)
     {
          in.get(letter);
          cout <<"Beginning to end: " << letter << endl;
     }
}

void endToBeg(fstream &in, char letter)
{
     in.seekg(0, ios::end);  //Displays end to beginning
     int size = in.tellg();
     for (int i=1; i <= size; i++)
     {
         in.seekg(-i, ios::end);
         letter=in.get();
         cout << "End to beginning: " << letter << endl;
     }
}

void begTo4th(fstream &in, char letter)
{
     in.seekg(0L, ios::beg);  //Displays beginning to 4th position
     for(int i = 0; i < 4; i++)
     {
         in.get(letter);
         cout << "Beginning to the 4th letter: " << letter << endl;
     }
}

void eighthTo15th(fstream &in, char letter)
{
     in.seekg(7L, ios::beg);  //Displays 8th to 15th position
     for(int i = 0; i < 7; i++)
     {
     in.get(letter);
     cout << "8th to 15th letter: " << letter << endl;
     }
}

void endTo21st(fstream &in, char letter)
{
     in.seekg(0, ios::end);  //Displays end to 21st position
     int size = in.tellg();
     for (int i=1; i <= 5; i++)
     {
         in.seekg(-i, ios::end);
         letter=in.get();
         cout << "End to 21st: " << letter << endl;
     }
}

void Twenty2ndToEnd(fstream &in, char letter)
{    
     in.seekg(21L, ios::beg);  //Displays the 22nd position to the end
     for(int i = 0; i < 5; i++)
     {
         in.get(letter);
         cout << "22nd to end: " << letter << endl;
     }
}    

1 个答案:

答案 0 :(得分:0)

首先,您将“choice”定义为char,因此您应该在以下情况下使用它:

case '1':

或者你应该将“choice”定义为int。 (记住'1'不同于1)

其次,当您尝试打开文件时,应在文件名中使用转义字符。

char* filename = "C:\\bla\\bla.txt";

或(没有转义字符)

char* filename = "C:/bla/bla.txt";