Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CString questions

CString questions

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    johnstonsk
    wrote on last edited by
    #1

    I have been reading in a file using the getline function and using a (,) as a delimiter. I made a string caled name. string name; Then I do a getline(fin, name, ','); I need to put the names in a char array. I thought I was doing that until I needed to use the names and I was getting garbage instead of the names I thought I was putting in the char array. I was doing this to add them to the array which gives me junk. Names[i] = stdup(name.c_str()) How can I read in a list of names 3 - 8 characters long and store them in a char arrray? I can't have an array of strings because of other constraints. Thanks, Steven

    A R 2 Replies Last reply
    0
    • J johnstonsk

      I have been reading in a file using the getline function and using a (,) as a delimiter. I made a string caled name. string name; Then I do a getline(fin, name, ','); I need to put the names in a char array. I thought I was doing that until I needed to use the names and I was getting garbage instead of the names I thought I was putting in the char array. I was doing this to add them to the array which gives me junk. Names[i] = stdup(name.c_str()) How can I read in a list of names 3 - 8 characters long and store them in a char arrray? I can't have an array of strings because of other constraints. Thanks, Steven

      A Offline
      A Offline
      Alexandru Savescu
      wrote on last edited by
      #2

      Where's the CString problem here? :) ;) Best regards, Alexandru Savescu P.S. Interested in art? Visit this!

      1 Reply Last reply
      0
      • J johnstonsk

        I have been reading in a file using the getline function and using a (,) as a delimiter. I made a string caled name. string name; Then I do a getline(fin, name, ','); I need to put the names in a char array. I thought I was doing that until I needed to use the names and I was getting garbage instead of the names I thought I was putting in the char array. I was doing this to add them to the array which gives me junk. Names[i] = stdup(name.c_str()) How can I read in a list of names 3 - 8 characters long and store them in a char arrray? I can't have an array of strings because of other constraints. Thanks, Steven

        R Offline
        R Offline
        Ryan Binns
        wrote on last edited by
        #3

        If you can be sure that you have enough space in your Names[i] field, then do

        strcpy(Names[i], name.c_str());

        instead of

        Names[i] = strdup(name.c_str());

        I seem to remember that your Names definition was an array of chars, in which case this would work. Hope this helps,

        Ryan

        "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

        D J 2 Replies Last reply
        0
        • R Ryan Binns

          If you can be sure that you have enough space in your Names[i] field, then do

          strcpy(Names[i], name.c_str());

          instead of

          Names[i] = strdup(name.c_str());

          I seem to remember that your Names definition was an array of chars, in which case this would work. Hope this helps,

          Ryan

          "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Ryan Binns wrote: I seem to remember that your Names definition was an array of chars, in which case this would work. Hmmm, I think it was an array of 45 char pointers, which made little sense since (!) the numeric members of that structure were NOT arrays. Edit: Looks like we were both right. In one post he had

          struct TSimHeader
          {
          char Name[45];
          ...
          }

          and in another he had

          struct TSimHeader
          {
          char *Name[45];
          ...
          }

          J 1 Reply Last reply
          0
          • R Ryan Binns

            If you can be sure that you have enough space in your Names[i] field, then do

            strcpy(Names[i], name.c_str());

            instead of

            Names[i] = strdup(name.c_str());

            I seem to remember that your Names definition was an array of chars, in which case this would work. Hope this helps,

            Ryan

            "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

            J Offline
            J Offline
            johnstonsk
            wrote on last edited by
            #5

            Ryan, When I use the strcpy() function I get the error: D:\.......cpp(105) : error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast The array that I am trying to put the ;name in is a char[]. Any Ideas? Stevne

            R 1 Reply Last reply
            0
            • J johnstonsk

              Ryan, When I use the strcpy() function I get the error: D:\.......cpp(105) : error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast The array that I am trying to put the ;name in is a char[]. Any Ideas? Stevne

              R Offline
              R Offline
              Ryan Binns
              wrote on last edited by
              #6

              Post the definition of Names. That will help :)

              Ryan

              "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

              J 1 Reply Last reply
              0
              • D David Crow

                Ryan Binns wrote: I seem to remember that your Names definition was an array of chars, in which case this would work. Hmmm, I think it was an array of 45 char pointers, which made little sense since (!) the numeric members of that structure were NOT arrays. Edit: Looks like we were both right. In one post he had

                struct TSimHeader
                {
                char Name[45];
                ...
                }

                and in another he had

                struct TSimHeader
                {
                char *Name[45];
                ...
                }

                J Offline
                J Offline
                johnstonsk
                wrote on last edited by
                #7

                You have a good memoy, But if you rememeber, I have to actually put the chars in the array and not just the pointers because the array is getting sent to another PC. The recieving PC would have no use for the pointers on my machine. here is my structure. **struct TSimSignal { int SimWriteFlag; int DisplayReadFlag; double Value[45]; double TimeStamp; }static TSimSignal_arr[1]; struct TSimHeader { char Name[45]; char Unit[45]; double Min[45]; double Max[45]; int SignalCount; int SimStatus; }static TSimHeader_arr[1];** Once again back to putting junk in the char arrays (Name and Unit) thanks, steven

                R 1 Reply Last reply
                0
                • R Ryan Binns

                  Post the definition of Names. That will help :)

                  Ryan

                  "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                  J Offline
                  J Offline
                  johnstonsk
                  wrote on last edited by
                  #8

                  I posted them in a reply above this one. Thanks Ryan, Steven

                  1 Reply Last reply
                  0
                  • J johnstonsk

                    You have a good memoy, But if you rememeber, I have to actually put the chars in the array and not just the pointers because the array is getting sent to another PC. The recieving PC would have no use for the pointers on my machine. here is my structure. **struct TSimSignal { int SimWriteFlag; int DisplayReadFlag; double Value[45]; double TimeStamp; }static TSimSignal_arr[1]; struct TSimHeader { char Name[45]; char Unit[45]; double Min[45]; double Max[45]; int SignalCount; int SimStatus; }static TSimHeader_arr[1];** Once again back to putting junk in the char arrays (Name and Unit) thanks, steven

                    R Offline
                    R Offline
                    Ryan Binns
                    wrote on last edited by
                    #9

                    TSimHeader header;
                    strcpy(header.Name, name.c_str());

                    Should do the trick. If you're sending an array of TSimHeader's then you'll have something like this:

                    TSimHeader headers[10];
                    strcpy(header[0].Name, name.c_str());

                    Similarly for other members of the array. Hope this helps,

                    Ryan

                    "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups