Created
June 16, 2022 20:36
-
-
Save eduardo-cess/597ee2ccbce3bc810dd6fd04c93eafe3 to your computer and use it in GitHub Desktop.
Function in C to get text from file and put in a variable
This file contains hidden or 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> | |
char text[100]; | |
void getTextFromFile(char *filename) { | |
FILE *fp = fopen(filename, "r"); | |
if (fp == NULL) | |
{ | |
printf("Error: could not open file %s", filename); | |
} | |
char ch; | |
int i = 0; | |
while ((ch = fgetc(fp)) != EOF){ | |
text[i] = ch; | |
i++; | |
} | |
fclose(fp); | |
} | |
int main() | |
{ | |
getTextFromFile("teste2.txt"); | |
printf(text); | |
getTextFromFile("redme.txt"); | |
printf(text); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you!