Skip to content

Instantly share code, notes, and snippets.

@yongjun823
Created December 2, 2019 07:04
Show Gist options
  • Save yongjun823/268597746a34d0c72d9f56144007fa46 to your computer and use it in GitHub Desktop.
Save yongjun823/268597746a34d0c72d9f56144007fa46 to your computer and use it in GitHub Desktop.

Revisions

  1. yongjun823 revised this gist Dec 2, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion simple_cat.c
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ void read(char** sample, char* file_name)
    char buffer[256] = {0};
    fgets(buffer, 256, fp);

    sample[i] = (char*) malloc(sizeof(char) * strlen(buffer) + 1);
    sample[i] = (char*)malloc(sizeof(char) * strlen(buffer) + 1);
    strcpy(sample[i], buffer);
    i++;
    }
  2. yongjun823 created this gist Dec 2, 2019.
    34 changes: 34 additions & 0 deletions simple_cat.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    void read(char** sample, char* file_name)
    {
    FILE* fp;
    int i = 0;

    fp = fopen(file_name, "r");

    while(feof(fp) == 0)
    {
    char buffer[256] = {0};
    fgets(buffer, 256, fp);

    sample[i] = (char*) malloc(sizeof(char) * strlen(buffer) + 1);
    strcpy(sample[i], buffer);
    i++;
    }

    fclose(fp);
    }

    int main(int argc, char* argv[])
    {
    char* sample[100] = {0};
    read(sample, argv[1]);

    for(int i=0; sample[i] != NULL; i++)
    {
    printf("%s", sample[i]);
    }
    }