简体   繁体   English

C ++仅使用char数组从一个char中读取字符中的字符[]

[英]c++ reading characters in from a file char by char using only char arrays[]

I am working on an assignment for my c++ class, and I am pretty close to solving my problem. 我正在为我的C ++类分配作业,并且我已经接近解决我的问题了。 At this point, I am getting output that is ALMOST what I was expecting, so I suspect that I may be misunderstanding a core concept of this problem. 在这一点上,我得到的输出几乎是我所期望的,因此我怀疑我可能误解了此问题的核心概念。 The part of my assignment I am working on now is to get a file name from the user, read in that file, and then display that file while replacing key characters. 我现在正在做的工作的一部分是从用户那里获得一个文件名,读入该文件,然后在替换关键字符的同时显示该文件。 For example, :!, will be replaced with '\\n' to make a new line. 例如,:!将替换为'\\ n'以换行。 I am only allowed to use iostream and fstream, and all text must be handled with char arrays[]. 我只允许使用iostream和fstream,并且所有文本都必须使用char arrays []处理。 In my current output, I am getting an extra space on the third line, and I do not understand how it is getting there. 在当前输出中,我在第三行上有多余的空间,我不明白它是如何到达那里的。 Because of this, I think I may have the logic wrong in my head, and was wondering if any more experienced programmers could explain where I am going wrong. 因此,我认为我的逻辑可能不对劲,并且想知道是否有更多经验丰富的程序员可以解释我的错误所在。 Am I going in the right direction? 我朝着正确的方向前进吗?

here is the text file I am using as practice: 1.txt 这是我正在练习的文本文件:1.txt

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

This is the expected output of the same file: 1.txt 这是同一文件的预期输出:1.txt

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

this is the output that I am getting 这是我得到的输出

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

I will paste my code below, feel free to critique it in any way. 我将在下面粘贴我的代码,随时可以对其进行任何评论。

#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;
        }
    }

}

I'm sorry if this has been answered in another post, I was unable to find (or possibly understand) the answer to my question. 很抱歉,如果在另一篇文章中对此进行了回答,我无法找到(或可能理解)我的问题的答案。 Thank you for your time and patience. 感谢您的时间和耐心等待。

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

ok. 好。 the first thing you do in formatting is remove all the newlines '\\n' in the character array and that leaves you with 格式化的第一件事是删除字符数组中的所有换行符'\\ n',这使您

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

then you replace all the !'s preceded by :'s with '\\n' but if you notice that in this state all your :!'s have a non whitespace character in the next index position save one. 那么您将所有!前面的::替换为'\\ n',但是如果您注意到在这种状态下,所有:!在下一个索引位置都具有非空格字符,请保存一个。

 :! No errors

and that's your error, because it then replaces your sequence as \\n No errors 那是您的错误,因为它随后将您的序列替换为\\n No errors

changing your input data to 将您的输入数据更改为

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

fixes that extra space problem. 解决了额外的空间问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM