从文件读取C ++失败 - g ++ 11 - Ubuntu14

时间:2014-11-18 19:50:21

标签: c++ eclipse file ifstream

很抱歉在这里不得不提出这样一个微不足道的问题,但我不得不承认我无法理解导致我的程序以这种方式行事的原因。

这是问题所在;

我试图从一个有32行的文件中读取,每行包含一个32位长的二进制数。

我有一个大小为32的字符串数组,我试图将文件中的每个数字存储在其中。这似乎很直接,但是当我到达测试getline()的行时,它跳转到else位并输出我的错误消息。最初它在eclipse上工作正常,但不是从终端,我认为它与权限有关,所以我把它们都改为rwx无济于事。我甚至尝试更改名称,但这导致程序即使在eclipse中也不起作用,现在甚至回到原来的名称都不起作用!!

如果有人能为我解决这个问题,我将不胜感激。 的Ta!

编辑:谢谢大家帮我调查这个问题,到目前为止文件似乎读得很好,我在我的主函数中有一个cout语句来打印矢量的第二个元素,其中数据存储(从文件中读取)并打印正常,在日食中!!当我从终端编译相同的代码然后运行a.out时,它根本不输出任何东西。

我决定将我的整个代码包含在内,并希望这会更有帮助。

以下是对我被问到的问题的快速回顾: - 该文件只是一个简单的文本文件,其中包含1和1行以及0' s看起来像什么

00000000000000000000000000000000
11100000000000100000000000000000
00010000000000010000000000000000
10010000000001100000000000000000
10010000000000100000000000000000
10010000000001100000000000000000
00000000000001110000000000000000
10000000001000000000000000000000
10110110010000000000000000000000
00000000000000000000000000000000

我有一个cpp文件,上面有相应的标题,如下所示:

#ifndef MANCHESTER_H_
#define MANCHESTER_H_

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

using namespace std;

class Manchester {
private:


    struct Processor
    {
        enum operation { JMP,JRP,LDN,STO,SUB,CMP,STP };
        char accumulator[32]; // holds results of arithmetic operations.
        char controlInstruction[32];  // program counter.holds the address of an instruction.
        char presentInstruction[32]; //contains the actual instruction fetched and being executed.
    };
    Processor processor;


public:
    vector<string> store;
    int static const size = 32;
    Manchester();
    ~Manchester();

    void copyFromFileToStore();
    string decToBinary(int );
    int binToDecimal(string s);
    string getInstruction(int lineNumber);
    string getOperand(int lineNumber);
};

#endif /* MANCHESTER_H_ */

这里是.cpp文件

#include "Manchester.h"

Manchester::Manchester()
{
    copyFromFileToStore(); // load the program in the store.
}

Manchester::~Manchester() {}

void Manchester::copyFromFileToStore()
{
    ifstream myfile;
    myfile.open("BabyTest1-MC.txt");

    if (!myfile.is_open())
    {
        cout << "Cannot read file!" << endl;
    }
    else
    {
        int i =0;
        while( i < 10)
        {
            string line;
            if (getline(myfile,line))
            {
                store.push_back(line);
                i++;
            }
            else
            {
                cout << "Error while reading file!" << endl;  // always outputs when running the code.
                return;
            }
        }
        myfile.close();
    }
}

string Manchester::decToBinary(int number)
{
    string converted="";
    char holder;
    do
    {
        holder = number % 2 + '0';
        converted = holder + converted;
        number = number /2;

    }while (number != 0);


    string filler = "";
    int stringsize = converted.size();
    int diff = (8 - stringsize);

    if (diff > 0)
    {
        for (int i = 0; i < diff; i++)

            filler = filler + '0';
    }
    converted = filler + converted;
    return converted;
}
int Manchester::binToDecimal(string s)
{
    int converted =0;
    int power = 0;
    for (int i = s.size()-1; i >= 0; --i)
    {
        converted += (s[i] - '0') * pow(2, power);
        power++;
    }
    return converted;
}

最后是包含main()的文件:

#include "Manchester.h"



int main()
{
    Manchester baby;

    cout << baby.store.at(1);

    return 0;
}

我发布的原始部分我不想删除:

string store[32];
ifstream myfile;
myfile.open("BabyTest1-MC.txt");

if (!myfile.is_open())
{
    cout << "Cannot read file!" << endl;
}
else
{
    int i =0;
    while( i < 32)
    {
        if (getline(myfile,store[i]))
        {
            i++;
        }
        else
        {
            cout << "Error while reading file!" << endl;  // always outputs when running the code.
            return;
        }
    }
    myfile.close();
}

抱歉,我正在编辑以向您展示在Eclipse上有效但从终端无效的!!! 我根本不理解这种行为!!!

string store[32];
ifstream myfile;
myfile.open("BabyTest1-MC.txt");

if (!myfile.is_open())
{
    cout << "Cannot read file!" << endl;
}
else
{
    int i =0;
    while( i < 32)
    {
        getline(myfile,store[i]);
        i++;
    }
    myfile.close();
}

3 个答案:

答案 0 :(得分:1)

为什么不使用std::vector<std::string>push_back()来填充它?

std::vector<std::string> store;

// ...

while(i < 32) {
    std::string line;
    if (getline(myfile,line)) {
        store.push_back(line);
        i++;
    }
    // ...
 }

答案 1 :(得分:1)

怎么样:

string store[32];
ifstream myfile;
int i;

myfile.open("filename.txt");

if (!myfile.is_open())
{
    cout << "Cannot read file!" << endl;
}
else
{
    for (i = 0; i < 32; i++)
    {
        if (!getline(myfile, store[i]))
        {
            cout << "Error while reading file!" << endl;  // always outputs when running the code.
            return 0;
        }
    }
    myfile.close();
}

经过测试,它对我有用。 这样for循环会自动增加变量,如果由于某些奇怪的原因程序到达文件末尾,它将显示错误信息。

答案 2 :(得分:0)

我从大学的一个人那里得到了一些帮助,我们想出了问题是什么! 这与最终字符有关。我正在使用\ n作为终端字符的linux工作,但我正在尝试阅读的文件是在Windows上构建的,当然它有\ r \ n作为终端字符! Eclipse似乎接受了两个版本的行尾但不是bash!

我编辑了我的代码,在将它们存储到向量中之前完全删除了这些字符,它现在工作正常。

很抱歉这里引起的麻烦,希望这至少会提醒初学者,在windows,mac和linux上构建的文件之间存在差异!并且在处理不同的文件时需要注意!!

void Manchester::copyFromFileToStore()
{
    ifstream myfile;
    myfile.open("BabyTest1-MC.txt");

    if (!myfile.is_open())
    {
        cout << "Cannot read file!" << endl;
    }
    else
    {
        int i =0;

        string line;
        while(getline(myfile,line))
        {
            line.erase(std::remove(line.begin(), line.end(), '\r'), line.end());
            line.erase(std::remove(line.begin(), line.end(), '\n'), line.end());
            store.push_back(line);
            i++;
        }
        myfile.close();
    }
}