reading a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • c language

    #1

    reading a file

    Hello everybody,

    I have to do some calculations on two different files with C program.
    First file has 2 columns and second file has 100 columns. The program
    should get the columns of the first file and 2 columns of the second
    file and do some calculation on different rows of those files. It
    should repeat this action for all the columns of the second file which
    it needs to be repeated 50 times for all 50 pairs columns of the second
    file.
    I do not have any problem for the calculation part but I am not sure
    how I can write a program to go and read the columns of a file one by
    one.
    Is there any body to give me some clues?

    Thanks,
    MJK

  • Michael Mair

    #2
    Re: reading a file

    c language schrieb:[color=blue]
    > I have to do some calculations on two different files with C program.
    > First file has 2 columns and second file has 100 columns. The program
    > should get the columns of the first file and 2 columns of the second
    > file and do some calculation on different rows of those files. It
    > should repeat this action for all the columns of the second file which
    > it needs to be repeated 50 times for all 50 pairs columns of the second
    > file.
    > I do not have any problem for the calculation part but I am not sure
    > how I can write a program to go and read the columns of a file one by
    > one.
    > Is there any body to give me some clues?[/color]

    Yep. Read the files line by line[*] and read numbers using
    strtod(). Depending on whether you aim for less memory use
    or less overall time, (1) discard unnecessary numbers or (2)store
    all columns. Make sure that even if you have read 100 numbers
    there is no 101st left on the line.

    Consider starting like this (or similar) and write the missing
    functions.

    /*
    return 0 on success
    pair: there is a 0th pair
    max: number of expected column entries; pass 0 to say you
    don't care
    */
    int GetColumnPair(F ILE *pFile, size_t pair, size_t max,
    double (**ppCols)[2], size_t *plen)
    {
    size_t clen = 0, buflen = 0;
    char *buf = 0;

    if (NULL == ppCols) {
    return -1;
    }

    clen = 0;

    while (EOF != ReadLine(fp, &buf, &buflen))
    {
    if (clen >= *plen) {
    /* realloc ppCols, adjust plen */
    }
    if (GetTwoNumbers( buf, 2*pair, max, *pCols)) {
    DiscardLineBuff er(&buf);
    return clen<INTMAX?cle n:-2;
    }
    clen++;
    DiscardLine(&bu f, &buflen);
    }

    DiscardLineBuff er(&buf);

    return 0;
    }
    [*]There are many, many, many threads in the comp.lang.c archives
    dealing with that. Consider using fgets() or the public domain fggets().
    DiscardLine() may or may not be necessary.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.

    Comment

    Working...