Created
December 2, 2019 07:04
-
-
Save yongjun823/268597746a34d0c72d9f56144007fa46 to your computer and use it in GitHub Desktop.
c lang file read & save string in char* array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
C언어 파일 읽어서 배열에 문자열 저장해서 함수로 리턴