Read an array of structs in C

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 พ.ย. 2024

ความคิดเห็น • 76

  • @Sword001SK
    @Sword001SK 4 ปีที่แล้ว +5

    I am so glad that I found you. Thanks for making these C lecturing videos!

  • @naboulsikhalid7763
    @naboulsikhalid7763 9 หลายเดือนก่อน

    as usual, you explained with your heart, so all you demonstrated stayed vivid in my mind. thank you

  • @diogenes_of_sinope
    @diogenes_of_sinope ปีที่แล้ว

    Thank you very much for your efforts and sharing the knowledge, you are a good person and you will be rewarded for the enlightenment of people.

  • @Jorgeee
    @Jorgeee 4 ปีที่แล้ว +2

    Hey man, will you ever upload c++ tutorials? You explain everything so amazingly it's very easy to follow. You would make the c++ syntax so much easier to understand

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว +6

      Possibly, if enough people want me to. For now, I've got a lot planned for C programming tutorials and other computer science related topics which I found many people lack knowledge in.

  • @akhilpandey1539
    @akhilpandey1539 10 หลายเดือนก่อน

    Don't stop making such videos please

  • @zaabimahdi
    @zaabimahdi 4 ปีที่แล้ว +1

    @CodeVault! Hey where are you ! C community miss your videos on youtube !

  • @relaxingweathersoundsrainy1138
    @relaxingweathersoundsrainy1138 4 ปีที่แล้ว

    There are five data (plain text) files, namely, the “students.txt”, “courses.txt”, “registrations.txt”, “criteria.txt”, and “performances.txt” that keep information. Student file keeps the information about students such as the student number, name, surname, and department in abbreviated form. Course file keeps the information about courses such as the course code and course title. Criteria file keeps the information about course assessments such as the course code, course credit value, homework, lab, quiz, midterm, and final percentages. Registration file keeps the information about the student’s registrations such as the student number, academic year, semester, and course code. Performance file keeps the information about the student’s performances such as the student number, course code, and performances.
    The program should read all given files and should prepare transcripts for each student
    i am managed to read all files into structures but i am failing to match and collect information for each student
    @CodeVault please help

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว

      Head on over to our discord server (link in the description) and you can ask there in more details what your issue is.

    • @relaxingweathersoundsrainy1138
      @relaxingweathersoundsrainy1138 4 ปีที่แล้ว

      @@CodeVault ok thanks man

  • @anjalibhatt56
    @anjalibhatt56 4 ปีที่แล้ว +1

    I'll start this series once I'm done with basic... Thank you 👌

    • @chillydoog
      @chillydoog 4 ปีที่แล้ว

      Ok. Sounds good!

  • @tynanmcgrady2624
    @tynanmcgrady2624 4 ปีที่แล้ว +3

    brilliant explanation , thank you!

  • @abdurezzaq758
    @abdurezzaq758 2 ปีที่แล้ว +1

    Thank you for this amaizing explanation!❤

  • @ignabelitzky
    @ignabelitzky 4 ปีที่แล้ว

    Hey man... your vids are amazing!!! You deserve more subscribers!!!

  • @mika1758
    @mika1758 3 ปีที่แล้ว +1

    how can we do this for csv (excel) files

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +2

      Just change the format string to output CSV:
      sscanf(buffer, "%d,%d,%d", &p->x, &p->y, &p->val);

    • @mika1758
      @mika1758 3 ปีที่แล้ว

      @@CodeVault Thank you

  • @TehRespawner
    @TehRespawner 3 ปีที่แล้ว

    thanks for the tutorial! but im having trouble understanding how the information gets stored in the variable 'points'. i dont see it being assigned other than the *p = points + i, but this is assigning p? .. sorry if my question is stupid, but im a beginner programmer so its a little bit confusing :)

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +1

      It's this line that is setting things in the points array:
      sscanf(buffer, "%d %d %d", &p->x, &p->y, &p->val);
      Since
      *p = points + i;
      We know that p points to some element in the points array.
      Then, in that sscanf line we do:
      &p->x
      This is a two step process:
      p->x means "get whatever is at the address p is pointing to (which is an element in that array) and get the x member of that element"
      &p->x means "get the address of the result of p->x (from above)"
      Thus &p->x gives us an address to the x member inside of an element in that points array.
      Same logic for the rest of the parameters in that sscanf line

  • @alexbravo7467
    @alexbravo7467 4 ปีที่แล้ว

    yes, thank you, buddyboy ...

  • @hey-its-me239
    @hey-its-me239 2 ปีที่แล้ว

    THANK YOU SO MUCH FOR SAVING MY LIFE AND PROJECT! IT WORKED YEAHHH❤

  • @starfart69
    @starfart69 3 ปีที่แล้ว

    Around @8:30, line 22, why would you rather use another struct pointer p instead of just using the struct in line 19 which is points[100]? From what I understand you modified the values on the same address anyway.

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว

      Doing this:
      Point p = points[i];
      sscanf(buffer, "%d %d %d", &p->x, &p->y, &p->val);
      Would NOT change the array as p would simply be a copy of "points[i]".
      But you could use points[i] directly if you don't like that pointer.
      Instead of
      sscanf(buffer, "%d %d %d", &p->x, &p->y, &p->val);
      it could be done with:
      sscanf(buffer, "%d %d %d", &points[i]->x, &points[i]->y, &points[i]->val);
      The pointer was just used for shortening the code.

    • @starfart69
      @starfart69 3 ปีที่แล้ว

      @@CodeVault thank you!

  • @yusefalimam130
    @yusefalimam130 4 ปีที่แล้ว

    excellent presentation, ill be checking this channel out for sure!!!

  • @afsanehakhbari8880
    @afsanehakhbari8880 3 ปีที่แล้ว

    thank you for perfect explanation would you please describe how we can pass struct and struct arrays to the pthread_creat

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +1

      I will make a video on that. But it's really the same way as you'd pass an integer. Usually dynamically allocated so that it doesn't get overriden:
      struct Example e = malloc(sizeof(struct Example));
      e->x = 1;
      e->y = 5;
      pthread_create(&th, NULL, &routine, e);

    • @afsanehakhbari8880
      @afsanehakhbari8880 3 ปีที่แล้ว

      @@CodeVault thank you so much for the answer. Is there any platform for asking questions? because the way you introduce processes and threads are perfect and easy understandable.

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +1

      Yes! I answer questions regularly on our Discord server: discord.code-vault.net
      There will soon be a similar feature on our website as well: code-vault.net

  • @Zahra-qb7nm
    @Zahra-qb7nm 3 ปีที่แล้ว

    Hi thank you for your videos!
    Got a question though, in my program i’ve array of struct, struct User* user;
    And the elements are lets say
    username[200];
    password[200];
    But when i’m passing them to int functions, from one function to another, the data that has been saved before seems to vanish
    For example in a function i strcpy(user[i].username,”bluish”); and it saves successfully but when i pass it to another function it writes for that.
    Can tell where its problem?
    And in the function that im saving the username it’s like Signup(&user, i) so that it saves but in the other function it’s just (user,i) as arguements

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว

      How are you accessing that data?

    • @Zahra-qb7nm
      @Zahra-qb7nm 3 ปีที่แล้ว

      @@CodeVault ah thank you! It got sorted, the access was also through watch window, thank you for the video it helped me alot :)))

  • @ib1664
    @ib1664 4 ปีที่แล้ว

    How do you delete a line of data in a serialized record

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว

      The easiest and foolproof way is to just read everything into an array, delete the element and write everything back into it

  • @anupkodlekere8604
    @anupkodlekere8604 4 ปีที่แล้ว +1

    what shortcut did you use at 7:45 to select all the dots?

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว +3

      I pressed CTRL+D to create a new cursor at the next occurance of the same selection

    • @aibou2399
      @aibou2399 3 ปีที่แล้ว

      @@CodeVault nice!

  • @asifsaad5827
    @asifsaad5827 4 ปีที่แล้ว

    same thing happens when you do that with class type?

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว +2

      Classes don't exist in C. But, sure, you can use the same technique with classes in C++. But you can use streams there.

  • @oliverondrus1470
    @oliverondrus1470 3 ปีที่แล้ว

    You just saved my ass right here , Thank youuu.

  • @davidsampson94
    @davidsampson94 4 ปีที่แล้ว

    love these vids please continue very good

  • @MKSundaram
    @MKSundaram 3 ปีที่แล้ว

    Yes, one of the best. How do I store lines from files into an array? I tried few things but failed. My file contains "test line 1 in the first line and in next line test line 2 and so on.... there is a total of four lines. One of the solutions was declaring a 2D array and store the four lines and this made me more confused, could you please help me? thanks :-)

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว +1

      Yea, basically a 2D array or an array of strings should be good enough. You can simply call in a while loop.
      char strArray[100][100];
      while (fgets(strArray[i], 100, file) != NULL) {
      i++;
      }
      Something like this... also, make sure your file has a new line at the end so that the while loop doesn't stop prematurely.

    • @MKSundaram
      @MKSundaram 3 ปีที่แล้ว

      @@CodeVault Thanks a lot

  • @armandolopezespinoza3823
    @armandolopezespinoza3823 4 ปีที่แล้ว

    Read json array of object deserialize on struct, please

  • @bharatnarayana8401
    @bharatnarayana8401 3 ปีที่แล้ว

    {"sam","0004","SDE1","VLSI"}
    what will be the de serialization format i.e to read this type of forat
    const char* PERSON_FORMAT_IN=....?

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว

      Here you can't really use %s, but you can specify that you only want a string with alphanumerics using the %[A-Za-z0-9], then just use that in the combination with the other characters found in that string.

    • @bharatnarayana8401
      @bharatnarayana8401 3 ปีที่แล้ว

      @@CodeVault {\"%[^\"]\",\"%[^\"]\",\"%[^\"]\",\"%[^\"]\"} this is not working
      {"%[A-Za-z0-9]","%[A-Za-z0-9]","%[A-Za-z0-9]","%[A-Za-z0-9]"} this works..?

    • @CodeVault
      @CodeVault  3 ปีที่แล้ว

      @@bharatnarayana8401 Yep. You just need to not forget about the \ before each "
      Sorry for the late reply

  • @kibisovnikita72
    @kibisovnikita72 4 ปีที่แล้ว

    When is the next video?

  • @sagarbasnet6088
    @sagarbasnet6088 4 ปีที่แล้ว

    please make video about List in C.

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว

      Here's a playlist for linked lists in C, hope this helps: th-cam.com/play/PLfqABt5AS4FmXeWuuNDS3XGENJO1VYGxl.html

  • @admirmehmedovic4308
    @admirmehmedovic4308 2 ปีที่แล้ว

    This helped me a lot, thanks;

  • @othmaneer-rouhly8121
    @othmaneer-rouhly8121 2 ปีที่แล้ว

    Thank you so much legend

  • @yourlocalsuedi
    @yourlocalsuedi 4 ปีที่แล้ว

    Dude you saved my mf life! Greetings from Sweden! 🇸🇪

  • @taherrezzag9848
    @taherrezzag9848 4 ปีที่แล้ว

    but why you put fgets() out of while loop

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว

      It's so that you always call fgets right before checking if you're at the end of file.
      You could also call it inside but check it twice:
      while (!feof(file)) {
      fgets(buffer, 200, file);
      if (feof(file)) {
      break;
      }
      }

    • @asifsaad5827
      @asifsaad5827 4 ปีที่แล้ว

      @@CodeVault why did you write an If statement inside while loop?
      when the file reaches the end of it, then will simply be out of the loop due to the condition set by the while loop

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว +1

      This is assuming that you also process this data in some way. Like this:
      while (!feof(file)) {
      fgets(buffer, 200, file);
      if (feof(file)) {
      break;
      }
      // Processing goes here
      sscanf(buffer, ...);
      }
      If we don't add that if statement, fgets wouldn't read anything and that would prompt sscanf to process the previous value that was in buffer thus duplicating the value (similarly to how I showed in the video)

    • @asifsaad5827
      @asifsaad5827 4 ปีที่แล้ว

      @@CodeVault what if there is only one line of input in txt file?
      then what ?
      it won't enter the while loop ??
      right?

  • @asifsaad5827
    @asifsaad5827 4 ปีที่แล้ว

    btw why did you write Point *p=points +i?
    can't we do like this Point *p=points[i] ?

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว +2

      No.
      points[i] is equivalent to *(points + i) which returns type Point
      Which means it would dereference that point. But we want a pointer to it, not the actual value since we want to modify the contents of that array.
      &points[i] would be equivalent to points + i which returns the type Point*

    • @asifsaad5827
      @asifsaad5827 4 ปีที่แล้ว

      @@CodeVault why did you use a pointer to insert elements into an array?
      can't you simply put the values of x,y and val (during every loop) in the array named "points"?

    • @asifsaad5827
      @asifsaad5827 4 ปีที่แล้ว

      @@CodeVault like this
      int i=0;
      while(!foef(file))
      {
      sscanf(buffer,"%d %d %d",&points[i].x,&points[i].y,&points[i].val);
      i++;
      //rest are as usual
      }

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว +1

      Yep, you can. It was just shorter to write that way. I prefer to have a pointer to the iterated element in the array rather than repeating points[i] everywhere.

  • @stickshift583
    @stickshift583 3 ปีที่แล้ว

    Thank you!

  • @maedesolgi8251
    @maedesolgi8251 2 ปีที่แล้ว

    Thanks

  • @mongraal2272
    @mongraal2272 2 ปีที่แล้ว

    thank u bro

  • @gabrielcalper6295
    @gabrielcalper6295 4 ปีที่แล้ว

    I'm Brazilian(not English speaker) learning this in English is easier than my teacher lecturing hahahahahahhhaha

  • @relaxingweathersoundsrainy1138
    @relaxingweathersoundsrainy1138 4 ปีที่แล้ว

    you are the best 🤗🤗🤗

  • @naruz5262
    @naruz5262 4 ปีที่แล้ว

    I love your videos , please consider making a patreon , i would love to contribute to make this channel bigger ...

    • @CodeVault
      @CodeVault  4 ปีที่แล้ว

      Thank you! We just launched the new website and it now has a shop where you can buy educational materials. If you find something useful there feel free to get as all the money goes into supporting this channel and community: code-vault.net/shop

  • @taherrezzag9848
    @taherrezzag9848 4 ปีที่แล้ว

    thanks for help

  • @armandolopezespinoza3823
    @armandolopezespinoza3823 4 ปีที่แล้ว

    Le di la campanita