高级I / O文件操作,带有功能

时间:2012-11-19 02:54:09

标签: c++

请原谅我的新手格式。我试图通过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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

相关问题