Reading in data into array (special case, please see)

R

Rob111b

Hi there,

I need to make a piece of code in C that

1. opens a specified file,
2. Reads the data in the file and separates it as shown below,
3. Converts the strings into integers and then perfoms some
manipulation on them,
4. returns the new values to a second text file (possibly, not quite
sure on this one yet).

My data is given in the following format (there is one space between
a1 and b1, b1 and c1, etc.):
x
y
a1 b1 c1 d1 e1 f1
a2 b2 c2 d2 e2 f2
while not EOF

I would like to read in data in the following format for further
processing:
x
y
a b c d e f
a[i+1] b[i+1] c[i+1] d[i+1] e[i+1] f[i+1]

I guess I need to read it in as an array and then do some sort of
concatenation/separation (my inputs can be one or two digits, i.e.
a can be 1 or 55). This is probably not very difficult, but I
haven't touched C in about 6-7 years. It must be done in C (no C++ or
C#). I tried to search, but couldn't find my case (maybe it was posted
before and I just did not see it).
Please give me some hints. Timewise, I should be able to have it
working ASAP.

Thank You very much.
 
R

Richard Heathfield

(e-mail address removed) said:
Hi there,

I need to make a piece of code in C that

1. opens a specified file,
2. Reads the data in the file and separates it as shown below,
3. Converts the strings into integers and then perfoms some
manipulation on them,
4. returns the new values to a second text file (possibly, not quite
sure on this one yet).

My data is given in the following format (there is one space between
a1 and b1, b1 and c1, etc.):
x
y
a1 b1 c1 d1 e1 f1
a2 b2 c2 d2 e2 f2
while not EOF


Look up fgets and strtol in your C book.

fgets reads a line from a file (if the line isn't too long - if you know
how long your lines are, just make sure you define your array to be
that long plus a few.

strtol converts a string to an integer, and - rather wonderfully -
provides a mechanism for telling you where to continue parsing after
it's done. Very useful in this context.

If you don't know in advance how many rows you have, you will need to
use dynamic allocation for your arrays, and be prepared to realloc from
time to time.

I suspect that x and y will give you that info, though - presumably
that's what they're for?

Anyway, here's your strategy: use fgets and strtol - one after the
other! Don't try to combine them in one call! - to get x. Then do it
again to get y. Don't worry about code repetition at this stage. It's
easier than trying to shoehorn those reads into the main loop.

Now - presumably - use x and y to shape your dynamic array using malloc
(I'm assuming that's what x and y are for, and of course I could be
wrong).

Once you've done that, you are ready for your main loop:

line = 0;
while(fgets(buffer, sizeof buffer, fp) != NULL)
{
char *p = buffer;
char *endl = NULL;
for(datum = 0; datum < x; datum++)
{
array[line][datum] = strtol(p, &endl, 10);
/* here, I'm ignoring the possibility of a duff
data value, but you probably shouldn't. If
endp == p, no conversion was possible.
*/
p = endl + 1;
}
++line;
}

if(line != y)
{
you missed something, I guess!
}


That's about as much help as I can give you without actually doing it
for you. :)
 
R

Rob111b

Thanks Richard,

I probably was not very clear on the task, so here are clarifications:

- x & y have no impact on the # of rows and columns

-I do not know the number of rows in the file and the number of rows
will vary from file to file

-there are always 6 elements in a row (or line), but number of digits
in each element can be 1 or 2 (i.e. a1 can vary from 0 to 99); the
elements (columns) are separated by one space

-I would like to sort data into array as speficied in original post, I
believe it will make the analysis better: I can just increment I to
perform manipulations with the next row of data.

I will take a look on strtol.

Thank You again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top