![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Extracting data from text file based on configuration set in config file | suparnbector | Shell Programming and Scripting | 3 | 08-09-2007 11:25 PM |
| Extracting Data from a File | oop | UNIX for Dummies Questions & Answers | 0 | 07-31-2007 08:48 AM |
| Extracting Data from xml file | nishana | Shell Programming and Scripting | 3 | 07-13-2007 04:17 AM |
| Extracting data from an AFP file | Dolph | UNIX for Advanced & Expert Users | 4 | 05-22-2007 01:29 AM |
| extracting recursive data file | bbeugie | UNIX for Dummies Questions & Answers | 1 | 06-16-2004 02:48 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#8
|
|||
|
|||
|
Quote:
Code:
strcpy(filename, argv[1]); |
| Forum Sponsor | ||
|
|
|
#9
|
|||
|
|||
|
Here's another approach but it's not really pretty.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#define BSZ 256
#define MAXLINES 5000
#define READLEN 12
int *retData(int num, int offs, char *data, int delim, void *ptr) {
int *retarr = ptr;
char *read_d;
if (offs >= num) {return retarr;}
if (retarr == NULL) {retarr = malloc(num * sizeof(int)); bzero(retarr,num);}
if ( (read_d = index(data,delim)) == NULL) {
sscanf(data,"%d",&retarr[offs]);
return retarr;
} else {
sscanf(data,"%d",&retarr[offs]);
data = read_d + 1;
}
printf("Debug: Returning data at %p = %d at iteration %d of %d\nData = %p = %s\n",&retarr[offs],retarr[offs],offs,num,data,data);
return retData(num,(offs + 1),data,delim,(void *)retarr);
}
int main(int argc, char **argv) {
int x = 0, n, g;
char buf[BSZ];
int *arr[MAXLINES];
FILE *fpt;
if (argc != 2) {printf("Please specify a filename.\n"); return 1;}
if ( (fpt = fopen(argv[1],"r")) == NULL) {
printf("Could not access filename %s.\n",argv[1]);
return 1;
}
while (fgets(buf,BSZ,fpt) != NULL) {
arr[x++] = retData(READLEN,0,buf,',',NULL);
printf("Node pointer at %p and returned pointer = %p\n",&arr[x],arr[x]);
if (x == (MAXLINES - 1)) {break;}
}
fclose(fpt);
for (n = 0 ; n < x ; n++) {
printf("Array node pointer at %p.\n",&arr[n]);
for (g = 0 ; g < READLEN ; g++) {
printf("Values stored at %p %d = %d\n",&arr[n],n,arr[n][g]);
}
free(arr[n]);
}
return 0;
}
|
|
#10
|
|||
|
|||
|
Hi,
I managed to get them corrected and its running. Thanks alot. However, I have another question; given that I need to generate alot of these test files. I have a fopen function in my separate main code which write numbers to these file. But I wanted to differentiate these file number. Do you aware how could we insert number variable in fopen command? Assume that I have a counter of size 100 whether each round i need to execute below. e.g Code:
if((cfPtr = fopen("test%d.txt","a+"))==NULL)
printf("File could not be opened\n");
else fprintf(cfPtr, "%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d \n", a,b,c,d...);
e.g test1, test2,test3.txt...etc. for each different counter counts. Please advise. |
|
#11
|
|||
|
|||
|
Use sprintf or strcat and a counter.
Code:
for (i=0 ; i < filecnt ; i++) {
sprintf(buf,"%s%d.txt",basefilename,i);
filearr[i] = fopen(buf,"r");
}
|
|||
| Google The UNIX and Linux Forums |