The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
read from a formated data file
Discuss read from a formated data file in the C Programming forum on Dev Shed. read from a formated data file C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 20th, 2002, 08:13 PM
|
|
Junior Member
|
|
Join Date: Oct 2002
Location: Canada
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
read from a formated data file
Hello...
I need to read from a data file and then do use the data for computations. I cannot figure out a way to extract the data. This is what the file looks like(of course, the comments are not in the file):
AB111 //its the ID
34
12 22 4.6
33 67 2.3
.
-1 -1 -1 //end of data block
AB567 //next ID
45
14 44 6.8
.
-1 -1 -1 //marks end of data block
.
.
I need to read each number related to an ID, even the three numbers in one line. Next, for each ID, I use the numbers to computer other stuff. The first two lines for each data block is going to contain the same info(ID# and some integer). The next lines will have 3 numbers in each.
Basically I cannot figure out a way to capture the data, esp. the 3 numbers that are on the same line.
|

October 21st, 2002, 06:12 PM
|
 |
*bounce*
|
|
Join Date: Jan 2002
Location: Delft, The Netherlands
|
|
Ok, you can read lines as a whole using fgets(), then you can use strtod() to get the actual floating-point values. The easiest way (IMO) to communicate the converted values and the amount of values is by using a prototype like:
Code:
int scan_line (char *lline, int *num_values, double values[]);
with *num_values being the amount of numbers converted and returned in values[]. Obviously values[] has to be large enough to hold three values.
Hope this helps.
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/
|

October 21st, 2002, 06:47 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
Why not just use fscanf() as in:
Code:
#include <stdio.h>
int main(void) {
FILE *fp;
char sID[10];
int num1, num2, num3, num4;
char dummy[2];
if ((fp = fopen("test.txt", "r")) == NULL)
return 1;
fscanf(fp, "%s", sID); /* Read the ID */
fscanf(fp, "%d", &num1); /* Read the next number */
fscanf(fp, "%d %d %d", &num2, &num3, &num4); /*Read 3 numbers */
fclose(fp);
printf("We have %s and numbers %d %d %d %d\n", sID, num1, num2, num3, num4);
return 0;
}
This way you don't have to convert the values separately. Hope this helps!
|

October 29th, 2002, 08:56 AM
|
|
Contributing User
|
|
Join Date: Oct 2002
Location: Flint, MI
Posts: 328
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
|
|
|
Re: read from a formated data file
Quote: Originally posted by CplusQ
Hello...
I need to read from a data file and then do use the data for computations. I cannot figure out a way to extract the data. This is what the file looks like(of course, the comments are not in the file):
AB111 //its the ID
34
12 22 4.6
33 67 2.3
.
-1 -1 -1 //end of data block
|
CplusQ,
The function to do this is pretty simple. The data structure necessary to maintain all of that data is less so. You have two options:
1) store it in a linked list - if you don't know what a linked list is, grab a copy of "Mastering Data Structures and Algorithms in C" from O'Rielly & Associates, which will provide both explanation and implementation.
2) Store them in a two-dimensional array. This is harier than the linked lists, because you'll need to determine the size of the array and allocate it dynamically. I wouldn't do this unless the ability to index into the list of numbers is essential to your calculations.
The general solution for the second route is to read the entire data file into memory, and store it as a big string. The following code will do that for you:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
...
struct stat sb;
FILE* fdata;
char* data;
if (stat("datafile.dat", &sb)) {
perror("stat");
return 0;
}
data = (char*)calloc(1, sb.st_size + 1);
fdata = fopen("datafile.dat", "r"):
fread((void*)data, sb.st_size, 1, fdata);
fclose(fdata);
Now that you have all of this data in memory (in the string data), you can use character pointers as bookmarks to remember where a record started. You then read to the end of the record and keep count of how many rows of decimals you read. Then allocate space for them, go back to your bookmark, and start again.
It's going to take some work and debugging on your part, but the basic routine is pretty simple.
__________________
Clay Dowling
Lazarus Notes
Articles and commentary on web development
http://www.lazarusid.com/notes/
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|