Thread: Read file of ints, sort it and write to another file

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    180

    Read file of ints, sort it and write to another file

    I'am having some problems with files. I try to write the sorted array to the file but it just fills it with trash.
    My understanding is that i open a new file called Inteiros2.txt and it fwriting writes the numbers. But that's clearly not the case, what's rong?

    Code:
    #include <stdio.h>
    
    void SelectionSort(int v[], int n)
    {
        for(int i=0;i<n-1;i++)
        {
            int iMin=i;
            for(int j=i+1;j<n;j++)
            {
                if(v[j]<v[iMin])
                    iMin=j;
            }
            int aux=v[i];
            v[i]=v[iMin];
            v[iMin]=aux;
            
        }
    }
    int main()
    {
        FILE *f;
        f=fopen("C:\\Users\\PC\\Documents\\Inteiros.txt", "r");
        if(f==NULL)
        {
            printf("Erro\n");
            return 0;
        }    
        int numbers[100], num, i=0;
        while(fscanf(f, "%d", &num)>0)
        {
            numbers[i]=num;
            i++;    
        }
        fclose(f);
        printf("%d numbers were read\n", i);
        SelectionSort(numbers, 61);
        for(int i=0;i<61;i++)
        {
            printf("%d --> %d\n", i, numbers[i]);
        }
        FILE *f2=fopen("C:\\Users\\PC\\Documents\\Inteiros2.txt", "w");
        if(f==NULL)
        {
            printf("Erro\n");
            return 0;
        }
        fwrite(numbers, sizeof(int), 61,f2);
        fclose(f2);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should count the number of numbers read from the file (which you do with the variable named i), then only use that portion of the array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    I thought i was doing that with the 61 parameter in the fwrite function.
    I changed the array to numbers[61] but Inteiros2.txt is still with trash. See there how it looks:
    Read file of ints, sort it and write to another file-6ced8b029b-jpg

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    The filename 'Inteiros2.txt' sugests that it should be a plain text file, but you write a binary file.
    Insteed of 'fwrite' you should use 'fprintf' in a loop.
    Other have classes, we are class

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by telmo_d
    I thought i was doing that with the 61 parameter in the fwrite function.
    Why 61? 61 is a magic number. Worse still, it is a constant where a variable is expected. Are you absolutely sure that there are 61 numbers in your input file? Are you absolutely sure that there will always and forever be 61 numbers in your input file? If so, well and good, but I can tell you that changing 61 to 5 and testing with 5 4 3 2 1, your program works for me, but leaving 61 and again testing with 5 4 3 2 1, I get the kind of problematic output that you encountered.

    Quote Originally Posted by telmo_d
    I changed the array to numbers[61] but Inteiros2.txt is still with trash. See there how it looks:
    You should use a named constant for the number of elements of the numbers array and a variable for the number of elements in use, e.g.,
    Code:
    #define NUMBERS_SIZE 100
    Then you could write:
    Code:
    int numbers[NUMBERS_SIZE];
    int numbers_count = 0;
    int num;
    while (numbers_count < NUMBERS_SIZE && fscanf(f, "%d", &num) == 1)
    {
        numbers[numbers_count] = num;
        numbers_count++;
    }
    This way, you avoid the magic number and ensure that you do not read past the bounds of the array. Furthermore, this allows you to replace your use of the magic number 61, e.g.,
    Code:
    SelectionSort(numbers, numbers_count);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    That worked as if by magic... Thanks guys!

    Code:
    #include <stdio.h>
    #define NUM_NUMBERS 61
    
    
    void SelectionSort(int v[], int n)
    {
        int iMin;
        for(int i=0;i<n-1;i++)
        {
            iMin=i;
            for(int j=i+1;j<n;j++)
            {
                if(v[j]<v[iMin])
                {
                    iMin=j;
                }
            }
            int aux=v[i];
            v[i]=v[iMin];
            v[iMin]=aux;    
        }
    }
    
    
    
    
    int main()
    {
        FILE *f, *f2;
        int inteiros[NUM_NUMBERS], number, i=0;
        f=fopen("C:\\Users\\PC\\Documents\\Inteiros.txt", "r");
        if(f==NULL)
            printf("Erro\n");
            
        while(i <NUM_NUMBERS && fscanf(f, "%d",&number)==1)
        {
            inteiros[i]=number;
            i++;
        }
        printf("Foram lidos %d numeros\n", i);
        fclose(f);
        SelectionSort(inteiros, NUM_NUMBERS);
        f2=fopen("C:\\Users\\PC\\Documents\\Inteiros2.txt", "w");
        for(int i=0;i<NUM_NUMBERS;i++)
        {
            fprintf(f2, "%d\n", inteiros[i]);
            
        }
        fclose(f2);
    
    
        return 0;
        
    }

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    When you open your file for reading, you check if it opened successfully ... but then still try to access it either way. If the file failed to open, you should take appropriate action. In your case, since the entire program depends on that file, you should exit the program if it failed to open properly.

    When you open your file for writing, you don't even check if it opened successfully.

    [edit] I see these checks in the original version of the program ... where'd they go?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 01-04-2013, 11:20 PM
  2. Replies: 2
    Last Post: 01-02-2013, 11:03 PM
  3. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  4. Basic file read, file write program
    By starwarsyeah in forum C++ Programming
    Replies: 5
    Last Post: 02-28-2011, 03:23 PM
  5. Replies: 15
    Last Post: 12-17-2008, 12:11 AM