0

Here is my code so far. I am perfectly able to sort files holding numbers but clueless when it comes to characters. It takes in a file of my choosing and outputs another file with the sorted array. But so far all I'm getting are blank files and I can't figure out why.

So how can I fix my code to sort an array of characters and then output it?

#include <stdio.h>

int bubble_sort(char *a, int n);

int main(void) {
    char a[10];
    int n = sizeof a / sizeof a[10];
    int i;
    char inname;
    char outname;

    printf("Enter input name: ");
    scanf("%s", &inname);
    printf("Enter output name: ");
    scanf("%s", &outname);

    FILE *in, *out;

    out = fopen(&outname, "w");

    if ((in = fopen(&inname, "r")) == NULL) {
        printf("File not found\n");

    }


    else {
        for (int i = 0; i < 10; i++)
        {
            fscanf(in, "%s ", &a[i]);
        }

        bubble_sort(a, n);

        for (i = 0; i < 10; i++) {
            printf("%s\n", a[i]);
            fprintf(out, "%s\n", a[i]);
        }
    }

        fclose(in);
        fclose(out);
        return 0;
    }


int bubble_sort(char *a, int n) {
    int i, j;
    char temp;

        for (j = 1; j<n; j++)
        {
            for (i = 0; i<n - j; i++)
            {
                if ((int)a[i] >= (int)a[i + 1])
                {
                    temp = a[i];
                    a[i] = a[i + 1];
                    a[i + 1] = temp;

            }
        }
    }
    return a[i];
}
4
  • Tip: Change int n = sizeof a / sizeof a[10]; --> int n = sizeof a / sizeof a[0]; Commented Feb 17, 2016 at 17:12
  • It looks like confusion between characters and strings is the main reasn of the problem. Commented Feb 17, 2016 at 17:17
  • Quite probably you want to change fscanf(in, "%s ", &a[i]); to fscanf(in, "%c", &a[i]); Commented Feb 17, 2016 at 17:19
  • @JohnHascall That was it. That is all it took. I'm fairly new to programming and I knew it was going to be something tiny. Thank you so much for your help. Commented Feb 17, 2016 at 17:23

1 Answer 1

2

The basic problem, as I can see, is with

scanf("%s", &inname);

In your code, inname is a single char, which cannot hold string inputs. You'll be needing an array.

You need to change

char inname;
char outname;

to

#define NAMSIZ 32

char inname[NAMSIZ] = {0};
char outname[NAMSIZ] = {0};

and then,

scanf("%31s", inname);

and accordingly.

Same problem exist with fscanf(in, "%s ", &a[i]);, too.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help. I changed what you suggested but its still not working as I had hoped. What did you mean by "Same problem exist with fscanf(in, "%s ", &a[i]); , too." (I'm fairly new to coding to be honest :P)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.