Hello i need to write a program read a text file and store the data with text in the format of :

name 14 6
name2 15 26

However i cannot use fopen() scanf() getline() or fgets(), it has to be done with only open() read() and strtok() i have almost done it using a series of loops but i cannot seem to get the offset write with the buffer, and it is full of bugs.
please help,
any ideas at all welcome
thanks in advance

>> i have almost done it using a series of loops but i cannot seem to get the offset write with the buffer, and it is full of bugs.


Sort of hard to debug when you haven't posted it, yes?

That's because I think I'm going about it the wrong way I just wanted some general ideas on overcoming this problem?

We can't help you overcome any problems when we have no idea what the problems are.
So far you have given us no information and you have not asked any questions.
(adding a ? to the end of a statement does not make it a question)

Well presumably you have a structure that contains an array of characters and two ints, and you have an array of these structures. One record per line in the file. Your tokens are going to be spaces and line returns, but actually assuming the file is well-formed, just call it all white space. You read a character at a time and throw it away till you bump into something that isn't white space. That starts the character array. Read into the character array till you see more white space. Then read your integer in. Then the next integer. You may need to convert text to int. Try atoi, strtol, whatever. Start again till the end of the file. strtok doesn't seem to be needed to me.

Hi,

According to VernonDozier, you might use structure with a char array and two integer. You build and array with this strucure, then, for every line, you will have to use:

sscanf(line, "%s %d %d", struct_abc[i].char_array , &(struct_abc[i].first_int), &(struct_abc[i].second_int));

Hope it help you

This article has been dead for over six months. Start a new discussion instead.