Help with changing char *string to int * string for sorting.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pokerkook

    Help with changing char *string to int * string for sorting.

    Hello,

    If anybody could help me with this I would greatly appreciate it. Or at least
    tell me why I get the output of this garbage:

    49
    49
    10
    49
    52
    10

    I'm trying to sort integers from a text file. Sorry if my code is bad or way
    off base. I am a newbie to the C Language & I am just learning Arrays &
    pointers.


    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>


    void Outputdata(char filename[], char theString[]);
    void Sortdata(int theString[], int n );

    int main()
    {
    char theString[20000]="";
    char filename[20];
    int count;
    int n=10;

    int x[20000];

    printf("\nPleas e enter Filename: ");
    gets(filename); /* user inputs filename */
    Outputdata(file name, theString);
    *x = atoi(theString) ;
    Sortdata( x, n );
    printf("\nAfter the sort, the contents are:\n");

    for( count = 0; count < n; count++ )
    printf("Count = %d = %.d\n", count, theString[ count ]);

    }

    void Sortdata( int String[], int n )

    {
    int index = 0;
    int x = 0;
    int temp;

    while ( x < (n - 1) )
    {
    index = x + 1;
    while ( index < n )
    {
    if( String[ x ] > String[ index ] )
    {
    temp = String[ x ];
    String[ x ] = String[index];
    String[index] = temp;
    }
    index++;
    }
    x++;
    }
    }


    void Outputdata(char filename[], char theString[])
    {

    FILE *fp;

    char buf[BUFSIZ+1];
    fp = fopen (filename, "r"); /* file is 10 integers on separate lines */

    while (fgets(buf, BUFSIZ, fp) != NULL)
    {
    strcat(theStrin g, buf);
    }

    printf("\n%s", theString);

    fclose(fp);

    }

  • Al Bowers

    #2
    Re: Help with changing char *string to int * string for sorting.



    Pokerkook wrote:[color=blue]
    > Hello,
    >
    > If anybody could help me with this I would greatly appreciate it. Or at least
    > tell me why I get the output of this garbage:
    >
    > 49
    > 49
    > 10
    > 49
    > 52
    > 10
    >
    > I'm trying to sort integers from a text file. Sorry if my code is bad or way
    > off base. I am a newbie to the C Language & I am just learning Arrays &
    > pointers.
    >
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <ctype.h>
    > #include <string.h>
    >
    >
    > void Outputdata(char filename[], char theString[]);[/color]
    This function should return a value which can be use
    to determine whether or not the file was accessed.
    [color=blue]
    > void Sortdata(int theString[], int n );
    >
    > int main()
    > {
    > char theString[20000]="";[/color]

    using theString is a poor approach in parsing the file.[color=blue]
    > char filename[20];
    > int count;
    > int n=10;
    >
    > int x[20000];[/color]

    Since the size of the array is unknown, why not dynamically
    allocate space as you need it using function realloc?
    You can define a data struct that will a member pointint to
    the array and another member keeping a count. See the Example
    below.
    [color=blue]
    >[/color]
    [color=blue]
    > printf("\nPleas e enter Filename: ");
    > gets(filename); /* user inputs filename */[/color]
    Function gets very unsafe, use function fgets.[color=blue]
    > Outputdata(file name, theString);
    > *x = atoi(theString) ;
    > Sortdata( x, n );
    > printf("\nAfter the sort, the contents are:\n");
    >
    > for( count = 0; count < n; count++ )
    > printf("Count = %d = %.d\n", count, theString[ count ]);
    >
    > }
    >
    > void Sortdata( int String[], int n )
    >
    >[/color]
    .........snip,, ,,,,,,

    You can use function qsort to sort the numbers.[color=blue]
    >[/color]

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    typedef struct INTARRAY
    {
    int *num;
    size_t count;
    } INTARRAY;

    int GetDataINTARRAY (const char *filename, INTARRAY *p);
    void SortINTARRAY(IN TARRAY *p, int (*cmp)(const void *, const void *));
    void PrintINTARRAY(I NTARRAY *p);
    void FreeINTARRAY(IN TARRAY *p);
    int cmp(const void *v1, const void *v2);

    int main(void)
    {
    char filename[64],*s;
    INTARRAY myint = {NULL};

    printf("\nPleas e enter Filename: ");
    fflush(stdout);
    fgets(filename, sizeof filename,stdin) ;
    if(s = strrchr(filenam e,'\n')) *s = '\0';
    else while('\n' != getchar());
    if(GetDataINTAR RAY(filename, &myint))
    {
    puts("\tUnSorte d");
    PrintINTARRAY(& myint);
    SortINTARRAY(&m yint,cmp);
    puts("\n\tSorte d");
    PrintINTARRAY(& myint);
    }
    else puts("File not Found or Memory Allocation Failure");
    FreeINTARRAY(&m yint);
    return 0;
    }

    void SortINTARRAY(IN TARRAY *p, int (*cmp)(const void *, const void *))
    {
    if(p->num)
    qsort(p->num,(size_t) p->count,sizeof *p->num,cmp);
    return;
    }

    int GetDataINTARRAY (const char *fname, INTARRAY *p)
    {
    FILE *fp;
    int ibuf, *tmp;

    if((fp = fopen (fname, "r")) == NULL) return 0;
    while(1 == fscanf(fp," %d",&ibuf))
    {
    tmp = realloc(p->num,(p->count+1)*(size of *tmp));
    if(tmp == NULL)
    {
    FreeINTARRAY(p) ;
    return 0;
    }
    p->num = tmp;
    p->num[p->count++] = ibuf;
    }
    fclose(fp);
    return 1;
    }

    int cmp(const void *v1, const void *v2)
    {
    const int *i1 = v1;
    const int *i2 = v2;

    return (*i1<*i2)?-1:(*i1!=*i2);
    }

    void PrintINTARRAY(I NTARRAY *p)
    {
    size_t i;

    for(i = 0; i < p->count; i++)
    printf("num[%u] = %d\n",i,p->num[i]);
    return;
    }

    void FreeINTARRAY(IN TARRAY *p)
    {
    free(p->num);
    p->num = NULL;
    p->count = 0;
    return;
    }


    --
    Al Bowers
    Tampa, Fl USA
    mailto: xabowers@myrapi dsys.com (remove the x to send email)
    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


    Comment

    • Jens.Toerring@physik.fu-berlin.de

      #3
      Re: Help with changing char *string to int * string for sorting.

      Pokerkook <pokerkook@aol. com> wrote:[color=blue]
      > I'm trying to sort integers from a text file. Sorry if my code is bad or way
      > off base. I am a newbie to the C Language & I am just learning Arrays &
      > pointers.[/color]
      [color=blue]
      > #include <stdio.h>
      > #include <stdlib.h>
      > #include <ctype.h>
      > #include <string.h>[/color]
      [color=blue]
      > void Outputdata(char filename[], char theString[]);
      > void Sortdata(int theString[], int n );[/color]
      [color=blue]
      > int main()
      > {
      > char theString[20000]="";
      > char filename[20];
      > int count;
      > int n=10;
      >
      > int x[20000];[/color]
      [color=blue]
      > printf("\nPleas e enter Filename: ");[/color]

      You need either a '\n' at the end of the string or call fflush(),
      otherwise the string may not appear.
      [color=blue]
      > gets(filename); /* user inputs filename */[/color]

      Never ever use gets(), this function is nothing than a desaster waiting
      to happen. You can't check if the user inputs a file name with more
      than 19 characters and if he does gets() will happily write past the
      end of the buffer you set aside for 'filename'. Use fgets() instead.
      [color=blue]
      > Outputdata(file name, theString);
      > *x = atoi(theString) ;[/color]

      It looks as if you have the impression that atoi() would convert all
      numbers in the string into integers and store them successively in
      the array 'x'. But that's not what atoi() does: it converts the
      first number in the string it finds and returns this number. You
      will have to chop up the string into the parts consisting of numbers
      and apply atoi() to each of them. Things get a lot easier if you
      use strtol() instead of atoi() since it a) lets you do error checking
      and b) tells you where it left off, so you can use that knowledge
      to figure out where to look for the next number. So you could do
      something like (assuming there are only numbers and white-space in
      the file and, beware, untested)

      size_t i;
      int x[ 20000 ];
      char *startptr;
      char *endptr;

      for ( startptr = theString; i < 20000; i++ )
      {
      x[ i ] = ( int ) strtol( startptr, &endptr, 10 );
      if ( startptr == endptr ) /* no number found in file anymore */
      break;
      startprt = endptr;
      }

      if ( i >= 20000 )
      fprintf( stderr, "Stopped after converting 20000 numbers, "
      "there could be more.\n" );
      else
      fprint( "Got %ld numbers\n", ( long ) ++i );

      You could (and for a real program should) even build in more error
      checking, i.e. for cases where numbers are too large to be repre-
      sented as integers (or even as long integers).
      [color=blue]
      > Sortdata( x, n );[/color]

      Why would you want to sort only the first 'n' numbers and not all
      you read in?
      [color=blue]
      > printf("\nAfter the sort, the contents are:\n");[/color]
      [color=blue]
      > for( count = 0; count < n; count++ )
      > printf("Count = %d = %.d\n", count, theString[ count ]);[/color]

      While the dot in "%.d" won't hurt it doesn't do anything useful.
      And since you promised that main() would return an int you need
      here
      return 0;
      or
      return EXIT_SUCCESS;

      (but you must include <stdlib.h> for the definition of EXIT_SUCCESS).
      [color=blue]
      > }[/color]
      [color=blue]
      > void Sortdata( int String[], int n )
      > {
      > int index = 0;
      > int x = 0;
      > int temp;[/color]
      [color=blue]
      > while ( x < (n - 1) )
      > {
      > index = x + 1;
      > while ( index < n )
      > {
      > if( String[ x ] > String[ index ] )
      > {
      > temp = String[ x ];
      > String[ x ] = String[index];
      > String[index] = temp;
      > }
      > index++;
      > }
      > x++;
      > }
      > }[/color]

      Bubblesort - not very famous for it's speed. Why not use qsort() that
      is already built-in into C?
      [color=blue]
      > void Outputdata(char filename[], char theString[])
      > {[/color]
      [color=blue]
      > FILE *fp;[/color]
      [color=blue]
      > char buf[BUFSIZ+1];
      > fp = fopen (filename, "r"); /* file is 10 integers on separate lines */[/color]

      And what is supposed to happen when the file does not exist or you
      don't have permissions to open it, i.e. when fopen() returns NULL?
      [color=blue]
      > while (fgets(buf, BUFSIZ, fp) != NULL)
      > {
      > strcat(theStrin g, buf);
      > }[/color]
      [color=blue]
      > printf("\n%s", theString);[/color]
      [color=blue]
      > fclose(fp);[/color]
      [color=blue]
      > }[/color]

      You should be more careful here. All this will probably work fine with
      your short file, but when you try to use the program with a file that
      is longer than the 20000 characters you set aside for 'theString'
      suddenly all kind of weird problems will start to appear since you then
      are going to write past the end of that buffer.

      Once you got that program to work properly you should start to learn
      about dynamical memory allocation. That will allow you to read in
      files of all lengths (limited only by how much memory your system
      is willing to give your program).
      Regards, Jens
      --
      \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
      \______________ ____________ http://www.toerring.de

      Comment

      • Gregory Shearman

        #4
        Re: Help with changing char *string to int * string for sorting.

        On Sat, 04 Dec 2004 16:35:48 +0000, Jens.Toerring wrote:
        [color=blue][color=green]
        >> printf("\nPleas e enter Filename: ");[/color]
        >
        > You need either a '\n' at the end of the string or call fflush(),
        > otherwise the string may not appear.[/color]

        You can always turn off the buffering on the standard output stream using
        setvbuf(). It would save a lot of calls to fflush() every time you want to
        print something to the controlling terminal.

        --
        Regards,
        Gregory.
        "Ding-a-Ding Dang, My Dang-a-Long Ling Long."


        Comment

        • Lawrence Kirby

          #5
          Re: Help with changing char *string to int * string for sorting.

          On Thu, 09 Dec 2004 11:36:56 +1100, Gregory Shearman wrote:
          [color=blue]
          > On Sat, 04 Dec 2004 16:35:48 +0000, Jens.Toerring wrote:
          >[color=green][color=darkred]
          >>> printf("\nPleas e enter Filename: ");[/color]
          >>
          >> You need either a '\n' at the end of the string or call fflush(),
          >> otherwise the string may not appear.[/color]
          >
          > You can always turn off the buffering on the standard output stream using
          > setvbuf(). It would save a lot of calls to fflush() every time you want to
          > print something to the controlling terminal.[/color]

          However buffering is usually a GOOD THING. Try to avoid turning it off if
          you can.

          Sometimes a better strategy than flushing after you "print" is to flush
          just before you input.

          Lawrence

          Comment

          Working...