0

Still in the fiddling stage with C. Not sure what I'm doing wrong here but I think it has to do with pointers. Anyway. I'm trying to read in and parse a tab-delimited text file into a 2D array. The actual array is 6109 x 14, I allotted a bigger pointer array.

FILE *in = fopen("afile","rt");
// read the first line from the file
char line[2000];
int x=0;
int y=0;
char *result[7000][14];
char *curResult;
char delim[]= "\t";

while (fgets(line, 2000, in) != NULL) {
    printf("LINE IS %s\n",line);
    curResult=NULL;
    curResult = strtok(line,delim);
    y=0;
    while( curResult != NULL ) {
        result[x][y]= curResult;
        curResult = strtok( NULL, delim );
        y++;
    }
    x++;
}

//print a random line to check 
for(int i=0; i<10; i++)
    printf("%s\n", result[50][i]);

The line I printed at the bottom which should be the 50th line prints the last line of the text file. I tried this with different numbers instead of 50 but they all print the same thing.

EDIT: afile looks like this:

  instance_id   batch_id    cmap_name   INN1    concentration (M)   duration (h)    cell2   array3  perturbation_scan_id    vehicle_scan_id4    scanner vehicle vendor  catalog_number  catalog_name
1   1   metformin   INN 0.00001 6   MCF7    HG-U133A    EC2003090503AA  EC2003090502AA  HP GeneArray Scanner    medium  Sigma-Aldrich   D5035   "1,1-dimethylbiguanide hydrochloride"
2   1   metformin   INN 0.00001 6   MCF7    HG-U133A    EC2003090504AA  EC2003090502AA  HP GeneArray Scanner    medium  Sigma-Aldrich   D5035   "1,1-dimethylbiguanide hydrochloride"
3   1   metformin   INN 0.0000001   6   MCF7    HG-U133A    EC2003090505AA  EC2003090502AA  HP GeneArray Scanner    medium  Sigma-Aldrich   D5035   "1,1-dimethylbiguanide hydrochloride"
4   1   metformin   INN 0.001   6   MCF7    HG-U133A    EC2003090506AA  EC2003090502AA  HP GeneArray Scanner    medium  Sigma-Aldrich   D5035   "1,1-dimethylbiguanide hydrochloride"
21  2   phenformin  INN 0.00001 6   MCF7    HG-U133A    EC2003091104AA  EC2003091102AA  HP GeneArray Scanner    medium  Sigma-Aldrich   P7045   phenformin hydrochloride
22  2   phenyl biguanide        0.00001 6   MCF7    HG-U133A    EC2003091105AA  EC2003091102AA  HP GeneArray Scanner    medium  Sigma-Aldrich   P19906  1-phenylbiguanide hydrochloride
23  2   valproic acid   INN 0.001   6   MCF7    HG-U133A    EC2003091106AA  EC2003091102AA  HP GeneArray Scanner    medium  Sigma-Aldrich   P4543   2-propylpentanoic acid
61  2a  metformin   INN 0.00001 6   MCF7    HG-U133A    EC2003091103AA  EC2003091102AA  HP GeneArray Scanner    medium  Sigma-Aldrich   D5035   "1,1-dimethylbiguanide hydrochloride"
4

1 回答 1

1

Imagine line is a big box where you can put a bunch of letters.
That's what you do with fgets().

Next you point to specific letters inside that box.

Next you replace the contents of the box (another fgets() to the same variable line) and the pointers, of course still point to the same box, but now to the new letters.

You need to copy the characters (with strcpy(), check that you have enough space first) somewhere inside your loop.

于 2012-04-17T17:59:58.067 回答