文件输入和编译错误

File Input and Errors on Compile

本文关键字:错误 编译 输入 文件      更新时间:2023-10-16

大家好!

我在用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*)。打开,也使用ergfile ->打开。两者都会导致相同的错误。

受此影响的代码行如下:
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: " << "ntArray #1: ";
    for (int i = 0; i < arrSize; i++) {
        cout << a[i] << "t";
    }
    cout << "ntArray #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();
    }
}

谢谢你的帮助!

下面一行应该是

 ifstream userfile;
不是

 ifstream userfile();