tring to sort file along with converting it from char to int

This is a discussion on tring to sort file along with converting it from char to int within the C++ Programming forums, part of the General Programming Boards category; I seem to have tried everything I know. This thing is due in a few days so any help would ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    2

    tring to sort file along with converting it from char to int

    I seem to have tried everything I know. This thing is due in a few days so any help would be greatly appreciated. I read numbers from a text file and sort them.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAXLINE 30
    void sort(int[], int);
    void display(int[], int);
    FILE *fp;
    
    int main(void)
    {
       int x;
       int i;
       fp = fopen("C:\\grades_data.txt", "r");
       char line[MAXLINE];
       while(fgets(line, MAXLINE, fp) != NULL && line[0] != '\n')
             fputs(line, stdout);
       for(i = 0; i < MAXLINE; i++)
          x = atoi(line);
       sort(x, 30);
       fclose(fp);
       system("PAUSE");
       return 0;
    }
    
    void sort(int n[], int size)
    {
       for(int i = 0; i < size; i++)
       {
          for(int j = 0; j < size; j++)
          {
             if(n[i] < n[j])
             {
                int temp = n[i];
                n[i] = n[j];
                n[j] = temp;
             }
          }
       }
    
       display(n, size);
    }
    
    void display(int n[], int size)
    {
       int i;
       for(i = 0; i < size; i++)
       {
          printf("This is the sorted list %d :\n", n[i]);
       }
    }
    here is the contents of the grades_data.txt file
    Code:
    9
    25
    55
    1
    4
    98
    74
    44
    11
    19
    0
    85
    64
    23
    3
    6
    99
    51
    100
    0
    68
    97
    90
    13
    17
    45
    38
    42
    12
    24

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    what exactly is the problem?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    while(fgets(line, MAXLINE, fp) != NULL && line[0] != '\n')
        fputs(line, stdout);
    Read a line, print a line. And you're saving the value contained on the line, where?
    Code:
    for(i = 0; i < MAXLINE; i++)
        x = atoi(line);
    Ah, this must be where you're trying to save the contents of the file in an array. Unfortunately, it's too late by now. line contains the last line, but all of the others are lost. It would be better to put this in the while loop that reads lines.

    >int x;
    This should be an array such as:
    Code:
    int x[MAXLINE];
    >x = atoi(line);
    x[i] = atoi(line);

    Try replacing your main function with this one:
    Code:
    int main(void)
    {
       int x[MAXLINE];
       int i = 0;
       fp = fopen("C:\\grades_data.txt", "r");
       char line[MAXLINE];
       while(fgets(line, MAXLINE, fp) != NULL && line[0] != '\n') {
           if(i == MAXLINE)
               break;
           fputs(line, stdout);
           x[i++] = atoi(line);
       }
       sort(x, 30);
       fclose(fp);
       system("PAUSE");
       return 0;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  3. C programing doubt
    By sivasankari in forum C Programming
    Replies: 2
    Last Post: 04-29-2008, 09:19 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 03:24 PM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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