Thread: File IO, .ini readable?

  1. #1
    #junkie
    Join Date
    Oct 2004
    Posts
    240

    Post File IO, .ini readable?

    I wanna start off as simplistic and somewhat useful as possible. So i think imma try to make a Quiz program. But one thing i would like to do is hold the questions and whatnot in a ini file.

    So can you point me in the right direction for ini reading? or would i have to create a function to do that for me? And also, Can someone point me in the right direction for something like "tokens"? In other words, if you have
    string myString = "jake.lee.john.joe.phill"

    How can you retrieve the 2nd token, and whatnot. In mIRC you can use lovely convienient things called token identifiers, such as $gettok which allows you to retrieve a "token" from within text and any seperating character. ie if i wanted to get lee from myString i would type $gettok(myString,2,46) where "2" is the 2nd token, and "46" is the 46th ASCII character. Commonly known as ".", so can anyone help me with these things? Im not asking anyone to write em for me, just if they exist let me know where, and ifnot can you possibly give me a link or something in the directed fields? Thanks!
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    For your second question, the function strtok() comes to mind, but having always made my own string functions (I don't know why, actually...), I wouldn't be able to tell you for sure. If you're feeling ambitious and are feeling very comfortable with C-Style strings, you can just set up a for loop, cycle through the elements in the string, and every time you encounter the ".", in your example, start reading to a different string. You'd end up with an array of string, and thus an array of tokens.

    As for your first question. File formats are pretty much completely up to you. It sounds like you're fairly new to programming so I would go with a simple setup. Set up a file of questions (questions.dat or whatever) and just have one question per line, then have an answers file with all the answers on corresponding lines. You use strcmp() to check the answer the user inputs with that read from the answers file. If you google enough you could probably find code libraries to make this easier, but to be honest - you'll learn a lot more if you do it yourself. Probably easier in the long run for a project of this size, actually.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>So can you point me in the right direction for ini reading?

    Reading files using C++ syntax requires either an fstream in ios::in mode or an ifstream. You can use C file syntax, FILE *, in C++ as well.

  4. #4
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    ya i can read files simplisticly, but reading an ini formatted file is another story. Is their a pre programmed function for such a use? ie "ifstream.ini(File,Group,IniItem)" somethin like that.
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  5. #5
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    is this wot u mean by reading from an ini file...

    this is a quick program that I threw together, i hope this helps in some way...

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <fstream>
    
    using namespace std;
    char str[256];
    
    int main(int argc, char *argv[])
    {
        
        cout << "Please enter a string (maximum of 256 chars):" << endl;
        cin.getline ( str,256, '\n' );
        ofstream a_file("test.ini");
        a_file << str;
        a_file.close();
        cout << "This is what is contained in the file test.ini:" << endl;
        cout << "" << endl;
        std::ifstream b_file("test.ini");
        std::cout << str;
        cout << "" << endl;
        b_file.close();
      
      system("PAUSE");	
      return 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    There is no function specifically for reading ini formatted files. You can either look for an INI API online or just make your own file format and function, or just our own INI reading function. It sounds like you already know standard file I/O in C/C++. Correct?

  7. #7
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    I assume there is far more to it then i know of, i dont even fully understand b_file, a_file, ect., but the tutorials iv covered so far have lightly explained the usage of the most basic applications of fstream.

    But if i have to make my own thats no problem at all, if thats going to happen i'll just need to learn a bit more and go a step further than standard INI, such as subitems and whatnot. Same thing i did for hash tables in mIRC.

    Thanks all for your replies!

    *edit*
    And silly nub me, the application and usage would be a bit diff, but i didnt even think about using Arrays in place of my "tokens". So i could just go
    [Jake][Lee][John] ect.. (the order is wrong im sure, cant remember and too lazy to load the page to check, but you get the idea hehe).

    Thanks
    Last edited by Zeusbwr; 10-12-2004 at 03:04 PM.
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The Windows function for reading .ini files is GetPrivateProfileString(). Search the board for samples.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM