splitting file contents into struct

This is a discussion on splitting file contents into struct within the C++ Programming forums, part of the General Programming Boards category; Hi, its me again sry about all the questions lately... I have saved a file with a string of digits. ...

  1. #1
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69

    splitting file contents into struct

    Hi, its me again

    sry about all the questions lately...


    I have saved a file with a string of digits. I wish to split this string into sections of 3 digits and load each set of 3 into a field of a struct...

    ill try to explain some more:

    file contains this: (just an example could contain any amount of digits but it would always be able to split into exact "chunks" of 3.)

    152142138

    how would I go about reading those digits from the file in groups of 3? (im using fstream for file access).

    Code:
    struct nums
    {
    int num;
    };
    
    
    nums number[10];
    I want to load the first 3 into number[0].num, second 3 into number[1].num etc....


    any ideas?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I'd try something reading the every group of three into a string using getline() and then converting to int with atoi() and assigning that value to the appropriate element of the array.

  3. #3
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    well i was thinking I need to use getline() but i wasnt sure how to read only 3 at a time...

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    when used with C style strings you have to indicate how many char maximum for the input as the second parameter. I always forget whether the terminating null char is taken into account or not by the second parameter so you'll have to check it out for yourself.

    char buffer[4]; //will hold up 3 non-null char
    cin.getline(buffer, 4, '\n'); //if null char taken into account
    cin.getline(buffer, 3, '\n'); //if you have to leave room for the null char.
    Last edited by elad; 05-06-2004 at 08:57 AM.

  5. #5
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    ive tried that but it wont work becasue getline cant be used with int's

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    127
    Quote Originally Posted by explosive
    ive tried that but it wont work becasue getline cant be used with int's
    Convert the string to an int then.
    Code:
    #include <cstdlib>
    
    [...]
    
    for (int i = 0; i < 10 && cin.getline(buffer, sizeof buffer); i++) {
      // Validation here would be a good idea
      number[i].num = atoi(buffer);
    }

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    So the digits are placeholders for a type double? Is there more than one value in the file? If so do all values contain the same number of digits?

    To peel off one digit at a time from a multidigit decimal number you can use alternating modulo and division operators.

    1) read the entire number into a type double or float or long or whatever.

    2) assign num % 10 to first index of an array of ints.

    3) increment the index

    4) divide num by 10

    5) repeat step 2 through 4 until num equals zero.

    You now have each digit peeled off as an int with the index of the array in which it resides representing the power of 10 placeholder the digit had in the original number. Now you can loop through the array in grabbing three ints at a time starting from the front, or the back, whichever you need. You can leave them as individual ints or convert them to a single value or do whatever you want with them.

    As an alternative, you could use a stringstream (or equivalent C style function) to convert the numeric value into a string. Then you can peel them off three at a time and leave them as characters or convert them to three digit ints with atoi(), etc.

  8. #8
    C++ Learner :D
    Join Date
    Mar 2004
    Posts
    69
    thank you all for your help

    ill have a go with your ideas and see how it goes...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM

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