Converting strings to char

This is a discussion on Converting strings to char within the C++ Programming forums, part of the General Programming Boards category; What I'm trying to do is to get pieces of information from a file and then store them in struct ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    66

    Converting strings to char

    What I'm trying to do is to get pieces of information from a file and then store them in struct array. So far I've managed to get text from the file just the way I need it, but I can't figure out how to convert the strings I get into some kind of type that is storable in a struct.

    In the below code, I would like to store the contents of temp into a struct array. How do I do this?

    Code:
        if ( accfile )
        {
            string temp;
            stringstream subtemp;
            while ( getline (accfile, line) )
            {
                subtemp<< line;
                while ( getline (subtemp, temp, ';') )
                {
                    cout << temp << endl;
                }
                subtemp.clear();
            }
    
        }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have to figure out what you mean by "struct array" first. Generally I use = to assign strings to other things.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,975
    What do you mean by a "struct array"? std::string is a type that is "storable in a struct".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Sorry for the vagueness.

    For example,
    Code:
    struct mystruct
    {
    char first;
    char second;
    int third;
    char fourth;
    };
    I have a file which has lines of information and after seperating them via the getline I want to put them in that struct. Since I have a lot of lines with four items in each, I want to make an array of that struct. For example,
    Code:
    mystruct myarray [struct_max];
    I then want to store the information from the file (above post for code of how I extract it) in this array.

    I have char and int types in the struct, but they could all be made to be char for ease.
    Last edited by Furious5k; 01-02-2009 at 02:03 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are these things supposed to be single characters? If so, then string was wrong (or at least, overkill) in the first place. Are they supposed to be char arrays? If so, then why not make them strings?

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    No, they're not single characters. The lines go something like:
    ttttt, ttttttt; ttttttttttt; t; 123456
    ttttt, ttttttt; ttttttttttt; t; 123456

    I'm not sure I understand what you mean. Do you mean changing the struct to:
    Code:
    struct mystruct
    {
    string first;
    string second;
    ...
    };
    Because I tried this and it gave me errors. If this is supposed to work, please tell me and I will try again and post the errors I get.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Not only is that supposed to work, it will work. (You won't be able to get an int without lexical_cast, maybe, or strtol.)

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    I've got to learn to seperate, in my mind, that when I change two things at once and I still get an error, only one might be wrong. Stupid, stupid.

    So now that I have my struct with strings and it can accept the information from getline, how do I send it to the struct?

    When I try this:
    [CODE]
    mystruct.first = temp;
    [CODE]
    I get an error like this:
    "request for member `first' in `mystruct', which is of non-aggregate type `struct[2000]'"

    2000 being struct_max.
    Last edited by Furious5k; 01-02-2009 at 03:18 PM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    The compiler is probably asking which out of the many structs you have, do you want to assign it to? Do you want the 1st, the 1000th, the 500th, the 2nd or something else?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    I had, at one point, specified a variable for array number. See above mistake of changing things back that aren't not working.

    Now, I'm not getting any errors but all information is being stored in the [0] location of the array. What am I doing wrong with variable here?
    Code:
    int i = 0;
          while (getline (accfile, line))
            {
              subtemp << line;
              while (getline (subtemp, temp, ';'))
                {
                  switch (i)
                    {
                    case 0:
                      mystruct[i].first = temp;
                      break;
                    case 1:
                      mystruct[i].second = temp;
                      break;
                    ...
                    }
                }
              subtemp.clear();
              ++i;
            }
    Last edited by Furious5k; 01-02-2009 at 03:34 PM.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    Well duh, i is always 0 in your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Sorry, I don't know why I wrote ++x, it's ++i. and it's correctly written in my code, so I can only assume my incrementation is in the wrong place but I don't see that.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then all your information is not, in fact, being stored in the [0] element of your array.

    Granted you're storing one thing in the [0] slot, one thing in the [1] slot, ..., one thing in the [3] slot, rather than all together. But you're not storing everything in [0].

  14. #14
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    I think I now have a working code, thanks to all the comments. Could you just check that there really is nothing wrong with my latest version of it?
    Code:
    int i = 0;
          while (getline (accfile, line))
            {
              subtemp << line;
              int x = 0;
              while (getline (subtemp, temp, ';'))
                {
                  ++x;
                  switch (x)
                    {
                    case 0:
                      mystruct[x].first = temp;
                      break;
                    case 1:
                      mystruct[x].second = temp;
                      break;
                    ...
                    }
                }
              subtemp.clear();
              ++i;
            }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM

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