sprintf buffer issue, wrong assignment to char array

Multi tool use
Multi tool use












0















I got an issue with sprintf buffer.
As you can see in the code down below I'm saving with sprintf a char array to the buffer, so pFile can check if there's a file named like that in the folder. If it's found, the buffer value will be assigned to timecycles[numCycles], and numCycles will be increased. Example: timecycles[0] = "timecyc1.dat". It works well, and as you can see in the console output it recognizes that there are only timecyc1.dat and timecyc5.dat in the folder. But as long as I want to read timecycles with a for loop, both indexes have the value "timecyc9.dat", eventhough it should be "timecyc1.dat" for timecycles[0] and "timecyc5.dat" for timecycles1. Second thing is, how can I write the code so readTimecycles() returns char* timecycles, and I could just initialize it in the main function with char* timecycles[9] = readTimecycles() or anything like that?



Console output



#include <iostream>
#include <cstdio>

char* timecycles[9];

void readTimecycles()
{
char buffer[256];
int numCycles = 0;
FILE* pFile = NULL;

for (int i = 1; i < 10; i++)
{
sprintf(buffer, "timecyc%d.dat", i);
pFile = fopen(buffer, "r");

if (pFile != NULL)
{
timecycles[numCycles] = buffer;
numCycles++;
std::cout << buffer << std::endl; //to see if the buffer is correct
}

}
for (int i = 0; i < numCycles; i++)
{
std::cout << timecycles[i] << std::endl; //here's the issue with timecyc9.dat
}
}

int main()
{
readTimecycles();

return 0;
}









