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.
c lang file read & save string in char* array
#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]);
}
}
@yongjun823
Copy link
Author

C언어 파일 읽어서 배열에 문자열 저장해서 함수로 리턴

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment