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.

12
  • Check this out stackoverflow.com/a/3364548/1135469 Commented May 6, 2019 at 23:36
  • One of many problems: while(!feof(fp)) always wrong Commented May 6, 2019 at 23:37
  • What should I be using? Commented May 6, 2019 at 23:39
  • Look closely at buffarray++=token;. Is this really what you want? Commented May 6, 2019 at 23:39
  • 1
    If all you want is to read all of the characters into one large array, then you should use fread() instead of fgets(). Commented May 6, 2019 at 23:55

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Note: alternative to #define PI (4*atan2(1,1)) --> acos(-1).
@chux: yes indeed. I wonder why the Standard does not mandate a definition in <math.h> to the best approximation with the target floating point representation. PI is not used in the posted code anyway :)
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: 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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.