0

Given a text file with a semi-known format. Total characters in 1 line will be less than 1000. Zinteger is just a normal integer but it serves a different purpose. String terminated via space.

String ZInteger Integer Integer

String ZInteger Integer Integer Integer Integer

So its a word followed by a number followed by pairs of numbers, but a random amount of pairs.

I want to store the string , Zinteger, and integer pairs for each line in a data structure.

So I tried an array where A[1] would be a struct that has String, Zinteger and the pairs of integers which will be another struct that has the integer pair. Heres what i tried.

typedef struct {
int num1;
int num2;
} ints_t;

typedef struct {
char term[1000];
int quantity(bad variable name, could be called alpha);
ints_t *pairs;
} info_t;

Help is appreciated.

EDIT: Alright so im being too open. So ill just ask a simple question are the two structs I made viable and if not how do I make them viable and how do I malloc the structs and array.

15
  • That's a lot of questions in one, and to be honest, the quickest way to answer them is to write code for you – which we won't do, since this is not a free code writing service. You should maybe start with just reading in things to structs, and printing to check whether you've got that right, and then you should try to put structs into arrays. Then ask precise, answerable questions (or don't. All your individual questions have very likely already been answered multiple times on this site – the trick is breaking down your problem into simpler problems that can be searched for). Commented Oct 15, 2016 at 10:35
  • 1
    The respective close vote reason I chose is There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs. Commented Oct 15, 2016 at 10:36
  • 1
    What is a String and what is a ZInteger. Is there a limit to the size of the string? How is the string terminated in the text file? Commented Oct 15, 2016 at 10:36
  • Alright I narrowed it down to just making the struct work. Commented Oct 15, 2016 at 10:50
  • Zinteger wouldn't happen to be the number of following integers (or pairs of integers) would it? Commented Oct 15, 2016 at 10:59

1 Answer 1

1

Your structure looks reasonable, however, it is missing a field to store a count for the number of pairs:

typedef struct {
    int num1;
    int num2;
} int_pair_t;

typedef struct {
    char term[1000];
    int zinteger;    /* so named to avoid confusion */
    int n_pairs;
    int_pair_t *pairs;
} info_t;

Given a maximum of 1000 characters per line, and assuming a one character string followed by a space, followed by a single digit Zinteger, 332 is the greatest number of pairs (single digit followed by space followed by a single digit) that could be accommodated in the remaining characters.

So you could use a fixed size array of int_pair_t pairs[332] into which the pairs from one line are read, as well as a string for the term, and ints for the Zinteger and pair count. Once you have read the line, you can copy the pairs data into a newly malloced info_t struct of precisely the right size, and add that to whatever collection you have for the lines.

If you don't overly care about memory usage (it's less than 3KB per line), you can skip the malloc and copy, and just allocate a fixed sized array in the info_t struct:

#define MAX_PAIRS    332

typedef struct {
    char term[1000];
    int zinteger;    /* so named to avoid confusion */
    int n_pairs;
    int_pair_t pairs[MAX_PAIRS];
} info_t;

Your original question also asked how to read the data in from a text file. Read a line of data from the file into a char buffer. Then you can use strtok() to process the fields from the file using space as a delimiter. You can combine that with sscanf() to extract the first 2 fields if you want, and the process the remaining fields with strtok.

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

3 Comments

Yeah I could just allocate a fixed size array, but I gotta dynamically allocate memory using malloc and realloc. Which is what Im having the most trouble with atm.
@Diablo: why do you have to use realloc? I doubt that your application can not afford 3KB for reading the data from the file. If you can't afford it then you could dynamically allocate once the memory for reading on line of the file and free it afterward the file has been read.
Alright so i just read it again and the 1000character limit is for each term in the line. Sorry so I dont know how many terms there will be. What do you think a suitable size for Max_Pairs would be that isnt too small or too larger? Like 5000?

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.