dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Converting from C++ to C language

  1. #1
    Join Date
    Jun 1999
    Posts
    505

    Converting from C++ to C language

    Hi,

    I want to convert from following C++ statements to C- language:

    Code:
     
    ifstream inFile;
    inFile >> num_rows;
    file_buffer.resize(num_rows);
    I have written the following code:
    Code:
         
         FILE* inFile;
        inFile = fopen(argv[1], "r");
        fgets(strNum_rows, 20, inFile);
        num_rows = atoi(strNum_rows);
    Is the above correct conversion?

    However, I can't convert

    Code:
    file_buffer.resize(num_rows);
    Some body please guide me how to convert the above code in C language.

    file_buffer has following declarations:

    Code:
         vector<double> file_buffer;
    Some body please guide me.

    Zulfi.

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,562

    Re: Converting from C++ to C language

    Quote Originally Posted by Zulfi Khan2 View Post
    Some body please guide me how to convert the above code in C language.

    file_buffer has following declarations:

    Code:
         vector<double> file_buffer;
    Some body please guide me.

    Zulfi.
    instead of
    Code:
         vector<double> file_buffer;
    you should use the array of doubles.
    To resize it - use malloc, realloc, free functions.
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,920

    Re: Converting from C++ to C language

    Current version of the C standard allow the size of an array to be set at run-time instead of only at compile time (C++ only allows at compile time). So for C compilers that offer this option, you can do:

    Code:
    double file_buffer[num_rows];
    If your C compiler doesn't allow this, then you'll need to use dynamic memory as suggested by Victor in his post #2.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)