reading in and sorting a data file line by line

I have data files (named filename) with multiple rows of doubles separated by whitespace. I need to read the data file line by line, as attempted through the function fgets, and compare the values of the first column and reorder from lowest to highest, as attempted through assigning each line to a new element of an array array[i], isolating the first double by sscanf and comparing them, followed by reassigning the array elements. Trouble is, currently this code segment is compiling but not doing anything. any pointers as to where I might be going wrong would be highly appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
int count;
char filename[100], a[count], array[count], line, restofline1[count], restofline2[count];
double doub_i, doub_j;


  FILE * newfile;
        newfile = fopen(filename, "r+");
        for (i=0; i<=count; i++)
        {
            fgets(&line, 100, newfile);
            array[i] = line;
        }
        for (i=0; i<=count; i++)
        {
            for (int j=0; j<=count; j++)
            {
            
                 sscanf(&array[i], "%f %s", &doub_i, &restofline1);
                 sscanf(&array[j], "%f %s", &doub_j, &restofline2);
                if (doub_i > doub_j)
                {
                    for (int b=0; b<=count; b++)
                    {
                        a[b] = array[i];
                        array[i] = array[j];
                        array[j] = a[b];
                    }
                }
            }            
            fputs(&array[i], newfile);
                    
        }
        fclose(newfile);
        return true;
    }
    else return false;
}
Last edited on
Lines 2 isn't standard C++ and won't compile without compiler extensions. Even if it compiles, it produces undefined behavior since count is uninitialized.
Line 7: filename is uninitialized so chances are excellent that it won't work.
Line 10: you're attempting to read 100 characters and stuff them into a single char (line is defined as a char). Again, this won't work.
Line 11 sets the i'th character of array to line.
Lines 22-27 swap the ith and jth characters of array, b count+1 times.

I'm sorry to say that this is basically a mess.
Can you use strings and vectors? That will make this a whole lot easier.
Thanks dhayden. I'm using C as opposed to C++ if that helps?? I can't know if this forum is specifically for just C++. My filename is initialized earlier in the code that i didn't post, but when i created a file through ofstream myfile (filename), where there is a previous line, cin<<filename.
I am obviously quite new to C/C++ so my knowledge of some of these functions and assigning data types etc is obviously limited. so i guess with C, strings can't be used exactly, more char arrays right? I can use vectors though
@aengland

Also, lines 8 and 13 are incorrect. Since the array is initialized to 100, and arrays start at 0, you have only array[0] thru array[99]. Reading into array[100] will give error since it would be out of bounds.

Use for (i=0; i<count; i++) instead. ( NO = sign )
ah cheers whitenite, forgot about zero indexing
Last edited on
Sorry I don't understand. First you said you use C, then you mention C++ constructs.
I'm using C as opposed to C++
ok that's clear.
cin<<filename
This is C++
I can use vectors though
Again vectors are C++
dhayden, when I change the data type for line, to char *line, or char line[100], this comes up with errors. I need to have a char for the first argument of fgets() is there any way around this?
neither do i tbh, the files are saved as .c, and string is not a valid, though cin and cout works fine. Am i right in thinking the whole code is actually C++,and i just need a #include <string> to get strings to work? I am working with code that someone else has previously created
to do this in C you can use the strcmp function to sort the strings.
you will have to write your own sort around that, let me recommend shell short which is just a touch off the simple insertion sort code but runs significantly better. Plenty of examples of that out there.

The rest is just pulling it together. A C struct to organize the data on each line of the file into strings for the various fields, some construct to sort (an array/pointer is simple enough), maybe a quick count # of records in the file function then allocate the pointer then read it in and sort it approach?

You should upcase or lowercase the strings before comparison.
Last edited on
BTW, if this is on a UNIX-like machine then you don't have to write any code at all, just use sort -n filename on the command line.

Is this homework?
oh my holy days, dhayden, that's awesome, and hilarious. I was spending ages agonising over this, to find out there is a simple linux command. great...
Topic archived. No new replies allowed.