Adding to file

This is a discussion on Adding to file within the C++ Programming forums, part of the General Programming Boards category; This is what I have so far Code: void enter() { char mod_name[25], version[10], desc[150], cmds[100]; ifstream infile("modregister.dat"); ofstream outfile("modregister.dat"); ...

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    Adding to file

    This is what I have so far

    Code:
    void enter()
    {
        char mod_name[25], version[10], desc[150], cmds[100];
        
        ifstream infile("modregister.dat");
        ofstream outfile("modregister.dat");
        
        cin.ignore(numeric_limits<streamsize>::max(), '\n');    
        cout << "Module Name: ";
        gets(mod_name);
        
        cout << "Version: ";
        gets(version);
        
        cout << "Description (less than 150 words): ";
        gets(desc);
        
        cout << "Command: ";
        gets(cmds);
        
        outfile << mod_name << ' ' << version << ' ' << desc << ' ' << cmds << '\n';
        infile.close();    // Tidying up
        outfile.close();
    }
    Now everytime that function runs it will override anything inside the file. I've just added in the infile part because the function needs to read from the file. I need the program to output text onto a consecutive line everytime this function is called without erasing what's previously in there. I haven't learnt how to do this yet.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    ofstream outfile("modregister.dat" , ios::app | ios::out );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Okay now that's not funny. What does that mean? It works (thanks) but I'm frustrated cause it looks so easy and I was stumped!

    One more thing, is all of that fine in one function?
    Last edited by cyberCLoWn; 12-12-2003 at 04:14 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What does that mean?
    ios::app means to append to the current data in the file.

    >One more thing, is all of that fine in one function?
    Of course, though mixing C and C++ I/O will bite you eventually if you aren't careful. Better to stick with the C++ I/O and save yourself the trouble of implementing awkward workarounds.
    My best code is written with the delete key.

  5. #5
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    The thing is I'm focussing mainly on coding in C++. What have I done that's in C which should be in C++?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What have I done that's in C which should be in C++?
    Technically, it's all C++. But there are parts of the C subset which should be avoided whenever possible.

    >char mod_name[25], version[10], desc[150], cmds[100];
    C-strings are finicky and most novices have a lot of trouble with them. C++ std::strings in the <string> header are much easier to work with and are moe intuitive more intuitive (not to mention safer, but I will anyway).

    >gets(mod_name);
    gets is a function that even C programmers avoid like the plague. fgets would be better, but since you should be avoiding C-strings, use the getline function instead. It is declared in <string> as well and you use it like so:
    Code:
    getline ( cin, an_std_string );
    >infile.close(); // Tidying up
    >outfile.close();
    Just being pedantic, but this isn't necessary. Both infile and outfile are local objects. Their destructors are called when the function exits and that flushes/closes the streams.
    My best code is written with the delete key.

  7. #7
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    Is

    Code:
     cin.getline(str, len, '\n');
    the same thing? The book I'm reading uses gets() which is why I used it, but it's not that hard to change gets() into something else.

    >char mod_name[25], version[10], desc[150], cmds[100];
    C-strings are finicky and most novices have a lot of trouble with them. C++ std::strings in the <string> header are much easier to work with and are moe intuitive more intuitive (not to mention safer, but I will anyway).
    Err...
    As I said I'm just using ways I've learnt from a book. If there's a better way of doing it then please let me know. I find this all really fascinating!
    Last edited by cyberCLoWn; 12-13-2003 at 02:52 AM.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >the same thing?
    No, cin.getline is for C-style strings. The kind that use arrays and a nul termination character '\0'. The string class has its own overloaded getline function. Since it requires std::strings, it makes sense to be declared in <string>:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      string s;
    
      cout<<"What's your name: ";
      if ( std::getline ( std::cin, s ) )
        std::cout<<"Hello, "<< s <<'!'<<std::endl;
    }
    As opposed to the C-string equivalent which is declared as a part of std::istream and can be reached from <iostream>:
    Code:
    #include <iostream>
    
    int main()
    {
      char s[100];
    
      cout<<"What's your name: ";
      if ( std::cin.getline ( s, sizeof s ) )
        std::cout<<"Hello, "<< s <<'!'<<std::endl;
    }
    My best code is written with the delete key.

  9. #9
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124
    If I have this in a database:

    module1 version1 description1 command1
    module2 version2 description2 command2
    ...
    What I want to do is read from a database containing quite a few of those and then display certain things. Basically I want to take the first chars up to the whitespace and put it in an array, then move up a character and include everything up until the next whitespace. Then I'd have the values in an 4 different arrays which I can then work with. I could then do a loop to move to the next line and pick up the next values. There must be a way to do this, but as I don't understand a thing in my C++ book on I/O systems (chapter18 - I'm on 8). Anyone help me please?

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    while ( fin>> mod >> ver >> des >> com ) {
      // Process a single record
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 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