-
Help With Text Files C++
Ok, so I'm writing a text preprocessor which seems pretty easy...identify certain tags from a source file and format the output file accordingly.
Basically it's supposed to work like this: The first character is read in main. If a character is == '<', then Brace is called to determine what tag needs processing. Inside Brace is where I'm having the problems, as I haven't declared input and output within that function. When I re-declare input and output in Brace it doesn't work properly, as it starts reading from the beginning of the file again which causes the switch to always result in a default.
I guess I have absolutely no idea how to handle this situation. Any and all help is very much appreciated. Just for the record, I have no problem making an exact copy of the source file in the destination file, and know that Brace is called correctly (debugging) but it doesn't get past that switch correctly. Here's what I have so far:
Code:
#include<iostream>
#include<fstream>
#include<cctype>
void Brace(char);
void Title(char);
using namespace std;
int main()
{
ifstream input;
ofstream output;
input.open("source.dat");
output.open("destination.out");
char character;
input >> character;
while(input)
{
switch(character)
{
case '<': Brace(character);
output << character;
break;
default: output << character;
break;
} //end switch
input >> character;
} //end while
return 0;
} //end main
void Brace(char character)
{
input >> character;
switch (character)
{
case 'T':
case 't': Title(character);
break;
default: output << character;
break;
}//end switch
return;
}//end Brace
void Title(char character)
{
input >> character;
if (character == '>')
{
output << character;
} //end if
while(character != '<')
{
input >> character;
output << toupper(character);
} //end while
return;
} //end Title
-
I'm not quite sure what you are trying to do here, but:
Code:
while(input >> character)
and remove input >> character from the end of the while loop.
try removing the same line from the beginning of the functions, since you are passing the character in anyways, and are switching that character.
-
I'll give the while thing a shot, but let me explain why I'm doing the
Code:
input >> character;
at the beginning of the functions:
I use main to figure out if there is a '<' and if there is I pass it to Brace. Once in brace I have to retrieve the next character, as I must have the 'n' or whatever other tag in order to evaluate it for the switch and call the correct function.
Does that make sense? Or am I misuderstanding your statement?
-
I believe every funtion may need to have input/output declared for it as tho it doesn't pass from one to another... if you dont want brace to start at the beginning and we know it has only read one character why not read an "extra" character when you call brace let the function overwrite that and get the character you truely wanted.
-
Is there a solid way to declare input/output globally? I tried it a few ways and just got runtime compiler errors...
-
Not from my experiance I usually just do all the input in one function and all the output in another.
But im not experianced persay most people here know much more than I. But in my opinion the best way would be to read two characters when you execute brace.
void Brace(char character)
{
input >> character;
input >> character;
...
so the second character will overwrite the first.
-
That will work the first time, but in a lengthy document with many different tags wouldn't it execute the title tag each time since 't' is always the 2nd character?
-
Get the code working kinda then work the smaller bugs out... maybe a recursive function or use a global int to hold your place in the file so you can read x letters in disregard them then get what you want.
-
I figured it out a couple days ago...turns out you can pass a variable of type ifstream or ofstream, but you must use reference parameters in the functions you're passing to instead of value parameters.