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. Is there any way to get this to work CString???

Is there any way to get this to work CString???

Scheduled Pinned Locked Moved C / C++ / MFC
helpgraphicsdata-structuresbusinessquestion
11 Posts 5 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 know I have been stuck on the same problem for the last few days but, I have to get this to work or my rpogram won't function correctly. Like I need it to. I am using the getline() function to read in a file. I need to put the first word on each line in a char array. string name, unit,min,max,value; for(int i=0; i<45; i++){ getline(fin, name, ','); if(name == "STOP") break; getline(fin, unit, ','); getline(fin, min, ','); getline(fin, max, ','); getline(fin, value); cout< All of the above are ways that I have tried. I don't get the correct word put in the char array. I know the words are getting read in from the file correctly. They are just not getting put in the char[] correctly. Is there another way that I could go about doing this? Is this the best wat to read in a file and put the words in a char[]? I have to use a char[] because of certain requirements that my boss has given me. I would love to use a vector, but it won't work Thanks for your help, Steven

    R S J 3 Replies Last reply
    0
    • J johnstonsk

      I know I have been stuck on the same problem for the last few days but, I have to get this to work or my rpogram won't function correctly. Like I need it to. I am using the getline() function to read in a file. I need to put the first word on each line in a char array. string name, unit,min,max,value; for(int i=0; i<45; i++){ getline(fin, name, ','); if(name == "STOP") break; getline(fin, unit, ','); getline(fin, min, ','); getline(fin, max, ','); getline(fin, value); cout< All of the above are ways that I have tried. I don't get the correct word put in the char array. I know the words are getting read in from the file correctly. They are just not getting put in the char[] correctly. Is there another way that I could go about doing this? Is this the best wat to read in a file and put the words in a char[]? I have to use a char[] because of certain requirements that my boss has given me. I would love to use a vector, but it won't work Thanks for your help, Steven

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

      If I remember your structure correctly, the first strcpy() method will work (you don't need the cast, though):

      strcpy(TSimHeader_arr[0].Name, name.c_str());

      What won't work is the method you're using to print it out:

      cout<<endl<<"names......."<<"\n"<<TSimHeader_arr[0].Name[i];

      You need to print out what you store to:

      cout<<endl<<"names......."<<"\n"<<TSimHeader_arr[0].Name;

      Your version was only printing one character, right?

      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
      • R Ryan Binns

        If I remember your structure correctly, the first strcpy() method will work (you don't need the cast, though):

        strcpy(TSimHeader_arr[0].Name, name.c_str());

        What won't work is the method you're using to print it out:

        cout<<endl<<"names......."<<"\n"<<TSimHeader_arr[0].Name[i];

        You need to print out what you store to:

        cout<<endl<<"names......."<<"\n"<<TSimHeader_arr[0].Name;

        Your version was only printing one character, right?

        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
        #3

        You are right it was only printing ouot 1 character. But, if later on I need to access the array of names and get Names[15] how can I do that? I just tried to print out TSimHeader_arr[0].Names[15] and it printed out Are the words getting put in the char Names[]? Sorry, but I just don't quite understand. Thanks, Steven

        R 1 Reply Last reply
        0
        • J johnstonsk

          You are right it was only printing ouot 1 character. But, if later on I need to access the array of names and get Names[15] how can I do that? I just tried to print out TSimHeader_arr[0].Names[15] and it printed out Are the words getting put in the char Names[]? Sorry, but I just don't quite understand. Thanks, Steven

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

          At their core, strings are an array of characters. The string "Hello" has 5 characters (plus a terminating NULL character), so it will need a 6-character array. Your Names array is declared as char Names[45], which means that it holds 45 characters, ie one string that can be up to [edit]44[/edit] characters long. If you are wanting to store more than one string, this won't work - an array of chars can only store one string. To store multiple strings you'll need to use a 2-dimensional array - an array of arrays of characters (think about it, it does make sense :)) I think you're wanting to store 45 strings. If each string is at most 10 characters long, then you'll need to declare Names as

          char Names[45][11];

          This is an array that holds 45 arrays of 11 characters, ie. it holds 45 strings that are up to 10 characters long each (remember the terminating NULL?). You'll have to work out how big to make the arrays to fit your strings (change the 11 to whatever is necessary to fit the string in). To write to these strings, use the code

          strcpy(TSimHeader_arr[0].Names[0], name.c_str());
          strcpy(TSimHeader_arr[0].Names[1], name.c_str());
          // etc...
          strcpy(TSimHeader_arr[0].Names[44], name.c_str());

          To display the strings, use

          cout << TSimHeader_arr[0].Names[0] << endl;
          cout << TSimHeader_arr[0].Names[1] << endl;
          // etc...

          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 1 Reply Last reply
          0
          • R Ryan Binns

            At their core, strings are an array of characters. The string "Hello" has 5 characters (plus a terminating NULL character), so it will need a 6-character array. Your Names array is declared as char Names[45], which means that it holds 45 characters, ie one string that can be up to [edit]44[/edit] characters long. If you are wanting to store more than one string, this won't work - an array of chars can only store one string. To store multiple strings you'll need to use a 2-dimensional array - an array of arrays of characters (think about it, it does make sense :)) I think you're wanting to store 45 strings. If each string is at most 10 characters long, then you'll need to declare Names as

            char Names[45][11];

            This is an array that holds 45 arrays of 11 characters, ie. it holds 45 strings that are up to 10 characters long each (remember the terminating NULL?). You'll have to work out how big to make the arrays to fit your strings (change the 11 to whatever is necessary to fit the string in). To write to these strings, use the code

            strcpy(TSimHeader_arr[0].Names[0], name.c_str());
            strcpy(TSimHeader_arr[0].Names[1], name.c_str());
            // etc...
            strcpy(TSimHeader_arr[0].Names[44], name.c_str());

            To display the strings, use

            cout << TSimHeader_arr[0].Names[0] << endl;
            cout << TSimHeader_arr[0].Names[1] << endl;
            // etc...

            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, That makes sense. Which would take up less space an array of char Names[45][10] or string[45]? I am trying to use the least amount of space. Thanks, Steven

            D J R 3 Replies Last reply
            0
            • J johnstonsk

              Ryan, That makes sense. Which would take up less space an array of char Names[45][10] or string[45]? I am trying to use the least amount of space. Thanks, Steven

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

              Names will be 450 bytes. I'm not sure about string as I don't use them. What does sizeof() report?

              1 Reply Last reply
              0
              • J johnstonsk

                I know I have been stuck on the same problem for the last few days but, I have to get this to work or my rpogram won't function correctly. Like I need it to. I am using the getline() function to read in a file. I need to put the first word on each line in a char array. string name, unit,min,max,value; for(int i=0; i<45; i++){ getline(fin, name, ','); if(name == "STOP") break; getline(fin, unit, ','); getline(fin, min, ','); getline(fin, max, ','); getline(fin, value); cout< All of the above are ways that I have tried. I don't get the correct word put in the char array. I know the words are getting read in from the file correctly. They are just not getting put in the char[] correctly. Is there another way that I could go about doing this? Is this the best wat to read in a file and put the words in a char[]? I have to use a char[] because of certain requirements that my boss has given me. I would love to use a vector, but it won't work Thanks for your help, Steven

                S Offline
                S Offline
                Shay Harel
                wrote on last edited by
                #7

                why not just to read it and stick it into a CStringArray ? You already have it as part of MFC.. CStringArray myarray; myarray.Add (StringFromFile); that's it.

                1 Reply Last reply
                0
                • J johnstonsk

                  Ryan, That makes sense. Which would take up less space an array of char Names[45][10] or string[45]? I am trying to use the least amount of space. Thanks, Steven

                  J Offline
                  J Offline
                  John M Drescher
                  wrote on last edited by
                  #8

                  johnstonsk wrote: Which would take up less space an array of char Names[45][10] or string[45]? string[45] would take a lot more space because of how it allocates the strings but since the data set is soooooo small I would not worry about this at all. Anyways it should be char Names[45][11] because you need 1 extra space for the '\0' character that terminates strings. Otherwise your strings would only be able to hold 9 characters... John

                  1 Reply Last reply
                  0
                  • J johnstonsk

                    I know I have been stuck on the same problem for the last few days but, I have to get this to work or my rpogram won't function correctly. Like I need it to. I am using the getline() function to read in a file. I need to put the first word on each line in a char array. string name, unit,min,max,value; for(int i=0; i<45; i++){ getline(fin, name, ','); if(name == "STOP") break; getline(fin, unit, ','); getline(fin, min, ','); getline(fin, max, ','); getline(fin, value); cout< All of the above are ways that I have tried. I don't get the correct word put in the char array. I know the words are getting read in from the file correctly. They are just not getting put in the char[] correctly. Is there another way that I could go about doing this? Is this the best wat to read in a file and put the words in a char[]? I have to use a char[] because of certain requirements that my boss has given me. I would love to use a vector, but it won't work Thanks for your help, Steven

                    J Offline
                    J Offline
                    John M Drescher
                    wrote on last edited by
                    #9

                    One other thing this is not a CString. CString is a class in MFC and ATL/WTL to handle strings.. John

                    1 Reply Last reply
                    0
                    • J johnstonsk

                      Ryan, That makes sense. Which would take up less space an array of char Names[45][10] or string[45]? I am trying to use the least amount of space. Thanks, Steven

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

                      The first one uses less memory. Are you wanting to transmit the information across a serial port or network of something like that? If you are, you can't use the second one - string internally stores the text as a pointer, so if you transmit it, you just transmit the pointer, not the text it points to. In this case, you'll have to use the first option. Just remember to increase your array size to fit your string in :)

                      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
                      • R Ryan Binns

                        The first one uses less memory. Are you wanting to transmit the information across a serial port or network of something like that? If you are, you can't use the second one - string internally stores the text as a pointer, so if you transmit it, you just transmit the pointer, not the text it points to. In this case, you'll have to use the first option. Just remember to increase your array size to fit your string in :)

                        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
                        #11

                        Yes, I have a PCI card with a gig of memory that is shared with 5 other machines. Some of the words are longer than others so, if I just make the 2nd dimension the size of the longest word will I have any problems reading them on the other side of the network? thanks, Steven

                        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