Skip to content

Instantly share code, notes, and snippets.

@eduardo-cess
Created June 16, 2022 20:36
Show Gist options
  • Save eduardo-cess/597ee2ccbce3bc810dd6fd04c93eafe3 to your computer and use it in GitHub Desktop.
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
#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;
}
@SuanneBarbosa
Copy link

thank you!

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