C/++/# Converting from C++ to C

  • Thread starter zak100
  • Start date
375
8
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:
10,334
3,865
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
 
10,334
3,865
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.
 
375
8
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.
 
10,334
3,865
Then look for c fread examples. I’ll try to find one to post here.

 
375
8
No sorry. I dont 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
 
10,334
3,865
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.
 
375
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.
 
24,303
5,989
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):


You will also need an int to store the number of items (since you're reading that from the file also).

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.

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?
 
32,344
4,130
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.
 
24,303
5,989
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.
 
32,344
4,130
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.
 
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...
 
24,303
5,989
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.
 

Want to reply to this thread?

"Converting from C++ to C" You must log in or register to reply here.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top