GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 24-Sep-2005, 21:22
monalin monalin is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
monalin is on a distinguished road

error C2106: '=' : left operand must be l-value


I'm fairly new to programing and i dont really understand what i'm doing wrong to get this error i'm sure its something to do with my syntax or struct if you guys could point out what i did and why it doesnt work that would rock thanks

**edit**
forgot to add i'm getting the "error C2106: '=' : left operand must be l-value" on every time i attempt to use my grid_entries struct.

CPP / C++ / C Code:
// This is the main project file for VC++ application project 
// generated using an Application Wizard.

#include "stdafx.h"

#using <mscorlib.dll>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

struct grid_entries {
    char gridid[64];
    char zoneid[64];
    char number[64];
    char x[64];
    char y[64];
    char z[64];
    char heading[64];
	char pause[64];
};
//typedef struct grid_entries grid_entries;

int main() {
	int x=0;
    char myinput[64];
    ifstream inFile;
	grid_entries grid;
    
    inFile.open("test.txt");
    if (!inFile) {
        cout << "Unable to open file";
        exit(1); // terminate with error
    }
    
    while (inFile >> myinput) {
        cout << myinput << endl;
		for(x=0;x<8;x++)
		{
			switch (x) {
				case 0:
					  grid.gridid = myinput;
					break;
				case 1:
					  grid.zoneid = myinput;
					break;
				case 2:
					  grid.number = myinput;
					break;
				case 3:
					  grid.x = myinput;
					break;
				case 4:
					  grid.y = myinput;
					break;
				case 5:
					  grid.z = myinput;
					break;
				case 6:
					  grid.heading = myinput;
					break;
				case 7:
					  grid.pause = myinput;
					break;
			}
		}

    }
    
    inFile.close(); 
	cin >> x;
    return 0;
}

thanks a bunch this is the only file, i'm trying to read .txt file that i exported my SQL db too in order to reformat it into another table and design. Its complicated but i cant convert it useing mysql commands... anyways that doesnt matter.
Last edited by admin : 24-Sep-2005 at 23:54. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 24-Sep-2005, 21:27
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: error C2106: '=' : left operand must be l-value


Code:
char x[64]; char y[64]; char z[64];
These are your problem...
There is no operator = defined for char arrays.
I would suggest a move to std::strings as they have an = operator defined and they have the benefit of automatic memory management.
  #3  
Old 24-Sep-2005, 22:08
monalin monalin is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
monalin is on a distinguished road

Re: error C2106: '=' : left operand must be l-value


ok well changing that worked but now i dont know how to save it to a file. I need to take the data and save it. The only way i know how to save data to a file is useing outFile << ..... ; format like i show below. Are there other ways or is there a way to convert the std::string into something i can export like this. the std::string class doesnt have the << operand.

CPP / C++ / C Code:
		outFile << "gridid: " << grid.gridid << endl;
		outFile << "zoneid: " << grid.zoneid << endl;
		outFile << "number: " << grid.number << endl;
		outFile << "x: " << grid.x << endl;
		outFile << "y: " << grid.y << endl;
		outFile << "z: " << grid.z << endl;
		outFile << "heading: " << grid.heading << endl;
		outFile << "pause: " << grid.pause << endl;

*********edit*******
nevermind dont bother answering i found a tutorial on useing a different form of I/O that should be good enough to get me started
  #4  
Old 24-Sep-2005, 23:04
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: error C2106: '=' : left operand must be l-value


hi,
i suggest you to use write function.
this code segment shows u how to write and retrieve an object from a file..
CPP / C++ / C Code:
fstream fs("test.txt",ios::out|ios::app); //create fstream as per ur requirement
fs.write((char *)str,sizeof(str));           //where str is the std::string
fs.close();                                       //close the connection.
fs.open("test.txt",ios::in);                  //create fstream for input
fs.read((char *)str,sizeof(str));           //read the string
fs.close();                                       //close the connection
i hope the code is correct.
bye
  #5  
Old 24-Sep-2005, 23:54
monalin monalin is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
monalin is on a distinguished road

Re: error C2106: '=' : left operand must be l-value


I'm sure that your code works, i've got a lot of information to read so i think that this other method i found may work better. But thats only if i can get it to work properly. I found a tutorial here on how to use this stuff but... i dont think i quite understand how to use the fprintf() function. Also I dont understand what the difference between %s or %d is i assume it has to do with the variable type but if thats the case i dont know what each one means so its of no use to me.

