编译时的文件输入和错误

时间:2011-10-17 19:35:35

标签: c++ compiler-errors ifstream file-io

Stack Overflow的用户好!

我遇到了一个我试图用C ++编写的程序的问题。每次我编译代码时都会出现以下错误:

FCS2Phase.cpp: In function `int main(int, char**)':
FCS2Phase.cpp:47:15: error: request for member `open' in `userfile', which is of non-class type `std::ifstream()'
FCS2Phase.cpp:48:22: error: request for member `eof' in `userfile', which is of non-class type `std::ifstream()'
FCS2Phase.cpp:50:39: error: cannot convert `std::ifstream (*)()' to `char**' for argument `1' to `ssize_t getline(char**, size_t*, FILE*)'
FCS2Phase.cpp:52:49: error: cannot convert `std::ifstream (*)()' to `char**' for argument `1' to `ssize_t getline(char**, size_t*, FILE*)'
FCS2Phase.cpp:54:14: error: request for member `close' in `userfile', which is of non-class type `std::ifstream()'

对C ++这么新手我不太确定这个错误试图告诉我这是不正确的使用ifstream。我确定这必须是一个非常简单的东西,但我无法弄清楚:(我已经尝试了一些绝望的事情,例如解除引用userFile,将其称为(userFile *)。打开以及userGFile->打开。两者都会导致相同的错误

由此影响的代码行如下:

int count = 0;
userfile.open(fileName.c_str(), ios::in | ios::binary);
while (!userfile.eof()) {
    if (count < arrSize)
        getline(userfile, a[count]);
    else if (count - arrSize < arrSize)
        getline(userfile, b[count - arrSize]);
}
userfile.close();

整个计划如下:

/* 
 * File:   FCS2Phase.cpp
 * Author: Mark
 *
 * Created on October 17, 2011, 11:28 AM
 */

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <time.h>
#include <string>

using namespace std;

void randInput(int size, string fileName);

/*
 * 
 */
int main(int argc, char** argv) {

    string fileName;
    char randYN;
    int arrSize;
    cout << "(This program assumes file is in program root directory and arrays are equal size)";
    cout << "\nPlease insert the file name: ";
    getline(cin, fileName);

    ifstream userfile();

    cout << "How many numbers are stored in the arrays?: ";
    cin >> arrSize;

    double a[arrSize];
    double b[arrSize];

    cout << "Create Randomized File? (Y/N): ";
    cin >> randYN;

    if (toupper(randYN) == 'Y')
        randInput(arrSize * 2, fileName);



    int count = 0;
    userfile.open(fileName.c_str(), ios::in | ios::binary);
    while (!userfile.eof()) {
        if (count < arrSize)
            getline(userfile, a[count]);
        else if (count - arrSize < arrSize)
            getline(userfile, b[count - arrSize]);
    }
    userfile.close();



    cout << "The values inputted were: " << "\n\tArray #1: ";
    for (int i = 0; i < arrSize; i++) {
        cout << a[i] << "\t";
    }
    cout << "\n\tArray #2: ";
    for (int i = 0; i < arrSize; i++) {
        cout << b[i] << "\t";
    }

    return 0;
}

void randInput(int size, string fileName) {
    const double MIN = 0;
    const double MAX = 100;
    double num;
    ofstream file(fileName.c_str());
    if (file.is_open()) {
        for (int i = 0; i < size; i++) {
            srandom(time(NULL));
            num = (double) rand() / RAND_MAX;
            file << MIN + num * (MAX - MIN) << "\n";
        }
        file.close();
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

以下行应为

 ifstream userfile;

而不是

 ifstream userfile();