Hi, I'm working on a project, and below is a segment of my code written in C++. However, it's required in C, but I don't know how to write the counterpart of it in C. Also don't know if it's appropriate to ask question about C here, I'll delete it if so.
somewhere along the way C added a getline.
so what the above says, is,
while there is data in the file
get a line
you can do that logic in C, but I am not sure what the correct loop condition is.
the file reading is done like reading from the console, fgets instead of gets or whatever it is. You just have to supply the file* instead of assuming its stdin.
start at the top and work through it, is my advice.
stringstream and string: c does not have, so you nee to mimic what you used from those tools.
Hi, @Handy Andy
Yes, "variable" is a struct. I missed it in my code.
Here it is:
1 2 3 4 5 6 7 8 9 10 11
struct variable
{
// The index of the variable
int index;
// The name of the variable
char name;
// Probability of the variable and its complement
float prob, prob_not;
};
and stream is just being used to parse, which can be done with atoi / atof and various c-string statements. You don't need to re-create it, you just need to do what it did. Same for vector, you don't have to re-create the whole thing
honestly
struct vec
{ type* tp; unsigned int size; unsigned int capacity;}; //this will do 90% of what you need where type is double or int or another struct ... whatever. you can use void * to make it generic if you want to go there, but that is more ugly than useful most of the time. a void* and an enum to tell it what type it is would be reasonable if you hide the void* from everything outside the struct and its immediate 'methods' (which are not like c++ they would just be free functions). You actually can add methods with function pointers, but here again, is it worth it?
^^^ while you can do some other data structure, I can't see how coding up a list is easier than just a single pointer allocation which acts like a vector / array 'naturally'.
^^^ while you can do some other data structure, I can't see how coding up a list is easier than just a single pointer allocation which acts like a vector / array 'naturally'.
did you meant to me? well, then you may need dynamically allocated "auto resizing" array. because input file may not be fixed sized.
edit: its possible without "auto resizing" array. count total object numbers from the input file & create array of that size.
edit: its possible without "auto resizing" array. count total object numbers from the input file & create array of that size.
yes, this. There are always a few edge cases where you can't find out how many for some reason, but C has realloc! Much like a vector, it would be wise to call this sparingly, getting a lot of items every time you run out -- that is why my little vector light struct had a capacity field. So even if you cannot count the input (say it is typed in by user) ... you can manage with a tiny bit of code rather than write a list. And the list idea isn't "bad" (I was not trying to imply this) if you need the functionality, its just a lot more work if its a small program being converted.
Another alternative to possibly simply the code is to process the file twice - first pass determine how many lines/variables there are and then allocate the required memory (calloc). Then a second pass to parse and extract. Its a small file so the performance difference won't be noticeable. This means there's no need to code a list/vector replacement or bother with realloc() et al.