Thread: fgets instead of gets

  1. #1
    Registered User
    Join Date
    May 2010
    Location
    Russia
    Posts
    4

    Question fgets instead of gets

    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

  2. #2
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    I'm wondering how many times this question arose around.

    Привет, бро, кстати.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Russia
    Posts
    4
    Hello Brother!

    I know this question is eternal. And I'm another one who doesn't know the answer

    А по существу есть что сказать?

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Johaneson View Post
    А по существу есть что сказать?
    He may not, but I do. USE ENGLISH, THIS AIN'T NO RUSSIAN BOARD!!!

  5. #5
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    Johaneson
    I already gave you a qouted link about your issue. What else do you want really?

    >THIS AIN'T NO RUSSIAN BOARD!!!
    Sorry
    Last edited by GL.Sam; 05-12-2010 at 01:23 PM.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Russia
    Posts
    4
    Quote Originally Posted by Kennedy View Post
    USE ENGLISH, THIS AIN'T NO RUSSIAN BOARD!!!
    Sorry! I'll try to use ENGLISH.

    GL.Sam, thank you. I've got all I need.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Russia
    Posts
    4
    I've found out the answer. Thanks to Cprogramming.com FAQ. The thread may be deleted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM