1

I have a text file that has the following lines:

(0,0) -180.000  77.500  -999.000  -999.000  -999.000  2740.831  45.000  -0.001  -0.001  0.000 458.138 45.000  -999.000
(1,0) -179.500  77.500  -999.000  -999.000  -999.000  2740.831  45.000  -0.001  -0.001  0.000 458.138 45.000  -999.000
(2,0) -179.000  77.500  -999.000  -999.000  -999.000  2740.831  45.000  -0.001  -0.001  0.000 458.138 45.000  -999.000
(3,0) -178.500  77.500  -999.000  -999.000  -999.000  2740.831  45.000  -0.001  -0.001  0.000 458.138 45.000  -999.000
...
...
(359,0) -0.500  77.500  -999.000  -999.000  -999.000  2740.831  45.000  -0.001  -0.001  0.000 458.138 45.000  -999.000

I am trying to put each line of this text file (buf) into an individual element of an array (buffarray) using the following program:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#define PI 4*atan2(1,1)

int main(int argc, char *argv[]) {
    FILE *fp;
    char buf[200];
    char *token;
    char buffarray[223920];
    char filename[150];
    int i, j, k;

    sscanf(argv[1], "%s", filename);

    if ((fp = fopen(filename, "rt")) == NULL) {
        printf("Failed in fopen: %s\n", filename);
        return -1;
    }

    while (!feof(fp)) {
        fgets(buf, 200, fp);
        token = buf;
        printf("buf is %s\n", buf);     
        buffarray++ = token;
    }
}

How every when compiling this program I get an error message:

translate_ww3file.c: In function ‘int main(int, char**)’:
translate_ww3file.c:30:12: error: lvalue required as increment operand
   buffarray++ = token;
        ^

How do I resolve this issue? I ideally want to create another text file where the lines are rearranged so that lines 180 to 359 from the original text are printed first in the new text file and then lines 1 to 179 are printing out afterwards in the new text file.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
jms1980
  • 1,017
  • 1
  • 21
  • 37

1 Answers1

3

Multiple problems:

  • the PI macro is not properly parenthesized. It should be #define PI (4*atan2(1,1))
  • while (!feof(fp)) is always wrong. Use while (fgets(buf, 200, fp)) instead.
  • You cannot increment an array, you want to concatenate the string at the end of the array with strcat(buffarray, token); but you must initialize buffarray[0] to '\0' before the loop.

Here is a corrected version:

#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PI  (4*atan2(1,1))

int main(int argc, char *argv[]) {
    FILE *fp;
    char buf[200];
    char *token;
    char buffarray[223920];
    char filename[150];

    if (argc < 2 || sscanf(argv[1], "%149s", filename) != 1) {
        printf("missing command line argument\n");
        return 1;
    }

    if ((fp = fopen(filename, "rt")) == NULL) {
        printf("Failed in fopen %s: %s\n", filename, strerror(errno));
        return 1;
    }

    *buffarray = '\0';
    while (fgets(buf, sizeof buf, fp)) {
        token = buf;
        printf("buf is %s\n", buf);     
        strcat(buffarray, token);
    }
    fclose(fp);

    printf("file contents:\n);
    fputs(buffarray, stdout);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 1
    Note: alternative to `#define PI (4*atan2(1,1))` --> `acos(-1)`. – chux - Reinstate Monica May 07 '19 at 02:24
  • 1
    @chux: yes indeed. I wonder why the Standard does not mandate a definition in `` to the best approximation with the target floating point representation. `PI` is not used in the posted code anyway :) – chqrlie May 07 '19 at 08:07
  • When I make the array in the above program. How can I have it set up so I can print out buffarray[i] for example. If I want to print out one element of the array how do I do it? – jms1980 May 08 '19 at 18:48
  • @jms1980: if your goal is to parse a text file, converting the text to numbers, the method is inappropriate. You probably should use `sscanf()` to convert the line into a set of values that you would store in an array of structures. Can you clarify the problem? – chqrlie May 08 '19 at 18:57