Conversion from- C++ to C-language
-
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.
-
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.
Is
file_buffer
avector
? 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 subsequentfgets()
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
-
Is
file_buffer
avector
? 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 subsequentfgets()
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
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.
-
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.
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
-
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.
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