3 Replies - 282 Views - Last Post: 31 May 2012 - 02:59 PM Rate Topic: -----

#1 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Error while writing values into a file

Posted 31 May 2012 - 02:26 PM

I have a pretty simple program that changes every plus sign with a minus, and every minus with a plus, and then stores it into a file. But I'm getting the following error in my code: "error: lvalue required as increment operand"
I'm getting that error in my increment part, namely: twidder++

Below you can find my simple program:

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

int main(void)
{
    static const char filename[] = "twidder_reverse.txt";
    char twidder[] = "+-+-";

    while(*twidder != '\0')
    {
        if(*twidder == '+')
        {
            *twidder = '-';
        }
        else if(*twidder == '-')
        {
            *twidder = '+';
        }

        twidder++;
    }

    FILE *file = fopen(filename, "w");
    if(file != NULL)
    {
        fwrite(twidder, 1, sizeof(twidder), file);
        fclose(file);
    }
    else
    {
        fprintf(stderr, "Couldn't open the file twidder_reverse.txt!");
        return 1;
    }

    return 0;
}



Thanks in advance.

This post has been edited by erkant: 31 May 2012 - 02:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Error while writing values into a file

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Error while writing values into a file

Posted 31 May 2012 - 02:33 PM

You aren't allowed to use an arrayname as an lvalue, since that would let you assign it to a different memory region. An arrayname, once declared, is "married" to the memory space that is allocated to it.

If you want to use pointer arithmetic, declare a pointer and initialize it to the array address, e.g.:
char *twPtr = twidder;


Now you can do whatever you want with twPtr.
Was This Post Helpful? 2
  • +
  • -

#3 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Re: Error while writing values into a file

Posted 31 May 2012 - 02:35 PM

Thanks, it solved my problem!
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Error while writing values into a file

Posted 31 May 2012 - 02:59 PM

You're welcome.

By the way, lvalue is a term used to describe an expression that can be used on the left side of the = operator. So basically it's a variable name that can serve as the object (i.e., target) of an assignment statement.

I tend to assume that's obvious to everyone, but maybe it's not.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1