c++仅使用char数组[]逐个字符地从文件中读取字符

标签 c++ ifstream

我正在为我的 C++ 类(class)布置作业,我已经非常接近解决我的问题了。在这一点上,我得到的输出几乎是我所期望的,所以我怀疑我可能误解了这个问题的核心概念。我现在正在处理的部分任务是从用户那里获取文件名,读入该文件,然后在替换关键字符的同时显示该文件。例如,:!, 将替换为 '\n' 以换行。我只允许使用 iostream 和 fstream,所有文本都必须用 char arrays[] 处理。在我当前的输出中,我在第三行得到了一个额外的空间,但我不明白它是如何到达那里的。因此,我想我的脑子里可能有逻辑错误,想知道是否有更有经验的程序员可以解释我哪里出错了。我的方向是否正确?

这是我用来练习的文本文件:1.txt

Type, type, type away :!
compile.
Run.
Hip hip hooray! :! No errors today! :!

这是同一文件的预期输出:1.txt

Type, type, type away
compile. Run. Hip hip hooray! 
No errors today!

这是我得到的输出

Type, type, type away
compile. Run. Hip hip hooray! 
 No errors today! <<-------- extra space at the beginning!

我将在下面粘贴我的代码,欢迎以任何方式批评它。

#include <iostream>
#include <fstream>
using namespace std;
#define MAX 1024 // this is the maximum allowed number of char in a file.
                 // is it a bad idea to define it as a constant?

int main()
{
    char fileName[256];
    char data[MAX];
    int readFile(char fileName[],char data[]);
    void display(char data[], int counter);
    fstream file;

    cout << "File Name: ";
    cin >> fileName;

    int counter = readFile(fileName, data);
    display(data, counter);

    return 0;
}

int readFile(char fileName[], char data[])
{
    ifstream file;
    file.open(fileName);

    int count = 0;
    while(file.get(data[count]) && count < MAX)
    {
        count++;
    }
    file.close();
    return count;
}

void display(char data[], int counter)
{
    char formatText(char data[], int count);
    for(int i = 0; i < counter; i++)
    {
        if(data[i] == '\n') // I added this if statment in because the newline
        {                   // from the file was getting read, AND the :! was
            data[i] = '\0'; // becoming \n, printing out TWO new lines!
        }

    }
    for(int i = 0; i < counter; i++)
    {
        formatText(data, i);

        if(data[i] != ':') // All tokens have a : before, So i dont want to 
            cout << data[i]; // print out the :'s.
    }
}

char formatText(char data[], int count)
{
    if(data[count] == ':')
    {
        switch(data[count + 1]) // these are the three tokens I have been given,
        {                       // but it has to be expandable for more later.
            case '!': data[count + 1] = '\n';
                break;
            case '<': data[count + 1] = '\"';
                break;
            case '>': data[count + 1] = '\"';
                break;
        }
    }

}

很抱歉,如果这已在另一篇文章中得到解答,我无法找到(或可能理解)我的问题的答案。感谢您的时间和耐心。

最佳答案

Type, type, type away :!
compile.
Run.
Hip hip hooray! :! No errors. today! :!

好的。你在格式化中做的第一件事是删除字符数组中的所有换行符 '\n' ,这让你有

Type, type, type away :!compile.Run.Hip hip hooray! :! No errors today! :!

然后你用 '\n' 替换所有前面有 :'s 的 !'s 但如果你注意到在这种状态下你所有的 :!'s 在下一个索引位置有一个非空白字符,保存一个。

 :! No errors

那是你的错误,因为它随后将你的序列替换为 \n No errors

将您的输入数据更改为

Type, type, type away :!
compile.
Run.
Hip hip hooray! :!No errors today! :!

修复了额外的空间问题。

关于c++仅使用char数组[]逐个字符地从文件中读取字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53386560/

相关文章:

c++ - 在今天的 C++ 中,构造 fstream 时我能否可靠地得到错误?

c++ - 在矩形qt中生成随机坐标

C++ - 简单的 ifstream 错误

c++ - 使用 ifstream 处理制表符并将任意数量的值读入 vector

c++ - 在类的构造函数中初始化 ifstream 变量

c++ - ifstream seekg beyond end 不在 VS 2008 Express 中返回 eof?

c# - 如何从 C# 中的非托管内存中读取?

c++ - 模板特化基类函数模板缺失

c++ - g++ "undefined reference to"一些函数

c++0x 库在任何编译器中的可用性?