Dear friends,
I'm a newbie in C programming and have a lot of questions. Could you be so kind as to answer on one of them? The question is: how to use fgets instead of gets? Let me briefly outline the problem:
User creates a file named disp_yag.txt for example, fill it with experimental data, saves onto a disk and runs my program. The program asks him to put (from a keyboard) file name and then opens this file for reading. I've written some code for testing
Code:
#include<stdio.h>
#include<stdlib.h>
#define SLEN 50
int main(void)
{
char file[SLEN];
float lambda;
FILE *fp;
printf("This function I believe will read data from a text file\n");
puts("Введите имя файла для обработки:");
gets(file);
if ((fp = fopen(file, "r")) == NULL)
{
fprintf(stderr, "Программа не может открыть файл %s\n", file);
exit(1);
}
fscanf(fp, "%f", &lambda);
printf("Длина волны %f", lambda);
if (fclose(fp) !=0)
fprintf(stderr, "Failed to close the file\n");
return(0);
}
It works but I always receive compiler's warning message
Building target: data_read
Invoking: GCC C Linker
gcc -o"data_read" ./func1.o
Finished building target: data_read
./func1.o: In function `main':
/home/johaneson/workspace/data_read/Debug/../func1.c:13: warning: the `gets' function is dangerous and should not be used.
Please direct me to the good books or papers where I can read how to solve this problem.
BR
J