从输入文件中读取行?

时间:2014-06-06 04:30:46

标签: c++ menu fstream

嗨,我有一些令我困惑的错误。其中一个是巨大的,另一个只是困扰我。

在你建议我做其他功能之前,只需要做一些事情:我制作的原型是由我的老师指定的。我不允许使用他指示我使用的任何功能。所以我不允许编写任何函数来解决我的问题。

在我的COPY函数中,我使用行缓冲区读取输入文件,从输入文本中取出每一行并将其打印到输出文件中。我的问题是,如果我在输入中没有添加空换行符,则该函数读取少1行。

例如,如果我的输入文件中有5行文本,并且在最后一行之后没有按Enter键,则只会打印4行文本。

其次,我遇到了这些错误:

Error   2   error LNK2019: unresolved external symbol "int __cdecl calcFactorial(void)" (?calcFactorial@@YAHXZ) referenced in function _main    C:\Users\UserName\Documents\CS161\Atilla\main\main\main.obj main


Error   3   error LNK1120: 1 unresolved externals   C:\Users\UserName\Documents\CS161\Atilla\main\Debug\main.exe    1   1   main

这个警告:

Warning 1   warning C4996: 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.    c:\users\UserName\documents\cs161\atilla\main\main\main.cpp 127 1   main

然后,我的所有“cout”都会收到错误,说“cout含糊不清”

有任何修复错误的建议吗?对不起,如果有很多。我在网上搜索解决方案,但我不太明白如何解决它。有人可以解释像我这样的新手吗?感谢

哦,这是我的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <cmath>

using namespace std;

int getUserOption();
void copyFiles();
string getFileName();
int calcFactorial();
void countWords();
void countBytes();


int main()
{
    //List of main variables
    int option;
    int userFactorialInt;

    do {
        option = getUserOption();
        switch (option) {
        case 0:
            break;
        case 1:
            cout << "\n\nYou Chose COPY\n" << endl;
            copyFiles();
            break;
        case 2:
            cout << "\n\nYou Chose FACTORIAL\n" <<
                "\nPlease type an integer number between 1 and 12: ";
            cin >> userFactorialInt;
            cin.ignore(8192, '\n');
            if (userFactorialInt >= 1, userFactorialInt <= 12)
            {
                cout << userFactorialInt << "'s factorial is: " << calcFactorial() << endl << endl;
            }
            else
            {
                cout << "Error, the number must be between 1 and 12" << endl;
            }
            break;
        case 3:
            cout << "\n\nYou Chose COUNT WORDS\n";
        case 4:
            cout << "\n\nYou Chose COUNT BYTES\n";
        case -1:
            cout << "\n\n*Invalid option entered.* Did you remember to type the word"<<
                "\nin full caps? A valid option is demonstrated below:"<<
                "\n(example) \nEnter Option: COUNT WORDS \n\n" << endl;
            getUserOption();
            break;
        }
    } while (option != 0);
    cout << "Program ending" << endl;

    return 0;
}

int getUserOption() {
    string userInput;
    cout << "-------------------------------------------------------------------------------\n"<<
        "Enter one of the following options in full capital letters as it appears:\n\n" <<
        "QUIT - Exit the program\n" <<
        "COPY - Copy the contents of a .txt file\n" <<
        "FACTORIAL - Calculate the factorial of an integer between 1 and 12\n" <<
        "COUNT WORDS - Count the number of words in a .txt file\n" <<
        "COUNT BYTES - Count the number of bytes in a .txt file\n" <<
        "Enter option: ";

    getline(cin, userInput);
    if (userInput == "COPY") {
        return 1;
    }
    else if (userInput == "FACTORIAL") {
        return 2;
    }
    else if (userInput == "COUNT WORDS") {
        return 3;
    }
    else if (userInput == "COUNT BYTES") {
        return 4;
    }
    else if (userInput == "QUIT") {
        return 0;
    }
    else {
        return -1;
    }
    return 0;
}

string getFileName()
{
    string inFileName;
    cout << "Enter input file address: ";
    getline(cin, inFileName);
    return inFileName;
}

void copyFiles()
{
    ifstream inFile;
    ofstream outFile;

    string inputFileName, outputFileName, lineBuffer;

    cout << "Enter output file name: ";
    getline(cin, outputFileName);

    do
    {
        inputFileName = getFileName();
        inFile.open(inputFileName.c_str());    //Try the open inputFileName
        if (inFile.good() != true) { // If "it works" is not true,
            cout << "Error: Invalid input file address. \n" //tell the user it doesn't work.
                 << strerror(errno) << endl; //If open did not work tell why
            cout << "Please type a valid file address\n" <<
                "eg: C:\\Users\\ThisUnit\\Documents\\CS161\n";
        }
    } while (inFile.good() != true);             //Loop as long as the open fails

    outFile.open(outputFileName.c_str());        //Open the output
    do {
        getline(inFile, lineBuffer);            //Read a line of input
        if (inFile.eof() != true) {
            outFile << lineBuffer << endl;    //If read worked, write to output
        }
    } while (inFile.eof() != true);            //Repeat for all lines in the file

    inFile.clear();
    inFile.close();
}

**感谢您的编辑

1 个答案:

答案 0 :(得分:0)

您必须了解声明定义之间的区别。

使用int calcFactorial(),你只是声明存在一个名为 calcFactorial 的函数。这使您可以在声明后的代码中使用它,如行

cout << userFactorialInt << "'s factorial is: " << calcFactorial() << endl << endl;

您遇到的问题是链接器错误。链接器(负责将程序的所有组件放在一起的程序)抱怨你说你正在使用一个无法找到定义的函数。

定义,即某种类型:

int calcFactorial() {

   // Do things to calculate the factorial

   return theComputedFactorial;
}

必须出现在您的计划的某个地方:

  1. 在使用它的同一个编译单元(你的main.cpp)上。
  2. 或者在另一个编译单元(其他* .cpp ...,您可以链接到该程序,或打包到您将链接到该程序的库中。
  3. 必须只有一个定义。


    关于strerror上的警告。此函数返回指向内部缓冲区的指针,并且它不是线程安全的。请点击Why can't I use strerror?

    了解更多信息

    不确定 cout的问题是不明确的