Read an array of structs in C

Поділитися
Вставка
  • Опубліковано 30 вер 2024
  • Check out our Discord server: / discord
    Documentation about the fgets function: www.cplusplus.c...

КОМЕНТАРІ • 76

  • @Sword001SK
    @Sword001SK 3 роки тому +5

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

  • @zaabimahdi
    @zaabimahdi 4 роки тому +1

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

  • @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.

  • @tynanmcgrady2624
    @tynanmcgrady2624 4 роки тому +3

    brilliant explanation , thank you!

  • @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

  • @akhilpandey1539
    @akhilpandey1539 8 місяців тому

    Don't stop making such videos please

  • @abdurezzaq758
    @abdurezzaq758 2 роки тому +1

    Thank you for this amaizing explanation!❤

  • @oliverondrus1470
    @oliverondrus1470 2 роки тому

    You just saved my ass right here , Thank youuu.

  • @diogenes_of_sinope
    @diogenes_of_sinope 11 місяців тому

    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.

  • @anjalibhatt56
    @anjalibhatt56 4 роки тому +1

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

  • @othmaneer-rouhly8121
    @othmaneer-rouhly8121 2 роки тому

    Thank you so much legend

  • @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!

  • @gabrielcalper6295
    @gabrielcalper6295 4 роки тому

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

  • @naboulsikhalid7763
    @naboulsikhalid7763 7 місяців тому

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

  • @mongraal2272
    @mongraal2272 2 роки тому

    thank u bro

  • @ignabelitzky
    @ignabelitzky 4 роки тому

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

  • @maedesolgi8251
    @maedesolgi8251 2 роки тому

    Thanks

  • @hey-its-me239
    @hey-its-me239 2 роки тому

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

  • @yourlocalsuedi
    @yourlocalsuedi 3 роки тому

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

  • @yusefalimam130
    @yusefalimam130 3 роки тому

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

  • @armandolopezespinoza3823
    @armandolopezespinoza3823 4 роки тому

    Read json array of object deserialize on struct, please

  • @davidsampson94
    @davidsampson94 4 роки тому

    love these vids please continue very good

  • @admirmehmedovic4308
    @admirmehmedovic4308 2 роки тому

    This helped me a lot, thanks;

  • @alexbravo7467
    @alexbravo7467 3 роки тому

    yes, thank you, buddyboy ...

  • @naruz5262
    @naruz5262 3 роки тому

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

    • @CodeVault
      @CodeVault  3 роки тому

      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

  • @relaxingweathersoundsrainy1138
    @relaxingweathersoundsrainy1138 4 роки тому

    you are the best 🤗🤗🤗

  • @taherrezzag9848
    @taherrezzag9848 4 роки тому

    thanks for help

  • @TehRespawner
    @TehRespawner 2 роки тому

    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  2 роки тому +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

  • @stickshift583
    @stickshift583 3 роки тому

    Thank you!

  • @armandolopezespinoza3823
    @armandolopezespinoza3823 4 роки тому

    Le di la campanita

  • @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!

  • @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

  • @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

  • @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.

  • @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 :)))

  • @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

  • @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

  • @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: ua-cam.com/play/PLfqABt5AS4FmXeWuuNDS3XGENJO1VYGxl.html

  • @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

  • @kibisovnikita72
    @kibisovnikita72 3 роки тому

    When is the next video?

  • @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.

  • @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?