How do I convert C++ statements to C?

  • C/C++
  • Thread starter zak100
  • Start date
  • Tags
    C++
In summary: You can't resize an array in C; you have to know how many elements you need it to have before you declare it.
  • #1
zak100
462
11
I have following C++ statements:

C++:
     ifstream inFile;

    inFile >> num_rows;

    file_buffer.resize(num_rows);

I have converted them into C:

C:
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 :

C++:
file_buffer.resize(num_rows);

in C language.

Similalry, for C++ statements:

C++:
send_buffer = new double[num_rows];

is this the correct conversion?

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

Some body please guide me.

Zulfi.
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Hi Zulfi, I had to adjust your post to make it readable. There was a table in it that was badly formatted and so I removed it. I think now it was your input.

Are you reading a line from a text file like the one below with 1, 2, 3?

1 2 3
 
  • #3
A couple of things

Is num_rows an int?

As the cpp code looks to be reading a binary number.

Then the fgets would be wrong as it reads strings.

Basically, let’s start with the input file, what does it look like?

fopen, fread, and fclose for binary vs fopen, fgets, fclose for text.
 
  • #4
Hi,
Thanks for your response. Its a file containing double values.
1st line contains the number of rows in the file which is an integer value.
Then there are double values in the file like:
1234.67
0.78904
8987667.34

and so on.Zulfi.
 
  • #6
No sorry. I don't need this. Please provide me specific answer otherwise it would be a wastage of my time and also of this great facility (i.e www.physics.forums.com)

Zulfi
 
  • #7
Replace the struct in the example with an int variable to get number of rows, construct a for loop to iterate that number of times, and now you’d be able to fread one double at a time which you can store in a array of doubles.
 
  • #8
Hi,
My friend thanks for your response. God bless you. I am not using "struct" anywhere in my code. Please provide something specific to my code.

Zulfi.
 
  • #9
zak100 said:
I can't understand how to declare file_buffer

The easiest C data type to use would be an array of floats. This tutorial deals with how to declare arrays (it gives examples of arrays of ints and chars but it's easy enough to adapt them to floats):

https://www.cprogramming.com/tutorial/c/lesson8.html
You will also need an int to store the number of items (since you're reading that from the file also).

zak100 said:
how to convert

You can't resize an array in C; you have to know how many elements you need it to have before you declare it.

zak100 said:
is this the correct conversion?

You could do it this way, but why do you need to allocate on the heap? Why not just declare another array using the same method as the tutorial above?
 
  • #10
jedishrfu said:
Then look for c fread examples.
I would recommend fscanf rather than fread, as it would be somewhat easier to use. Although fread can be used on text files, it is more useful in reading binary files, which doesn't seem to be the case here.

@zak100 , the basic algorithm would be to read the first (int) value, and then set up a for loop to read that many double values. Since you apparently need to store the double values, you need to dynamically allocate memory from the heap, using malloc() to do this.
 
  • Like
Likes jedishrfu
  • #11
Mark44 said:
you need to dynamically allocate memory from the heap

AFAIK, an array declaration in C within a function can have a size that is contained in a variable whose value is only known at run time; so I think the array could be allocated on the stack using a simple array declaration (using the int variable whose value was read from the first line of the file), instead of having to use malloc.
 
  • #12
PeterDonis said:
AFAIK, an array declaration in C within a function can have a size that is contained in a variable whose value is only known at run time; so I think the array could be allocated on the stack using a simple array declaration (using the int variable whose value was read from the first line of the file), instead of having to use malloc.
I don't believe this is true. The size of an array in C must be known at compiletime, whereas the value of a variable (int or otherwise) isn't known at compile time.
IOW, you can't do this:
C:
int size;
// Prompt user for size of array
double arr[size];

The above isn't what you were talking about, but is germane to the discussion. If you have an array parameter in a function, it's isn't really an array -- it's a pointer to some memory. If your function starts filling in that memory, doing so will likely clobber something important or cause a seg fault or the like. Am I correctly understanding what you're saying? I don't see any way other than using malloc() to get space on the heap for the OP's question.
 
  • #14
You are all right. The C99 standard supports variable length arrays but not all compilers support C99 and not all C99 compilers support VLA. Thus C11 made it an optional feature of the language. So the "correct" answer can be no, yes or maybe...
 
  • Like
Likes jedishrfu
  • #15
Mark44 said:
The above isn't what you were talking about

Yes, it was. If what you showed there is not allowed, then you're right, the only way to allocate space for the array of doubles is on the heap.
 

FAQ: How do I convert C++ statements to C?

1. How do I convert C++ statements to C?

To convert C++ statements to C, you will need to make a few changes to your code. First, you will need to remove any C++ specific syntax such as "using namespace" and "cout/cin" statements. Then, you will need to replace any C++ data types such as "string" with their C equivalents (e.g. "char arrays"). Additionally, C does not support classes and objects, so you will need to rewrite your code using functions and structures instead.

2. Do I need to make any changes to my C++ code before converting it to C?

Yes, you will need to make some changes to your C++ code before converting it to C. As mentioned in the previous answer, you will need to remove any C++ specific syntax and replace C++ data types with their C equivalents. You may also need to make changes to your code logic to work with C's different syntax and limitations.

3. Is it possible to automatically convert C++ code to C?

There are some tools available that claim to convert C++ code to C automatically, but they are not always reliable. It is recommended to manually convert the code to ensure accuracy and efficiency.

4. Can I use C libraries in my converted C++ code?

Yes, you can still use C libraries in your converted C++ code. However, you may need to make some changes to your code to work with C's syntax and limitations.

5. Are there any benefits to converting C++ code to C?

Converting C++ code to C can have some benefits, such as improved performance and compatibility with older systems. However, it also means sacrificing some of the advanced features and flexibility of C++.

Similar threads

Replies
23
Views
7K
Replies
2
Views
4K
Replies
4
Views
3K
Replies
16
Views
2K
Replies
8
Views
6K
Replies
6
Views
2K
Replies
1
Views
2K
Back
Top