Reading from text files in C

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

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

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

    Man i've been seeing your videos from like 5th semester, did a project because of u :) and still watchin !!!

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

    Trujt zoti mrsh vlla

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

    If you want to store the data to a variable and then do some work on the data, how do you do that? for example, your file might look like this:
    36 1 75 60 37 22
    46 79 100 88 24 38
    75 58 23 99 14 8
    65 88 50 38 2 64
    81 57 14 32 46 93
    68 73 8 66 97 44
    Now I might want to read this data line by line and number by number so that I have:
    A=36
    B=1
    C= 75
    D= 60
    E= 37
    F=22
    Now do something with the numbers and then move to the next line and so on until I reach the EOF.
    Now how can I do this in C? In C# it is pretty easy because it has functions/Methods to do these things but in C it looks quite difficult. However, with your expertise you can tell us how this done.
    Please write a blog or create a short video so that we can follow it.
    Thanks for these very helpful videos.
    Kind regards,

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

      Alright, I'll make a short video explaining this

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

      @@CodeVault OK thank you very much indeed.

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

    It might be a REAL GOOD IDEA to post the code

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

      Here you go: code-vault.net/lesson/pt6uwax8pw:1603733523045

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

    Thank you. This video helped me

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

    Can you help me with this please:
    Write a program that describes the work of a buffer between two processes ,one writes in the buffer and other one reads from the buffer. Buffer size is fixed and must not be passed

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

      Ask on our discord server: discord.code-vault.net

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

    Great tutorial! is serialization and the process of bringing in data the same as encapsualtion?

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

      No, encapsulation has to do with the architecture of the software (many individual variables combined create a single data type) whereas serailization is just a process. Basically, in C, this would be the creating of structs that make up some more complex entities from primitive types.
      This is regardless of how you load/save the data from/to disk.

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

      Got it, thank you so much! Very informative, I’ve watched most of your videos and they are packed with quality information in such a short time. Thank you so much! Excited to get better

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

    I always get segmentation fault and returns 1 when you check if(in == NULL). What should I do?

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

      Send in the whole code, a simple check for NULL can't give you seg fault

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

    Is there a way to read just one line from a file without using fgets?

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

      You could just read character by character with fgetc until you find a

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

    why do you have fclose twice? one within the if statement and once just after? if the file is closed then how does it read from it? Thanks

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

      if (fgets(buffer_in, 256, in) == NULL) {
      fclose(in);
      return 1;
      }
      fclose(in);
      Basically it's not closing the file twice, it's only closing once and only after it's done reading. One of them is in the if statement because, if fgets returns NULL that means an error occurred and as you can see, I am returning out of the main function there. Therefore I have to close the file beforehand. If that if is not executed then we also need to close the file since we don't need it anymore because we're done reading already.
      It could be done better. Maybe you can rewriting this piece of code so that fclose appears only once in there. Could be a good exercise ;)

  • @FlavioSilva-wh9kb
    @FlavioSilva-wh9kb 3 ปีที่แล้ว

    THankksssss :D

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

    Tqu

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

    is it possible to pass a variable with the name of the file to the function fopen_s as the second parameter? If so, how to do it?

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

      Yeah, of course it is:
      FILE* out;
      const char* filename = "my_text_file.txt";
      fopen_s(&out, filename, "r");

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

      @@CodeVault what if the file name will be entered from the keyboard (the number of symbols in the file name is undefined)? Is it possible to avoid compiler warning C6054? (Code works fine)

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

      Just make sure your string is NULL-terminated and you shouldn't get that warning

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

    How do you choose which line you want to read at ?

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

      You can't, unless you know the number of characters per line and fseek that number of characters.

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

      @@CodeVault I found a way in the end by saving all the lines one by one and not looking for a specific one but thx

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

    Can u please do the same video but using functions

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

      Using functions? Hmm, I'll look into it

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

    excel files pls?

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

      You could export an excel file to CSV format, then it's easy to read in C

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

      @@CodeVault what do I have to do to read a csv? "fopen "csv"?

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

      No. Just like in the video but change the format string to be a CSV string

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

      @@CodeVault so the code to open would be in what style? Could you write here?