Thread: what's the problem with this code?

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Question what's the problem with this code?

    It is supposed to write to a file..........
    Code:
    #include<stdio.h>
    int main()
    	{
    	FILE *fp;
    	char string[50];
    	fp=fopen("C:\\test.txt","w");
    	printf("\n\nEnter a few lines\n\n");
    	while(strlen(gets(string))>0)
    		{
    		fprintf(fp,string);
    		fprintf(fp,"\n");
    		}
    	getch();
    	}
    

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The problems are as follows:

    • You're not checking to see if fp is NULL before you use it.
    • You use gets().
    • You use getch().
    • Even with using getch(), you don't include conio.h.


    Overall, the code sucks.

  3. #3
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    fprintf(fp, "%s",string);
    fprintf(fp, "%s","\n");


    example

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Use fgets instead of gets and check for error on fgets return.
    You should also explicitly return 0 at the end of main.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    And I guess u are using one of these ancient 16bit turbo C compiler. And hence you are coming up with just a solution which is no more accpetable under ISO C standard. Use GCC.

    If you are on windows use DEV-C++.

    ssharish

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or Visual Studio 2008 Express
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM