DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 20th August 2008
18Googol2's Avatar
18Googol2 18Googol2 is offline
Real Name: whoami
Spam Deminer
 
Join Date: Apr 2008
Location: pwd
Posts: 283
Thanked 20 Times in 18 Posts
Default C and file input/output

Not sure if uni work's assistance is allowed here, pls advise me if itsnt. Anyway, Im studying C and atm, I have problem dealing with file input/output. Have been trying hard to work out, but no luck so far

Im given a file with the format like this:

Code:
A, 1, B
B, 3, D, E, F
C, 1, D
D, 1, F
...
And here is the diagram:



So, basically it can be interpreted like this:

A depends on 1 other class : B
B depends on 3 other classes: D, E, F
...

Im thinking about struct and array, but not sure if its the right method. Also, I have no idea how to read the file and write to memory with such format.

Can you give me a hint on it? I appreciate any input

Last edited by 18Googol2; 20th August 2008 at 07:34 AM.
Reply With Quote
  #2   (View Single Post)  
Old 20th August 2008
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 2,880
Thanked 190 Times in 160 Posts
Default

Quote:
Originally Posted by 18Googol2 View Post
Im thinking about struct and array, but not sure if its the right method. Also, I have no idea how to read the file and write to memory with such format.
There is no single right answer. Everything in programming is a compromise attempting to balance corporate bureaucracy, hardware constraints, time constraints, & considering the poor slob who gets to inherit your code once you leave the project or company.

That said, the format described dictates an easy representation:
  • The first character read names the node itself.
  • The next value read defines the number of dependencies/adjacencies...
  • ...followed by an enumeration of all the dependencies'/adjacencies' names.
The question ultimately becomes how to represent the variable list of names. If you know the upper bound of how many dependencies/adjacencies to expect, then you can use an array, however as a precaution, I would recommend you implement the error logic needed to ensure that the array bounds are not exceeded.

If this is how you implement the solution, you will need to a structure which:
  • stores the node's name.
  • stores the number of dependencies/adjacencies.
  • stores the names of all dependencies/adjacencies in an array.

A better solution would be to implement a linked list which gets away from the constraints of an array. This approach simplifies what needs to be stored given that the linked list is only restricted by available heapspace:
  • Again, store the node's name.
  • Store the names of all dependencies/adjacencies in a linked list.
  • Maintaining the number of dependencies/adjacencies is optional.
Perhaps you haven't gotten into dynamic memory allocation yet in your studies. If not, then the second approach is not an option.

So, this only defines a single record. Either you know the maximum number of records to expect, or you don't. If you do, then you can create an array of records, but you will also require an external index counting how many records have been read thus far. This describes one approach.

Again, a better approach is to create a linked list of records for the same reasons as before. Yes, this means managing nested linked lists, but if you test incrementally as you write the code, this won't be as daunted as it may appear.
Reply With Quote
  #3   (View Single Post)  
Old 20th August 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

isn't that a digraph (directed graph)?
Reply With Quote
  #4   (View Single Post)  
Old 20th August 2008
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

You might want to look at examples of topological sorting, it should be easy enough to find online; even /usr/src/usr.bin/tsort/tsort.c on FreeBSD. tsort is rarely used but useful on occasion.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP read file contents - Maximum file size cksraj Programming 1 21st September 2009 11:38 AM
Input foreign characters under X11 Beastie General software and network 5 30th August 2009 11:51 AM
Running a command with input from a file. bigb89 Programming 4 21st January 2009 06:36 PM
shell script with input c0mrade Programming 5 13th July 2008 04:33 AM
xkb multibyte characters input problem gosha General software and network 8 13th June 2008 12:13 AM


All times are GMT. The time now is 09:45 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick