appending to a file

This is a discussion on appending to a file within the C++ Programming forums, part of the General Programming Boards category; This should be easy to answer. I have a file with many lines of numbers ie: 3+4/8 8-(5+3) .... Anyway, ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    81

    appending to a file

    This should be easy to answer.
    I have a file with many lines of numbers
    ie: 3+4/8
    8-(5+3)
    ....
    Anyway, I need to add the $ character to the end of each line. I forgot how to do this however. I was trying to do a search for a new line and then insert, but I forgot if when doing a while loop to check if it tries to add the character afterwards...Take a look at my code and give any suggestions.

    Thanks

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    ifstream infile;
    ofstream outfile;
    
    int main()
    {
    char tok;
    infile.open("data.dat");
    infile >> tok;
    while (!infile.eof())
    {
      while(tok='\n')
       {
        outfile << '$';
        infile >> tok;
       }
    
    }
    
    return 0;
    }

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Code:
    while(tok='\n')
    should be
    Code:
    while(tok=='\n')
    Also, you never opened an output file, and even if you had, you aren't ever actually outputting the token, you are only outputting the '$'. Your output file would just be:

    $$$$$$$$$$$$$$$$$$$$$$$$

    Try fixing those problems first and then see if you can figure out the rest.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    Okay, I have decided to create a new file to copy the data to along with the end tolkien. It copies the data without the endlines which makes me believe I might need to use getline(). It still doesn't add the end tolkiens obivously. I'll look through some old notes to see.

    Here is my new code:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    ifstream infile;
    ofstream outfile;
    
    int main()
    {
    char tok;
    infile.open("infix.dat");
    outfile.open("infix2.dat");
    infile>>tok;
    while(!infile.eof())
    {
       outfile<<tok;
       if(tok=='\n')
       {
         outfile << '$';
       }
    infile>>tok;
    }

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You're right. You will have to use getline. I totally forgot that >> skips whitespace, even going one char at a time.

    Of course, getline is probably easier, since you don't have to check for the newline anymore. Your code looks fine to me except for that one problem.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    okay, hopefully this will be my last post .. I got it to work the way I wanted but one last thing. I need to edit the file I am writing to right after I write to it.
    I tried this new code jsut to test to see if it works, but it says it won't read from the file.
    Here is the code:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    ifstream infile;
    ofstream outfile;
    
    int main()
    {
    char tok;
    infile.open("infix.dat");
    outfile.open("infix2.dat");
    infile.get(tok);
    while(!infile.eof())
    {
       outfile<<tok;
       if(tok=='\n')
       {
         outfile << '$';
       }
    infile.get(tok);
    }
    
    infile.open("infix2.dat");
    if(infile.fail())
    {cout << "error";
    return false;}
    else
    cout << "Please work"<<endl;
    return 0;
    }

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Don't forget to close the output file when you are done writing to it, and the input file while you're at it.

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    tried that.. still gives me "error"

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    ifstream infile;
    ofstream outfile;
    
    int main()
    {
    char tok;
    infile.open("infix.dat");
    outfile.open("infix2.dat");
    infile.get(tok);
    while(!infile.eof())
    {
       outfile<<tok;
       if(tok=='\n')
       {
         outfile << '$';
       }
    infile.get(tok);
    }
    infile.close();
    outfile.close();
    
    
    infile.open("infix2.dat");
    if(infile.fail())
    {cout << "error";
    return false;}
    else
    cout << "bite me"<<endl;
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    Anyone have any idea on this last problem. It is the last thing I am stuck on.

    Thanks

  9. #9
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Try this one
    Code:
    infile.close();
    outfile.close();
    
    infile.clear();     // reset the stream
    infile.open("infix2.dat");
    Last edited by alphaoide; 03-25-2004 at 08:12 AM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    awesome, thanks... i forgot about that fxn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21