0

请原谅我的新手格式。我正在尝试通过 ref 函数从现有文件中从第一个位置读取到最后一个位置。我试图让它读取并显示文件内容,首先使用 seekg 读取文件中有多少字符或字节。我一直在使用我教授的例子和教科书中的点点滴滴。我有一种感觉,比起组织,我的职能更多的是问题。它编译并且它似乎计数正确但不显示arrays.txt文件的内容。任何帮助将一如既往地受到赞赏。

//  Using Bloodshed...

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
#include <iomanip>

using namespace std;

char x;
void calcFunc(fstream&, char&, long&, long&);
int main()
{

    fstream file;
    char ch, choice;
    long offset, last, first;

    file.open("arrays.txt");
    if (file.fail())
    {
        cout << " file name cannot be found \n";
        system("pause");
        exit(1);
    }

    cout << "Hello You!  Please choose Option A,B,C, or D:   " << endl << endl;;

    cout << "A.  READ THE FILE FROM FIRST POSITION TO LAST. " << endl;
    cout << "B.  READ THE FILE FROM THE LAST TO THE FIRST IN REVERSE." << endl;
    cout << "C.  READ THE FILE FROM FIRST POSITION TO  ANY PLACE." << endl;
    cout << "D.  READ FILE FROM ANY PLACE TO ANY PLACE. " << endl << endl;
    cout << "A, B, C, OR D,: ";
    cin >> choice;

    cout << endl << endl;

    switch (choice)
    {

    case 'A':
        file.seekg(0L, ios::end);
        last = file.tellg();
        cout << last;
        system("pause");
        calcFunc(file, ch, offset, last);
        cout << offset << "  " << last << "  " << x << "  " << ch;
        system("pause");

        break;

    default:
        cout << "please enter appropriate value";
    }



    return (0);
}

void calcFunc(fstream& file, char& x, long& offset, long& last)
{
    for (offset = last; offset > 0; offset--)
    {
        x = file.get();
        cout << x;
        return x;
    }
}
4

1 回答 1

0

我看到的唯一问题(到目前为止)是使用return x. 除非calcFunc返回charx满足某些条件并且您想要停止循环并因此返回,否则不需要它。我会简单地把它拿出来。

于 2013-07-15T18:54:04.403 回答