Anyways i compile my program and get no erros. But when i run it it tells me theres a problem and asks me to run a debugger. So i ran one and i gave u the output that the debugger gave me.

Code:
'grid_converter.exe': Loaded 'C:\Documents and Settings\Brian\My Documents\Visual Studio Projects\grid_converter\Debug\grid_converter.exe', Symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\system32\mscoree.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\system32\shlwapi.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\system32\user32.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorwks.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\msvcr71.dll', Symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\fusion.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\system32\shell32.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.2180_x-ww_a84f1ff9\comctl32.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\system32\comctl32.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorlib.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\assembly\NativeImages1_v1.1.4322\mscorlib\1.0.5000.0__b77a5c561934e089_ac28692c\mscorlib.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorsn.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\mscorjit.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\diasymreader.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\WINDOWS\system32\ole32.dll', No symbols loaded. 'grid_converter.exe': Loaded 'c:\windows\microsoft.net\framework\v1.1.4322\mscorlib.dll', No symbols loaded. 'grid_converter.exe': Loaded 'C:\Documents and Settings\Brian\My Documents\Visual Studio Projects\grid_converter\Debug\grid_converter.exe', Symbols loaded. The thread 'Win32 Thread' (0x2cc) has exited with code 0 (0x0). An unhandled exception of type 'System.NullReferenceException' occurred in grid_converter.exe Additional information: Object reference not set to an instance of an object. The thread 'Win32 Thread' (0xb58) has exited with code -1073741819 (0xc0000005). The program '[3236] grid_converter.exe' has exited with code 0 (0x0). The program '[3236] grid_converter.exe: Native' has exited with code 0 (0x0).

Heres the source code that i'm useing. I'm trying to read a file and save it to another file as i've said up above. All i'm trying to do is change the format that its written. I've limited my error to the while loop at the bottom of my code, because i can change the value of result and dont get the error, which means the error occurs somewhere inside that loop cause the error only happens when the program steps inside the while loop.

Other things to note, text.txt has 7 fields of data and grid_entries.sql is being created when i run the program without any information in it.

CPP / C++ / C Code:
// Include all the header files needed
#include "stdafx.h"

#using <mscorlib.dll>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

