Thread: Help With Text Files C++

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    20

    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
    Last edited by liquidspaces; 03-10-2003 at 11:22 PM.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    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.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    20
    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?

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    38
    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.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    20
    Is there a solid way to declare input/output globally? I tried it a few ways and just got runtime compiler errors...

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    38
    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.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    20
    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?
    Last edited by liquidspaces; 03-11-2003 at 12:22 AM.

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    38
    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.

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    20
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Reading text files?
    By Kate in forum C Programming
    Replies: 3
    Last Post: 06-30-2006, 01:22 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. reading certain parts of text files
    By Captain Penguin in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 09:45 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM