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. Conversion from- C++ to C-language

Conversion from- C++ to C-language

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++graphicsquestion
5 Posts 3 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.
  • Z Offline
    Z Offline
    zak100
    wrote on last edited by
    #1

    Hi, I want to convert the following C++ code into C-language. For the following code,

    ifstream inFile;
    inFile >> num_rows;
    file_buffer.resize(num_rows);

    I wrote:

    FILE* inFile;
    inFile = fopen(argv[1], "r");
    fgets(strNum_rows, 20, inFile);
    num_rows = atoi(strNum_rows);

    But I can't understand how to declare file_buffer and how to convert :

    vector file_buffer;
    file_buffer.resize(num_rows);

    in 'C' language? Somebody please guide me. Zulfi.

    D 1 Reply Last reply
    0
    • Z zak100

      Hi, I want to convert the following C++ code into C-language. For the following code,

      ifstream inFile;
      inFile >> num_rows;
      file_buffer.resize(num_rows);

      I wrote:

      FILE* inFile;
      inFile = fopen(argv[1], "r");
      fgets(strNum_rows, 20, inFile);
      num_rows = atoi(strNum_rows);

      But I can't understand how to declare file_buffer and how to convert :

      vector file_buffer;
      file_buffer.resize(num_rows);

      in 'C' language? Somebody please guide me. Zulfi.

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

      Is file_buffer a vector? If so, you could have something like:

      int *file_buffer = malloc(num_rows * sizeof(int)); // assumes you are reading ints from file

      Note: your fgets() call could be the source of an issue. It'll read 19 characters, or until an EOF or newline character is encountered. If it reads too many, then any subsequent fgets() calls will start at the wrong spot in the file stream.

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      Z 1 Reply Last reply
      0
      • D David Crow

        Is file_buffer a vector? If so, you could have something like:

        int *file_buffer = malloc(num_rows * sizeof(int)); // assumes you are reading ints from file

        Note: your fgets() call could be the source of an issue. It'll read 19 characters, or until an EOF or newline character is encountered. If it reads too many, then any subsequent fgets() calls will start at the wrong spot in the file stream.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        Z Offline
        Z Offline
        zak100
        wrote on last edited by
        #3

        Hi, Thanks for your response. God bless you. Yes, file_buffer is a vector. Thanks for your comments about fegts(). I am reading from a file which has following contents: 22 1111.909 12.3456 0.000345 and so on. First line is a integer and tells the count of data in the file. Okay if I have somehing like:

        //C++

        send_buffer = new double[num_rows];

        Then its equivalent C-code would be:

        send_buffer = (double *)malloc(num_rows * sizeof(double);

        Please let me know. Zulfi.

        D L 2 Replies Last reply
        0
        • Z zak100

          Hi, Thanks for your response. God bless you. Yes, file_buffer is a vector. Thanks for your comments about fegts(). I am reading from a file which has following contents: 22 1111.909 12.3456 0.000345 and so on. First line is a integer and tells the count of data in the file. Okay if I have somehing like:

          //C++

          send_buffer = new double[num_rows];

          Then its equivalent C-code would be:

          send_buffer = (double *)malloc(num_rows * sizeof(double);

          Please let me know. Zulfi.

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

          zak100 wrote:

          Please let me know.

          Looks okay from my vantage point. Have you tried it?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          1 Reply Last reply
          0
          • Z zak100

            Hi, Thanks for your response. God bless you. Yes, file_buffer is a vector. Thanks for your comments about fegts(). I am reading from a file which has following contents: 22 1111.909 12.3456 0.000345 and so on. First line is a integer and tells the count of data in the file. Okay if I have somehing like:

            //C++

            send_buffer = new double[num_rows];

            Then its equivalent C-code would be:

            send_buffer = (double *)malloc(num_rows * sizeof(double);

            Please let me know. Zulfi.

            L Offline
            L Offline
            leon de boer
            wrote on last edited by
            #5

            There is one slight difference which may or may not be important in some situations. C++ new usually zeros the allocated memory In C you can use calloc for that it has the format

            void *calloc(size_t nitems, size_t size)

            So you can actually remove the multiply because you have the number of items and the size of each item

            send_buffer = (double *)calloc(num_rows, sizeof(double));

            Now it allocates and zeros the allocation.

            In vino veritas

            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