|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
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/ |
|
#3
|
||||
|
||||
|
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! |
|
#4
|
|||
|
|||
|
Re: read from a formated data file
Quote:
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/ |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > read from a formated data file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|