Thread: Why doesn't it work?

  1. #1
    Registered User fenikkusu's Avatar
    Join Date
    Jan 2009
    Posts
    12

    Why doesn't it work?

    I tried:
    - to input data (some full names-string) into an array pointer then write these names into a file >> ok
    - to delete all data in the array (change all string to '\0') >> ok
    The problem is that when I want to restore the data of the array by copying from file (fgets function) something go wrong. Everything printed is just some smiling stupid faces..... Help me check this, pliz

    Code:
    #include<stdio.h>
    #include<conio.h>
    int input_n(void);
    void input_s(int n,char *str[]);
    void clear_enter(int n,char str[]);
    int main(void)
    {
        int n;
        int i;
    
        char name[20];
        char *string[20];
        FILE *fp;
        n=input_n();
        input_s(n,string);
        for(i=0;i<n;i++)
         string[i]=(char*)malloc(60);
        printf("Store the names in a file named:\n");
        fflush(stdin);
        gets(name);
    
        //Store all names in a file:
        if((fp=fopen(name,"w"))== NULL)
         {
          printf("Error opening\n");
          exit(1);
         }
        for(i=0;i<n;i++)
         fprintf(fp,"%s\n",string[i]);
         
        if(fclose(fp)!=0)
         {
          printf("Error closing\n");
          exit(1);
         }
    
        //Delete all data in the array:
        printf("After clearing the array\n");
        for(i=0;i<n;i++)
         {
          fflush(stdin);
          strcpy(string[i],"\0");
          printf("%s\n",string[i]);
         }
        
        //Restore the name list from file:
        printf("Restore the name list from file. The list is: \n");
        if((fp=fopen(name,"r"))==NULL)
         {
          printf("error opening\n");
          exit(1);
         }
         for(i=0;i<n;i++)
          {
           fflush(stdin);
           fgets(string[i],80,fp);
           clear_enter(n,string[i]);
           printf("%s\n ",string[i]);
          }
         
        if(fclose(fp)!=0)
         {
          printf("error closing\n");
          exit(1);
         }
        
        getch();
        return 0;
    }
    
    
    int input_n(void)
    {
        int num;
        printf("How many names(full names)?\n");
        scanf("%d",&num);
        return num;
    }    
    void input_s(int n,char *str[])
    {
         int i;
         char tmp[20];
         printf("Enter full names\n");
         for(i=0;i<n;i++)
         {
         printf("\nName #%d: ",i+1);
         fflush(stdin);
         scanf("%s",&tmp);
         str[i]=tmp;
         }
    }
    void clear_enter(int n,char str[])
    {
         int i;
         i=0;
         while((str[i]!='\n')&&(str[i]!='\0'))
          i++;
         str[i]='\0';
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps start by reading the FAQ, and stop using fflush(stdin) and gets()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    fflush(stdin) : BAD!
    gets() : BAD!
    Reading into an array without checking to make sure you've not overrun its boundaries : BAD!

    Fix those things first.

  4. #4
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    gets is an unsafe function and does not do its own error checking. Meaning that if you if you have to much data for the space you have set aside for it gets will just keep the ball rolling and ignore the bounds of the size of the space you have set aside.

    Please correct if I am wrong.
    -- Will you show me how to c++?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, that's right, and that is exactly why you should never use it.
    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. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM