LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-24-2004, 04:27 PM   #1
Keffin
LQ Newbie
 
Registered: Sep 2003
Location: England
Distribution: VectorLinux 4.0
Posts: 19

Rep: Reputation: 0
C help needed - reading from a complicated text file


Hi, I'm writing a C program that needs to read input from a text file that contains one or more objects and several sets of x,y coordinates. An example file currently looks like this:

Quote:
thing
10 20
20 30
40 10
whatsit
10 20
20 30
10 50
In the example above I would need to end up with a linked list with 2 nodes - "thing" and "whatsit", each holding their respective sets of coordinates in an array. The thing is that I do not know exactly how many sets of coordinates each object will have, so how would I go about reading such a file in? I currently have a solution for a file containing only 1 object:

Code:
count = 0;
fscanf(compFile, "%s", &compName);
while (!feof(compFile)) {
   fscanf(compFile, "%i", &xOffset);
   fscanf(compFile, "%i", &yOffset);
   strcpy(newNode->name, compName);
   newNode->point[count][x] = xval + xOffset;
   newNode->point[count][y] = yval + yOffset;
   count++;
}
I have the "point" member of the "newNode" structure as an array[100][2] i.e. able to hold 100 coordinates at the moment to make sure it can hold a worst-case length coordinates list, which is rather hacky but I'm fine with it at the moment.

When there is more than one object in a file I need some clever conditional statement that can tell if the next line contains a string (a new compName - start of new object) or a number (continuation of coordinates list), but I can't use atoi() because on a character string it would return 0 (zero), which is a valid x coordinate too.

All I can think of at the moment is adding an extra number at the start of each line that is a 0 or 1 depending on whether the line contains coordinates or the name of a new object. This would make the input file "unnatural" to write and I would like to avoid this at all costs.

Anyone have an idea how to do this? If it involves changing the layout of the input text file to help then that could as long as it remains intuitive to write.

Thanks for any input/ideas.
 
Old 01-24-2004, 08:33 PM   #2
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
There are several functions called isalpha(), isdigit(), and so on - try 'man isalpha' for a manual page, or google for them. What you could do is just read one character from the next line, and call isalpha() on it to see whether it's a letter or a number.

Also, maybe not the best solution, but one way you could solve the problem of not knowing how big to make the coordinates array:

Declare a really large array to hold input, large enough for the worst-case scenario. Read stuff from the file into the large array, and count how many you've read in; when you get to the end of the list of coordinates, use malloc or new to dynamically allocate however much space you need, and copy the data from the large array into the object. Lather, rinse, repeat
 
Old 01-25-2004, 05:20 AM   #3
Keffin
LQ Newbie
 
Registered: Sep 2003
Location: England
Distribution: VectorLinux 4.0
Posts: 19

Original Poster
Rep: Reputation: 0
Bingo!

Thanks a lot wapcaplet, isdigit() is just the thing I needed. I now read in the next full string and test the first character of it with (isdigit(test[0]) || test[0] == '-') (I can have negative numbers too). By taking a full string to test I don't have to worry about reading in the other half of a number (I can atoi the string) or the rest of a name (I would have the full thing).

And about the second part of your reply, I was thinking something along those lines, but it got very confusing the second I actually gave it some serious thought . I'm not worried by that though, it works as it is and that's the main thing for now at least.

Thanks again.
 
Old 01-25-2004, 10:24 AM   #4
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
Glad that worked for you.

Another, probably simpler, solution for knowing how large your array needs to be is to include an extra line in the file with the number of coordinates on it, like:

name
5 // number of coordinates that follow
0 2
3 1
5 6
1 4
2 2
anothername
3
...

But you'd still need to do some dynamic allocation to avoid wasting a lot of space with unfilled arrays.
 
Old 01-26-2004, 09:32 PM   #5
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
I agree with wapcaplet. I would definitely use dynamic allocation with variable-length arrays, otherwise it's going to waste a lot of memory.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading text from a file using shell scripting. mrobertson Programming 11 06-29-2005 12:12 PM
reading text from a file mrobertson Programming 16 06-28-2005 12:39 PM
C++ reading from text file query lrt2003 Programming 5 05-15-2004 04:25 AM
Searching a text file - complicated cyph3r7 Linux - General 2 12-16-2003 10:45 AM
reading a text file and outputting to another. Hardw1re Programming 28 11-03-2003 08:51 AM


All times are GMT -5. The time now is 07:24 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration