Copying into array and display in Command promt

This is a discussion on Copying into array and display in Command promt within the C Programming forums, part of the General Programming Boards category; Hi I have done the whole code it works fine except for some really wierd symbols that get displayed when ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    3

    Copying into array and display in Command promt

    Hi
    I have done the whole code it works fine except for some really wierd symbols that get displayed when I exceute. I have gone through it with a fine toothed comb but cannot find the issue, maybe someone can just point it to me if u see whats the issue
    Following I the .txt file it is supposed to read and then the code and after the code am pasting the wierd display with the symbols

    PHP Code:
    Darwin,3,33
    Darwin
    ,1,11
    Darwin
    ,6,66
    Darwin
    ,4,44
    Darwin
    ,5,55
    Brisbane
    ,2,222
    Brisbane
    ,1,111
    Brisbane
    ,6,666
    Brisbane
    ,3,333
    Sydney
    ,1,101
    Sydney
    ,2,202
    Sydney
    ,3,303
    Sydney
    ,6,606
    Sydney
    ,5,505
    Sydney
    ,4,404
    Perth
    ,3,313
    Perth
    ,4,414
    Perth
    ,5,515 
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    
    
    void main()
    {
        char str[80],str1[80];
        int n,i,j,temp,acc[100],indexes[100];
        char index,*p;
       FILE *fp,*fw;
    
       fw=fopen("index.txt","w");
      printf("\n How many records do you want : ");
      scanf("%d",&n);
       for(i=0;i<n;i++)
       {
        printf("\n Enter account number : ");
        scanf("%d",&acc[i]);
       }
    
      i=0;
      while(i<n)
      {
      index='0';
       fp=fopen("Accounts.txt","r");
    
       if(fp==NULL)
       {
       printf("\n file does not exist");
       exit(-1);
       }
       if(fw==NULL)
       {
       printf("\n file does not exist");
       exit(-1);
       }
       printf(" i = %d \n ",i);
    
       while(fgets(str,79,fp)!=NULL)
       {
    
          printf("account = %d and index =%c \n ",acc[i],index);
           p = strtok(str, "\t");
           p=strtok(NULL,"\t");
           printf("atoi = %d \n",atoi(p));
    
          if(atoi(p)==acc[i])
          {
    
          printf("index = %c , account = %s\n",index, p);
    		fputc(index,fw);
    		  fputc('\n',fw);
    	break;
    	}
    
    	index++;
       }
       i++;
        fclose(fp);
      }
        fclose(fw);
    
        fp=fopen("index.txt","r");
    
        i=0;
       //  sort index file
    
        while(fgets(str,79,fp)!=NULL)
        {
           indexes[i]=atoi(str);
           printf("index[i]= %d",indexes[i]);
           i++;
        }
        fclose(fp);
    
        for(i=0;i<n;i++)
        {
         for(j=0;j<(n-i-1);j++)
         {
           if(indexes[j]>indexes[j+1])
           {
    	     temp=indexes[j];
    	     indexes[j]=indexes[j+1];
    	     indexes[j+1]=temp;
           }
         }
        }
        i=0;
        fp=fopen("index.txt","w");
        while(i<n)
        {
        index=indexes[i]+'0';
        printf("\n index[i]= %c",index);
          fputc(index,fp);
          fputc('\n',fp);
          i++;
        }
        fclose(fp);
    
        fp=fopen("index.txt","r");
    
    
        printf("\n\n output - \n ");
        while(fgets(str,79,fp)!=NULL)
        {
           printf("\n str= %s",str);
          i=atoi(str);
           j=0;
           fw=fopen("Accounts.txt","r");
           while(fgets(str1,79,fw)!=NULL)
           {
    	if(i==j)
    	{
    	  printf("\n %s  ",str1);
    	  break;
    	}
    	j++;
           }
           fclose(fw);
        }
    
        fclose(fp);
    
    	scanf("%*c");
    }

    PHP Code:
    City              Jan      Feb    Mar    Apr    May    Jun

    Darwin
    &#9552;════
    Brisbane&#9552;══
    Sydney&#9552;════
    Perth&#9552;═════
    Perth&#9552;════,
    Perth&#9552;═════께께슬슬슬슬         11      0       33      44      55      66


    Brisbane&#9552;══
    Sydney&#9552;════
    Perth&#9552;═════
    Perth&#9552;════,
    Perth&#9552;═════께께슬슬슬슬 111     222     333     0       0       666

    Sydney&#9552;════
    Perth&#9552;═════
    Perth&#9552;════,
    Perth&#9552;═════께께슬슬슬슬         101     202     303     404     505     606


    Perth&#9552;═════
    Perth&#9552;════,
    Perth&#9552;═════께께슬슬슬슬         0       0       313     0       0       0 
    can someone pls tell me where I am wrong?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    now the output shows the desired city first with the rainfalls .... but for some reason is showing the other cities and the symbols

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You need to seriously rework the way you are reading that file... Look at fgets() and sscanf() ... the way you're doing it now you are printing stuff before you even know what it is.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Any time you see "funny characters" when working with strings, it's a pretty safe bet that you forgot to nul terminate one. Or you're playing with a pointer that doesn't point where you thought it did anymore.

    I'm with Tater on this one. Any time you have formatted data, one of the scanf functions is a pretty safe bet.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    3
    nothing was said anywhere else Salem ... thats what forced me to come here ... to no avail

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Plenty was said, you're just too lazy to take it on board.
    Hence the fact that this thread and the one on DW appeared within hours of one another.

    You never gave them a chance to respond before spamming away.

    Ah ........ it, you're not worth the effort.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help in copying unsigned char array to bit array
    By lovesunset21 in forum C Programming
    Replies: 8
    Last Post: 10-29-2010, 07:23 AM
  2. Command Promt showing in window programming - Compiler dependent?
    By random_cpp_user in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2010, 04:24 AM
  3. change command promt color ?
    By jamaican1231 in forum Tech Board
    Replies: 3
    Last Post: 04-15-2010, 08:24 AM
  4. Command to display X lines per page
    By prepare in forum C Programming
    Replies: 2
    Last Post: 09-23-2004, 08:45 PM
  5. Window won't display on button command!?
    By psychopath in forum Windows Programming
    Replies: 6
    Last Post: 06-22-2004, 08:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21