Help to determine vector/matrix size - C language

Asked

Viewed 431 times

2

What I’d like to do is put into a vector or matrix, information from a file txt no prior knowledge of the number of "inputs" to know the size of the vector or matrix.

With the size of the vector or matrix, I can invoke the function malloc and then read the contents of the file.

Could someone advise me the best way to do it? Thank you.

  • Good afternoon John, sorry I may not know the term entradas in C (if it is a term), what do you really mean by entries? You are getting referendum to I/O?

  • Good afternoon, I’m still a bit of a beginner, maybe I haven’t referred correctly, what I meant as input is some information contained in the txt file that goes up to n, and after that would be another entry. Example of what I mean: in the file would have: name1 (space) age1 (space) Nome2 (space)... each name and age would be an entry, the name1 would be the first entry, the idea1 would be the second and so on...

  • Perhaps it would be better for me to have said in general what I want to do and asked how to do it. I want to put in a vector or matrix the contents of a file, but do not know the "size" of the file to "by the vector size"

  • 1

    @John, welcome to [en.so], you can [Dit] your question and put these details you commented on it, then you can browse the [tour] and learn a little more about Sopt

  • See if this helping.

  • Thank you, you helped me out.

  • Would it not be better to use a dynamic chained list rather than a vector/matrix to store the data? So you don’t have to worry about the number of entries.

Show 2 more comments

2 answers

1

To know the file size, you can go to the end of the file and then get the position:

fseek(fp, 0L, SEEK_END);
sz = ftell(fp);

And then, you can go back to the beginning of the file:

fseek(fp, 0L, SEEK_SET);

Source: https://stackoverflow.com/a/238607/540552

  • Remembering that this method is especially useful in the scenario where all inputs have fixed size. Otherwise you would not have been able to calculate the number of vector positions, but rather the amount of memory you would need to allocate to put all the file contents in memory.

0

If the entries have a defined and unique size, all you have to do is take the file size and divide by the size of the input. The result is the number of entries.

If the size of the entries varies, things get a little more difficult, but you can try:

  • Use a linked list or a tree or some other type of data structure;

  • Make an estimate that is not less than the number of entries and leave some positions of the array at the end if you have overestimated the number;

  • Scroll through the entire file just to know what the number of entries is and then go through it again to fill the array.

  • Thank you, the size of the entries varies. But I don’t think I could explain myself in my question, so let me try to improve.. i would like to put in a vector or array the contents of a file, but do not know the "size" of the file to "by the vector size". You could advise me the best way to accomplish this?

  • @John My other answer helps you?

  • Thank you, but that’s not quite what was in mind, the fseek does not in fact give the amount of entries as I wanted, but I believe the value of bytes, or am I mistaken ? and I still haven’t gotten to that part of my learning to make good use of that function.. Still, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.