/*************************************************************************************************************************
/ STRUCT NAME: grid_entries
/ PURPOSE: to store data we read from the sql file and save it in a different format.
/*************************************************************************************************************************/
struct grid_entries {
    std::string gridid;
    std::string zoneid;
    std::string number;
    std::string x;
    std::string y;
    std::string z;
    std::string heading;
	std::string pause;
};
/*******************************************MAIN FUNCTION*****************************************************************/
int main() {
	int x=0,
	    result,
	    count=0;
    FILE *inFile,
          *outFile;
	grid_entries grid;
	char *fileopen = "test.txt",			//The file that we will read
	      *filesave = "grid_entries.sql";	//The file the data is being saved to. Will be in sql format.
    
	inFile = fopen(fileopen,"rb");//Open test.txt
	if (inFile == NULL)//Make sure there are no errors
	 {
		perror("Can't open file");//Report if any errors were found.
		exit(0);
	}
 	outFile = fopen(filesave,"wt");//Open grid_entries.sql
	if (outFile == NULL)//Make sure there are no errors
	 {
		perror("Can't open grid_entries.sql");//Report if any errors were found.
		exit(0);
	}

	 result = fscanf(inFile,"%s %s %s %s %s %s %s %s\n", &grid.gridid, &grid.zoneid, &grid.number, &grid.x, &grid.y, &grid.z, &grid.heading, &grid.pause);

  while (result == 7)
    {
		fprintf(outFile,"%2d. %s %s %s %s %s %s %s %s\n",++count,grid.gridid,grid.zoneid,grid.number,grid.x,grid.y,grid.z,grid.heading,grid.pause);
		result = fscanf(inFile,"%s %s %s %s %s %s %s %s\n", &grid.gridid, &grid.zoneid, &grid.number, &grid.x, &grid.y, &grid.z, &grid.heading, &grid.pause);
    }

	fclose(inFile);
	fclose(outFile);
    return 0;
  #6  
Old 25-Sep-2005, 03:02
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: error C2106: '=' : left operand must be l-value


hi,
the details of the fprintf function is below:

FUNCTION:
Print formatted data to a stream.
CPP / C++ / C Code:
int fprintf( 
   FILE *stream,
   const char *format [,
   argument ]...
);
stream
Pointer to FILE structure.
format
Format-control string.
argument
Optional arguments.
Return Value
fprintf returns the number of bytes written. fwprintf returns the number of wide characters written. Each of these functions returns a negative value instead when an output error occurs.

The fprintf formats and prints a series of characters and values to the output stream. Each function argument (if any) is converted and output according to the corresponding format specification in format. For fprintf, the format argument has the same syntax and use that it has in printf.

and %d specifies integer argument...
%s specifies string argument...
for example
CPP / C++ / C Code:
scanf("%d",&a);         //This reads the input as an integer;
scanf("%s",&s);          //This reads the input as a string;
  #7  
Old 25-Sep-2005, 08:07
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: error C2106: '=' : left operand must be l-value


Quote:
Originally Posted by monalin
Are there other ways or is there a way to convert the std::string into something i can export like this. the std::string class doesnt have the << operand.

1. Of course it does. See the example below. The things that won't work with std::string objects are scanf and printf (they require C-style strings).

Quote:
Originally Posted by monalin
*********edit*******
nevermind dont bother answering i found a tutorial

2. I am glad you found a tutorial, but some others reading this thread may benefit from knowing that C++ std::string objects are actually pretty easy to use, and are realllllllly better than C-style strings for many (most?) C++ applications. Here's a link to a pretty good series of tutorials, including one for the std::string class C++ Standard Template Library Tutorial

You have to understand that the std::string class is not part of the C++ language itself, so lots of functions that need "string" arguments actually require C-style strings (null-terminated sequence of chars). You need to have at least a nodding acquaintance with arrays of chars (and pointers to chars) to really understand what's going on, but basic operations (assignment, concatenation, input and output) are overloaded for std::strings to make them useful.

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

struct grid_entries {
  std::string gridid;
  std::string zoneid;
};


int main() 
{
  grid_entries grid;

  int count=0;

  std::string inName("test.txt");
  std::string outName("grid_entries.sql");

  ifstream inFile(inName.c_str());

  ofstream outFile(outName.c_str());

  if (!inFile) {
    perror("Can't open input file");
    exit(0);
  }

  if (!outFile) {
    perror("Can't open output file");
    exit(0);
  }


  inFile >> grid.gridid >> grid.zoneid;

  while (inFile) {
     outFile << " " << ++count << ". " << grid.gridid << " " 
             << grid.zoneid << endl;

    inFile >> grid.gridid >> grid.zoneid;
  }

  return 0;
}

<edit>Note that you should #include <string> in the above example and you don't need <iomanip> for the stuff shown here.</edit>

Regards,

Dave
Last edited by davekw7x : 25-Sep-2005 at 08:40. Reason: I forgot to #include <string>
  #8  
Old 25-Sep-2005, 08:33
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: error C2106: '=' : left operand must be l-value


I may be missing it, but you will want to include <string> as well
when using std::string...
  #9  
Old 25-Sep-2005, 08:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: error C2106: '=' : left operand must be l-value


Quote:
Originally Posted by L7Sqr
I may be missing it, but you will want to include <string> as well
when using std::string...

You are, of course, correct. I try really hard to submit only code that is Standards-compliant, and compiles and executes with at least three compilers, including Microsoft. I forgot to put the #include<string> in the example.
(It actually compiled OK with Borland and GNU compilers, but that's not Standard --- or maybe it's above and beyond Standard???).

Thanks,

Dave
  #10  
Old 25-Sep-2005, 08:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: error C2106: '=' : left operand must be l-value


Quote:
Originally Posted by Paramesh
i suggest you to use write function.
Why? You could use write() for output, but why would you want to do that for this case (simple text)? If you do use write() for std::strings, sizeof() is not what you want. (Try it: the sizeof() a std::string is the size of the object, which includes other information in addition to the actual array of chars that you want to print.)

Furthermore, if you declare a std::string variable, how would you use read() to get something from the input stream? (How many bytes do you want to read? sizeof() certainly doesn't give what you want.)

Quote:
Originally Posted by Paramesh
i hope the code is correct.

Sorry, it isn't. (Did you try it?)

Regards,

Dave
 
 

Recent GIDBlogVista ?Widgets? on Windows XP by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Firework pops up corazon Web Design Forum 0 19-May-2005 05:18
Quick, Insertion, and Partition silicon C++ Forum 0 18-May-2005 20:49
Merge and Heap...which is really faster silicon C++ Forum 0 10-May-2005 13:46
Insertion Sort Problem silicon C++ Forum 2 08-May-2005 16:13
Sorting Algorithms by Time silicon C++ Forum 4 02-May-2005 21:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:17.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.