share|improve this question



























    0















    I got an issue with sprintf buffer.
    As you can see in the code down below I'm saving with sprintf a char array to the buffer, so pFile can check if there's a file named like that in the folder. If it's found, the buffer value will be assigned to timecycles[numCycles], and numCycles will be increased. Example: timecycles[0] = "timecyc1.dat". It works well, and as you can see in the console output it recognizes that there are only timecyc1.dat and timecyc5.dat in the folder. But as long as I want to read timecycles with a for loop, both indexes have the value "timecyc9.dat", eventhough it should be "timecyc1.dat" for timecycles[0] and "timecyc5.dat" for timecycles1. Second thing is, how can I write the code so readTimecycles() returns char* timecycles, and I could just initialize it in the main function with char* timecycles[9] = readTimecycles() or anything like that?



    Console output



    #include <iostream>
    #include <cstdio>

    char* timecycles[9];

    void readTimecycles()
    {
    char buffer[256];
    int numCycles = 0;
    FILE* pFile = NULL;

    for (int i = 1; i < 10; i++)
    {
    sprintf(buffer, "timecyc%d.dat", i);
    pFile = fopen(buffer, "r");

    if (pFile != NULL)
    {
    timecycles[numCycles] = buffer;
    numCycles++;
    std::cout << buffer << std::endl; //to see if the buffer is correct
    }

    }
    for (int i = 0; i < numCycles; i++)
    {
    std::cout << timecycles[i] << std::endl; //here's the issue with timecyc9.dat
    }
    }

    int main()
    {
    readTimecycles();

    return 0;
    }









    share|improve this question

























      0












      0








      0








      I got an issue with sprintf buffer.
      As you can see in the code down below I'm saving with sprintf a char array to the buffer, so pFile can check if there's a file named like that in the folder. If it's found, the buffer value will be assigned to timecycles[numCycles], and numCycles will be increased. Example: timecycles[0] = "timecyc1.dat". It works well, and as you can see in the console output it recognizes that there are only timecyc1.dat and timecyc5.dat in the folder. But as long as I want to read timecycles with a for loop, both indexes have the value "timecyc9.dat", eventhough it should be "timecyc1.dat" for timecycles[0] and "timecyc5.dat" for timecycles1. Second thing is, how can I write the code so readTimecycles() returns char* timecycles, and I could just initialize it in the main function with char* timecycles[9] = readTimecycles() or anything like that?



      Console output



      #include <iostream>
      #include <cstdio>

      char* timecycles[9];

      void readTimecycles()
      {
      char buffer[256];
      int numCycles = 0;
      FILE* pFile = NULL;

      for (int i = 1; i < 10; i++)
      {
      sprintf(buffer, "timecyc%d.dat", i);
      pFile = fopen(buffer, "r");

      if (pFile != NULL)
      {
      timecycles[numCycles] = buffer;
      numCycles++;
      std::cout << buffer << std::endl; //to see if the buffer is correct
      }

      }
      for (int i = 0; i < numCycles; i++)
      {
      std::cout << timecycles[i] << std::endl; //here's the issue with timecyc9.dat
      }
      }

      int main()
      {
      readTimecycles();

      return 0;
      }









      share|improve this question














      I got an issue with sprintf buffer.
      As you can see in the code down below I'm saving with sprintf a char array to the buffer, so pFile can check if there's a file named like that in the folder. If it's found, the buffer value will be assigned to timecycles[numCycles], and numCycles will be increased. Example: timecycles[0] = "timecyc1.dat". It works well, and as you can see in the console output it recognizes that there are only timecyc1.dat and timecyc5.dat in the folder. But as long as I want to read timecycles with a for loop, both indexes have the value "timecyc9.dat", eventhough it should be "timecyc1.dat" for timecycles[0] and "timecyc5.dat" for timecycles1. Second thing is, how can I write the code so readTimecycles() returns char* timecycles, and I could just initialize it in the main function with char* timecycles[9] = readTimecycles() or anything like that?



      Console output



      #include <iostream>
      #include <cstdio>

      char* timecycles[9];

      void readTimecycles()
      {
      char buffer[256];
      int numCycles = 0;
      FILE* pFile = NULL;

      for (int i = 1; i < 10; i++)
      {
      sprintf(buffer, "timecyc%d.dat", i);
      pFile = fopen(buffer, "r");

      if (pFile != NULL)
      {
      timecycles[numCycles] = buffer;
      numCycles++;
      std::cout << buffer << std::endl; //to see if the buffer is correct
      }

      }
      for (int i = 0; i < numCycles; i++)
      {
      std::cout << timecycles[i] << std::endl; //here's the issue with timecyc9.dat
      }
      }

      int main()
      {
      readTimecycles();

      return 0;
      }






      c++






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '18 at 12:15









      BlinguBlingu

      1




      1
























          2 Answers
          2






          active

          oldest

          votes


















          1














          With the assignment



          timecycles[numCycles] = buffer;


          you make all pointers point to the same buffer, since you only have a single buffer.



          Since you're programming in C++ you could easily solve your problem by using std::string instead.





          If I would remake your code into something a little-more C++-ish and less C-ish, it could look something like



          std::array<std::string, 9> readTimeCycles()
          {
          std::array<std::string, 9> timecycles;

          for (size_t i = 0; i < timecycles.size(); ++i)
          {
          // Format the file-name
          std::string filename = "timecyc" + std::to_string(i + 1) + ".dat";

          std::ifstream file(filename);
          if (file)
          {
          // File was opened okay
          timecycles[i] = filename;
          }
          }

          return timecycles;
          }


          References:




          • std::array

          • std::string

          • std::to_string

          • std::ifstream






          share|improve this answer

































            1














            The fundamental problem is that your notion of a string doesn't match what a 'char array' is in C++. In particular you think that because you assign timecycles[numCycles] = buffer; somehow the chars of the char array are copied. But in C++ all that is being copied is a pointer, so timecycles ends up with multiple pointers to the same buffer. And that's not to mention the problem you will have that when you exit the readTimecycles function. At that point you will have multiple pointers to a buffer which no longer exists as it gets destroyed when you exit the readTimecycles function.



            The way to fix this is to use C++ code that does match your expectations. In particular a std::string will copy in the way you expect it to. Here's how you can change your code to use std::string



            #include <string>

            std::string timecycles[9];

            timecycles[numCycles] = buffer; // now this really does copy a string





            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53977499%2fsprintf-buffer-issue-wrong-assignment-to-char-array%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              With the assignment



              timecycles[numCycles] = buffer;


              you make all pointers point to the same buffer, since you only have a single buffer.



              Since you're programming in C++ you could easily solve your problem by using std::string instead.





              If I would remake your code into something a little-more C++-ish and less C-ish, it could look something like



              std::array<std::string, 9> readTimeCycles()
              {
              std::array<std::string, 9> timecycles;

              for (size_t i = 0; i < timecycles.size(); ++i)
              {
              // Format the file-name
              std::string filename = "timecyc" + std::to_string(i + 1) + ".dat";

              std::ifstream file(filename);
              if (file)
              {
              // File was opened okay
              timecycles[i] = filename;
              }
              }

              return timecycles;
              }


              References:




              • std::array

              • std::string

              • std::to_string

              • std::ifstream






              share|improve this answer






























                1














                With the assignment



                timecycles[numCycles] = buffer;


                you make all pointers point to the same buffer, since you only have a single buffer.



                Since you're programming in C++ you could easily solve your problem by using std::string instead.





                If I would remake your code into something a little-more C++-ish and less C-ish, it could look something like



                std::array<std::string, 9> readTimeCycles()
                {
                std::array<std::string, 9> timecycles;

                for (size_t i = 0; i < timecycles.size(); ++i)
                {
                // Format the file-name
                std::string filename = "timecyc" + std::to_string(i + 1) + ".dat";

                std::ifstream file(filename);
                if (file)
                {
                // File was opened okay
                timecycles[i] = filename;
                }
                }

                return timecycles;
                }


                References:




                • std::array

                • std::string

                • std::to_string

                • std::ifstream






                share|improve this answer




























                  1












                  1








                  1







                  With the assignment



                  timecycles[numCycles] = buffer;


                  you make all pointers point to the same buffer, since you only have a single buffer.



                  Since you're programming in C++ you could easily solve your problem by using std::string instead.





                  If I would remake your code into something a little-more C++-ish and less C-ish, it could look something like



                  std::array<std::string, 9> readTimeCycles()
                  {
                  std::array<std::string, 9> timecycles;

                  for (size_t i = 0; i < timecycles.size(); ++i)
                  {
                  // Format the file-name
                  std::string filename = "timecyc" + std::to_string(i + 1) + ".dat";

                  std::ifstream file(filename);
                  if (file)
                  {
                  // File was opened okay
                  timecycles[i] = filename;
                  }
                  }

                  return timecycles;
                  }


                  References:




                  • std::array

                  • std::string

                  • std::to_string

                  • std::ifstream






                  share|improve this answer















                  With the assignment



                  timecycles[numCycles] = buffer;


                  you make all pointers point to the same buffer, since you only have a single buffer.



                  Since you're programming in C++ you could easily solve your problem by using std::string instead.





                  If I would remake your code into something a little-more C++-ish and less C-ish, it could look something like



                  std::array<std::string, 9> readTimeCycles()
                  {
                  std::array<std::string, 9> timecycles;

                  for (size_t i = 0; i < timecycles.size(); ++i)
                  {
                  // Format the file-name
                  std::string filename = "timecyc" + std::to_string(i + 1) + ".dat";

                  std::ifstream file(filename);
                  if (file)
                  {
                  // File was opened okay
                  timecycles[i] = filename;
                  }
                  }

                  return timecycles;
                  }


                  References:




                  • std::array

                  • std::string

                  • std::to_string

                  • std::ifstream







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 30 '18 at 12:24

























                  answered Dec 30 '18 at 12:18









                  Some programmer dudeSome programmer dude

                  297k24252414




                  297k24252414

























                      1














                      The fundamental problem is that your notion of a string doesn't match what a 'char array' is in C++. In particular you think that because you assign timecycles[numCycles] = buffer; somehow the chars of the char array are copied. But in C++ all that is being copied is a pointer, so timecycles ends up with multiple pointers to the same buffer. And that's not to mention the problem you will have that when you exit the readTimecycles function. At that point you will have multiple pointers to a buffer which no longer exists as it gets destroyed when you exit the readTimecycles function.



                      The way to fix this is to use C++ code that does match your expectations. In particular a std::string will copy in the way you expect it to. Here's how you can change your code to use std::string



                      #include <string>

                      std::string timecycles[9];

                      timecycles[numCycles] = buffer; // now this really does copy a string





                      share|improve this answer






























                        1














                        The fundamental problem is that your notion of a string doesn't match what a 'char array' is in C++. In particular you think that because you assign timecycles[numCycles] = buffer; somehow the chars of the char array are copied. But in C++ all that is being copied is a pointer, so timecycles ends up with multiple pointers to the same buffer. And that's not to mention the problem you will have that when you exit the readTimecycles function. At that point you will have multiple pointers to a buffer which no longer exists as it gets destroyed when you exit the readTimecycles function.



                        The way to fix this is to use C++ code that does match your expectations. In particular a std::string will copy in the way you expect it to. Here's how you can change your code to use std::string



                        #include <string>

                        std::string timecycles[9];

                        timecycles[numCycles] = buffer; // now this really does copy a string





                        share|improve this answer




























                          1












                          1








                          1







                          The fundamental problem is that your notion of a string doesn't match what a 'char array' is in C++. In particular you think that because you assign timecycles[numCycles] = buffer; somehow the chars of the char array are copied. But in C++ all that is being copied is a pointer, so timecycles ends up with multiple pointers to the same buffer. And that's not to mention the problem you will have that when you exit the readTimecycles function. At that point you will have multiple pointers to a buffer which no longer exists as it gets destroyed when you exit the readTimecycles function.



                          The way to fix this is to use C++ code that does match your expectations. In particular a std::string will copy in the way you expect it to. Here's how you can change your code to use std::string



                          #include <string>

                          std::string timecycles[9];

                          timecycles[numCycles] = buffer; // now this really does copy a string





                          share|improve this answer















                          The fundamental problem is that your notion of a string doesn't match what a 'char array' is in C++. In particular you think that because you assign timecycles[numCycles] = buffer; somehow the chars of the char array are copied. But in C++ all that is being copied is a pointer, so timecycles ends up with multiple pointers to the same buffer. And that's not to mention the problem you will have that when you exit the readTimecycles function. At that point you will have multiple pointers to a buffer which no longer exists as it gets destroyed when you exit the readTimecycles function.



                          The way to fix this is to use C++ code that does match your expectations. In particular a std::string will copy in the way you expect it to. Here's how you can change your code to use std::string



                          #include <string>

                          std::string timecycles[9];

                          timecycles[numCycles] = buffer; // now this really does copy a string






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 30 '18 at 12:29

























                          answered Dec 30 '18 at 12:24









                          johnjohn

                          35.8k12847




                          35.8k12847






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53977499%2fsprintf-buffer-issue-wrong-assignment-to-char-array%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              CH,Q1xIbFvnY,REhxppLhpCC9i,ckVLjRs,SBjBysu,DMKxACc nG7C1iybep79,CXjCLVXiazaAfP9oX K,9I VA7Xi3triDgAp5sgThYY4
                              bWiy wV oMRQPId65 1,47Bkm8VWg7PQVuK G18jQSBLFATtXUvrxvQ 6B3ATc5LaTk D

                              Popular posts from this blog

                              Monofisismo

                              Angular Downloading a file using contenturl with Basic Authentication

                              Olmecas