0

I have a file where i have some records like that:

test one; test one; test one; 1
test two; test two; test two; 2

I need to sort those records according to the last number, so in my previous example the second record should be at the first place, since 2>1. For this, i'm trying to add each record to an array and then apply an insertion sort algorithm. I have some problems adding each part to an array, here is my current effort:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100

int main() {

    char one[MAXLEN] = {};
    char two[MAXLEN] = {};
    char three[MAXLEN] = {};
    int st[MAXLEN] = {};
    int i, j;

    FILE * fpointer = fopen("clients.txt", "r");


    for (i = 0; i < MAXLEN; i++) {
       fscanf(fpointer, "%s%s%s%d", &one[i], &two[i], &three[i], &st[i]);
    }


    for (j = 0; j < MAXLEN; j++) {
       printf("%s", one[i]);
    }


    fclose(fpointer);
    return 0;

}

In this example, i tried to add each field to an array, the second for loop is just a test to check whether or not data is being added to the array properly, but it's not.

7
  • btw there's no attempt to sort anything here. how can we help you? Commented Aug 30, 2018 at 13:01
  • @Jean-FrançoisFabre The problem in this question is not the sorting part, i still need to get there! To get to the sorting part, i first need to store everything in some way, and i'm having problem with it Commented Aug 30, 2018 at 13:03
  • 1
    aaah but you need 2D char arrays. currently one is just a string / char array, like char one[MAXLEN][MAXSIZE] Commented Aug 30, 2018 at 13:05
  • Won't i get the "format %s expect argument of type *" if i drop the &? Commented Aug 30, 2018 at 13:06
  • drop the & once you put the 2D char array. Commented Aug 30, 2018 at 13:07

2 Answers 2

1

you're currently scanning your data into 3 strings, shifting the offset by 1 each time, instead of 3 tables of strings.

You need to declare your data some other way. For example a 2D array of char

I suggest a structure instead, and an array of structures, so you have only one index (Here I'm assuming that max size for string is 100):

   typedef struct Element
   {
    char one[100];
    char two[100];
    char three[100];
    int st;
   };

  Element elements[MAXLEN];

now scan like this:

  for (i = 0; i < MAXLEN; i++) {
       Element *e = elements+i;  // pointer on ith element
       fscanf(fpointer, "%99s%99s%99s%d", e->one, e->two, e->three, &e->st);
    }

Use & on the integer, not on the strings (already pointers, they are). Also maybe it's good to check that fscanf returns 4 (error checking). The 99s ensures that you're not overflowing your strings (max len: 100 with nul terminator)

Aside: if you have strings with spaces in it, scanf isn't going to work properly, you'll have to use fgets then strtok on the semicolons to get the items

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

4 Comments

Shouldn't i refer to the data structure with e.one, for example, and not with e->one? I'm getting an error with it
Solved it! the error was here in the loop: struct Element *e = elements+i;
The problem is, indeed, that i have strings of words, so i should use fgets, which is giving me some problems accessing the data structure to store them
Here is what i tried: fgets("%99s%99s%99s%d", e->one, e->two, e->three, &e->st, 255, fpointer);
0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100
#define MAXWORDS 50
int main() {

    char one[MAXWORDS][MAXLEN] = {};
    char two[MAXWORDS][MAXLEN] = {};
    char three[MAXWORDS][MAXLEN] = {};
    int st[MAXWORDS][MAXLEN] = {};
    int i, j;

    FILE * fpointer = fopen("clients.txt", "r");


    for (i = 0; i < MAXWORDS; i++) {
       fscanf(fpointer, "%s%s%s%d", one[i], two[i], three[i], st[i]);
    }


    for (j = 0; j < MAXWORDS; j++) {
       printf("%s", one[j]);
    }


    fclose(fpointer);
    return 0;

}

check this program its working...! In this program you used the fscanf() function which can fetch data from file word by word . Here you need to take care that fscanf() wont give you the spaces as well as '\n'- line termination character .

1 Comment

Thanks! I'm undecided on which way to use, this one or the other one with the data structure!

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.