Thread: Obtaining complete structure from basic text file (non-included) possible????

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    45

    Obtaining complete structure from basic text file (non-included) possible????

    (THIS IS NOT FOR WINDOWS/PC/LINUX/MAC, w/e)

    well... Im making a media app and i want it to be skinnable... there for Ive used Collision Detection with a cursor, and have a structure FULL of all the images X, Y, Width, and Height data... Im wanting to copy and paste this INTO a config file, and read exactly from it, into my app... so basically copy paste whatever is in the config file, into the app itself at run time...

    any ideas? maybe atoi alot?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Write your file in binary mode with fwrite(), that way you don't even need to convert anything. For reading, there is a companion function fread()
    Last edited by whiteflags; 06-20-2006 at 04:03 PM. Reason: completeness

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Assuming you want text files, you just need to read and write each field. Set up a function to load information from a file into a structure. You'll have to verify that the information provided matches what you require, and provided default cases for things that aren't in the file, as well as error handling for it.

    You may want keyword / value pairs. Something like:
    Code:
    ImageWidth    10
    ImageHeight   20
    ...
    Foo   bar
    For all of your fields. Read a bit of info, see what the first section belongs to, which will tell you what type of data to expect for the second half. For example 'ImageWidth' may mean that we expect a number next. 'Foo' may mean we expect a string next.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    hmm.... Thats what I'm basically asking if and whats thebest way, to load information from a file into a structure...

    Read from x byte of a file to y bytes of a file...
    (not in actual configuration file line1-)backroundX [byte12][byte13]0

    And grab the 12th to 14th byte and feed that into the backroundX variable... But im not sure what i can do about go to next line again...

    Or, ill just parse it basically...

    Could someone point into how i could read whatever is inside the [] brackets of every line in a file and feed that into different integers for each line? I know you use 'atoi', but i need to read it line by line... then on line 1, feed the number inside the [ and ] into an integer...

    Im going to obviously test around, but I just thoguht Id get either the entire snippet, or the exact definiton of whawt im trying to do here to help me...

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    FAQ entry on getting a line from the keyboard. It's not much different to get a line from a file. You just change the final argument to fgets. Things like strchr can locate characters in a string, or sscanf to parse a string for a set format. You could always just use fgetc in a loop. There are a number of ways to do this.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    Yes, I know a for loop is the most common... So... I hope you dont mind I express and dump my thoughts here for later recap...

    -ok, I'll go through the config file once, labeling each 'fgets()' as a number/line number (check)
    -Now in my structure, ill have each data type = a line number from that file (working on it, any ideas? google is giving methe cold shoulder X( ... )

    Thats about it really i guess? Assign each time the fgets() is called in a for loop a line number via this function:
    Code:
    void FileRead(const char* FilePath) {
         auto char line[200];
         auto int LineCount = 1;
         auto FILE *fp;
         if((fp = fopen(FilePath, "rb"))==NULL) { printf("%s does not exist!", FilePath);
         } else {
         printf("\n\nNow Opening: %s\n", FilePath);
         while ( fgets(line, sizeof line, fp) != NULL)
         {  
               LineCount++;
               //printf("%i: %s",LineCount,line);
         }
    }
    }
    Someone could look over that real quick and see if thats an efficient way to assign each line in the file, a value inside the LineCount? Thatd be great...

    (oh and i have an array of the structures, like the structure has 4 varaibles, X, Y, Widht, Height... then the arraysetsall the images with there correct variables...)

    Now in my structure array, ill substitute the actual valuesfor eachdata type with the value found on a line i specify... now how could i go about that? What do you know will read a line, and take the integers off it and return it? OFF TO GOOGLE!!! (maybe the FAQ)

    EDIT

    hmm... My function works, but I cant seem to set my structure to now read from that line... Here is my structure :
    Code:
    ImageSAtr ImageSettings[19] = {
    {6, 74, 115, 98},   // FILLERers                  c
    {0, 0, 480, 272},      // backround               c
    {160, 60, 272, 116},       // logo                c 
    {208, 216, 48, 48},        // play                c
    {208, 216, 48, 48},      // PAUSE                 c
    {115, 233, 13, 13},        // stop                c
    {83, 4, 93, 26},             // Now playing ModeNum  c
    {321, 4, 64, 26},              // Option ModeNum       c
    {212, 4, 64, 26},              // Browser ModeNum    c
    {43, 5, 25, 25},      // Next Visual              c
    {15, 5, 25, 25},      // previous visual          c
    {270, 231, 14, 15},            // Forward         
    {182, 230, 14, 15},            // Rewind          
    {156, 231, 17, 15},            // Previous Track  
    {292, 231, 17, 15},            // Next Track       
    {340, 231, 17, 16},            // Repeat On               
    {434, 40, 43, 18},             // EXIT            c
    {217, 117, 1, 1}             // cursor            c 
    };
    Im thinking of making an array of integers, and a new arra buffer... hmm...

    EDIT

    Here is what I came up with, but it crashes... Think somone could help me out? Take the numbers from a line, feed it into an array of integers as '0' AKA line 0, and repeat til end of file/fgets() becomes false/breaks...
    Code:
    int LineAndRow[500];
    int y;
    void FileRead(const char* FilePath) {
         auto char line[MAX_MENU_ROWS];
         int count = 0;
         auto FILE *fp;
         if((fp = fopen(FilePath, "rb"))==NULL) { printf("%s does not exist!", FilePath);
         } else {
         printf("\n\nNow Opening: %s\n", FilePath);
         while ( fgets(line, sizeof line, fp) != NULL)
         {  
               
                                    snprintf(LineAndRow[count],4, "%i", (atoi(line)));
                                    printf(LineAndRow[count]);
                                    count++;
         }
    }
    }
    Im using snprintf to prevent an over flow, and since the total MAX number of bytes to be used in a image dimension/coordinate is 3 bytes long... it works out, but i get a crask when running this... Anyone tell me whats wrong with what im trying to do and this?
    Last edited by SG57; 06-21-2006 at 02:56 AM.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Open in binary mode and use fread() to read structure data from disk.

    No reason to store this as a text file unless you want it to be editable. Personally I store all settings for applications such as window locations, project options, etc., in binary files.

    For game config files I store everything in a text cfg file and make a list of specific strings to look for and at run-time set the correct variable in code to the value in the file. You can use the default Windows INI file support functions as well to read in config settings for your app.

    In C++ you would either create a Serialize() function for your objects and then traverse the chain of objects calling Serialize() for all of them or you would override the insertion and extraction operators relative to your object.

  8. #8
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    Well, Im just going to set the values in my structure to 'LineAndRow[line#w/eHere], LineAndRow[line#w/eHere], ect...

    I found my crashing error, it was the print LineAndRow... I was printing an integer, not a string...

    Ok, I can succesfully print out the number of each line, but i cant feed that number into a number.... but im working on it...

    Oh and yes the config file is editable... Its for skinning

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:54 AM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM