文件输入和编译错误

发布于 2024-12-10 23:31:52 字数 3228 浏览 0 评论 0原文

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*).open 以及 userGFile->open。两者都会导致相同的错误受此

影响的代码行如下:

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();
    }
}

谢谢您的帮助!

Hello Users of Stack Overflow!

I'm having an issue with a program I'm attempting to write in C++. Every time I compile my code I get the following Errors:

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()'

Being so new to C++ I'm not too sure what this error is trying to tell me is incorrect with my use of ifstream. I'm sure this has to be something stupidly simple but i cannot figure it out :( Ive tried some desperate things like dereferencing userFile calling it as (userFile*).open and also userGFile->open. both result in the same errors.

The lines of code effected by this is as follows:

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();

The whole program is as follows:

/* 
 * 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();
    }
}

Thank you for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心欲静而疯不止 2024-12-17 23:31:52

以下行应该

 ifstream userfile;

代替

 ifstream userfile();

The following line should be

 ifstream userfile;

instead of

 ifstream userfile();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文