Here is the solution:
#include <stdio.h>
#include <string.h>
int read() {
char text[50][30],buffer[150];
int i=0;
FILE *file_in;
file_in=fopen("test.txt","r");
if (file_in == NULL) {
printf("Error opening file\n");
}
while (fgets(buffer,150,file_in)) {
strcpy(text[i],buffer);
printf("line %d: %s\n",i,text[i]);
i++;
}
// getchar();why you were using this?
fclose(file_in);
// return 0;
}
void write(char str[])
{
FILE *file_in;
file_in=fopen("test.txt","a");
if (file_in == NULL) {
printf("Error opening file\n");
}
//write to the file
fprintf(file_in, "\n%s", str);
// fputs(str, file_in);
fclose(file_in);
}
int main()
{
char msg[50];
read();
printf("Enter some text: ");
// getchar();
gets(msg);//It reads in msg
write(msg);
return 0;
}