473,412 Members | 4,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

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

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[i] b[i] c[i] d[i] e[i] f[i]
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[i] 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.

Apr 20 '07 #1
2 1461
Ro*****@gmail.com 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. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Apr 20 '07 #2
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.

Apr 21 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
6
by: Dietrich Epp | last post by:
Are there any good modules for reading a bitstream? Specifically, I have a string and I want to be able to get the next N bits as an integer. Right now I'm using struct.unpack and bit operations,...
2
by: Chucker | last post by:
Hi Community, I think I can store Binary Data in SQL Server but when I try to retrieve it, I always only get one byte. I think I stored my Binary Data in SQL Server in a Colum of Type Image....
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
10
by: nuke1872 | last post by:
Hello guys, I have a file names network.txt which contains a matrix. I want to read this matrix as store it as an array. I am new to stuff like these...can anybody help me out !! Thanks nuke
31
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
8
by: Peter Bradley | last post by:
Hi, I wonder if anyone can help me out? I'm trying to implement an EPP (rfc4934 and rfc4930) client. So far I've managed to connect and authorise using an X509 Certificate. This should...
6
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.