-
April 24th, 2019, 05:53 PM
#1
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.
-
April 25th, 2019, 01:29 AM
#2
Re: Converting from C++ to C language
Originally Posted by Zulfi Khan2
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
-
April 25th, 2019, 03:27 AM
#3
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++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|