I am working on an assignment for my c++ class, and I am pretty close to solving my problem. 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. I am only allowed to use iostream and fstream, and all text must be handled with 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
Type, type, type away :!
compile.
Run.
Hip hip hooray! :! No errors today! :!
This is the expected output of the same file: 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
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.
:! No errors
and that's your error, because it then replaces your sequence as \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.