I have some text like
T_411_A/T_414_B,T_412_A/T_415_A
in which every one of them i.e T_411_A, T_414_B so on are all defined as macro's as follows:
#define T_411_A 0x2341
every one of them has a unique value. 0x2341 is a unique and the max size of the value which macro may consist of is 4 bytes.
in the above example i need to read those T_411_A or any other macro's from console window as then get the value of the macro.
example: if i give T_411_A in the command prompt then i should get the value of that 0x2341 according to the above example.
Note: this is C code where i am working on visual studio 2008.
Loading
VulpesPosted Nov 9, 2011, 9:42 AM
Sam HobbsPosted Dec 28, 2011, 9:15 PM
Sam HobbsPosted Dec 28, 2011, 8:58 PM
For what it is worth, the reason I get frustrated is because Karthik's initial questions are seldom clear and then Karthik and Vulpes go through many discussions and the problem changes; the problem often becomes something entirely different. I know I can help but Karhik does not explain things. I try to help anyway but Karthik won't explain what Karthik needs well enough for me to help.
It would be one thing if Karthik understood what I suggest and chooses not to do it. It is frustrating for me when Karthik does not understand what I am saying and does not try to understand.
Karthik AgarwalPosted Dec 28, 2011, 8:21 AM
Sam HobbsPosted Nov 11, 2011, 1:26 PM
Sam HobbsPosted Nov 11, 2011, 5:17 AM
Sam HobbsPosted Nov 11, 2011, 5:12 AM
#define T_411_A 0x2341
#define T_414_B 0x2342
#define T_412_A 0x2343
static const char Filename[] = "Data.txt";
class macroitemsmap : public std::map
void ProcessRecord(std::string &line, macroitemsmap ¯oitems) {
std::string::const_iterator at_i(line.begin());
std::string::const_iterator begin_i;
std::string token;
if (line.length() == 0)
return;
while (at_i != line.end()) {
begin_i = at_i;
for (; at_i != line.end() && *at_i != '/' && *at_i != ','; ++at_i);
if (at_i != begin_i) {
token.assign(begin_i, at_i);
std::cout << token << ' ' << macroitems[token] << '\n';
}
if (at_i != line.end())
++at_i;// past the token
}
}
int _tmain(int argc, _TCHAR* argv[])
{
std::ifstream stream;
std::string line;
macroitemsmap macroitems;
macroitemsmap::iterator Iterator;
macroitems["T_411_A"] = T_411_A;
macroitems["T_414_B"] = T_414_B;
macroitems["T_412_A"] = T_412_A;
//for (Iterator=macroitems.begin(); Iterator!=macroitems.end(); ++Iterator)
//std::cout << Iterator->first << ' ' << Iterator->second << '\n';
stream.open(Filename);
if (!stream.is_open()) {
std::cout << "Open error\n";
return 0;
}
while (getline(stream, line))
ProcessRecord(line, macroitems);
stream.close();
return 0;
}
Sam HobbsPosted Nov 10, 2011, 5:16 PM
// http://www.c-sharpcorner.com/Forums/Thread/147992/how-to-read-from-console-window-in-visual-studio.aspx
#define T_411_A 0x2341
#define T_414_B 0x2342
#define T_412_A 0x2343
static const char Filename[] = "Data.txt";
struct macroitem {
char *id;
int value;
};
int compare(char **arg1, char **arg2)
{
macroitem *pitem1 = (macroitem *)arg1;
macroitem *pitem2 = (macroitem *)arg2;
return _strcmpi( *arg1, *arg2 );
}
void ProcessRecord(char *line, macroitem *macroitems, int itemcount) {
char seps[] = "/,";
char *token = NULL;
char *next_token = NULL;
macroitem *pmacroitem;
void *p;
int n;
n = strlen(line);
if (n == 0)
return;
// usually there will be a newline at the end
if (line[n-1] == '\n')
line[n-1] = 0;
puts(line);
token = strtok_s(line, seps, &next_token);
while ((token != NULL)) {
p = bsearch(&token, (char *)macroitems, itemcount,
sizeof(macroitem), (int (*)(const void*, const void*))compare);
if (p == NULL)
printf("\t%s not found\n", token);
else {
pmacroitem = (macroitem *)p;
printf("\t%s %s %i\n", token, pmacroitem->id, pmacroitem->value);
}
token = strtok_s(NULL, seps, &next_token);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
FILE *stream;
char line[100];
macroitem macroitems[]= {
{"T_411_A", T_411_A},
{"T_414_B", T_414_B},
{"T_412_A", T_412_A}
};
int itemcount = sizeof(macroitems)/sizeof(macroitem);
qsort((void *)macroitems, itemcount, sizeof(macroitem), (int (*)(const void*, const void*))compare);
//for (int i=0; i
if (fopen_s(&stream, Filename, "r") != 0 ) {
puts("Open error");
return 0;
}
fgets(line, 100, stream);
while (!ferror(stream) && !feof(stream)) {
ProcessRecord(line, macroitems, itemcount);
fgets(line, 100, stream);
}
fclose(stream);
return 0;
}
Sam HobbsPosted Nov 10, 2011, 1:44 PM
Another possibility is to write a macro that generates the additional code, and then you could do a one-time conversion and then in the future when an id is added then there would be only one line that needed changing. I assume that that is not clear, so I will show you an example later.
Karthik AgarwalPosted Nov 10, 2011, 7:18 AM
VulpesPosted Nov 10, 2011, 6:57 AM
Karthik AgarwalPosted Nov 10, 2011, 6:25 AM
VulpesPosted Nov 10, 2011, 6:10 AM
Karthik AgarwalPosted Nov 10, 2011, 5:54 AM
What i want know exactly is that?
Can i read the name of the macro name from the console window and the get the value of the same defined in the code? As of knowledge i think it's not possible. Sam tell me straight away tell me whether it can be done or not?
Sam HobbsPosted Nov 10, 2011, 5:45 AM
char *id;
int value;
}
struct macroitem macroitems[3];
macroitems[0].id = "T_411_A";
macroitems[0].value = T_411_A;
macroitems[1].id = "T_414_B";
macroitems[1].value = T_414_B;
macroitems[2].id = "T_412_A";
macroitems[2].value = T_412_A;
If it is possible to build an array that maps your ids (macro) to numbers (macro value), then it is possible to sort using qsort. I can show you how to do that but not right now. Then you can read each line in and separate the text that is delimited by a slash or comma and then do a lookup of each id (macro) using bsearch. I can show you how to do that too but not right now.
Karthik AgarwalPosted Nov 10, 2011, 5:13 AM
If the id's are just separated by slash then they mean that the corresponding strings to those will be concatenated in the sequence which id's are present. if they are just separated by comma's then they mean that they are individual strings which can selected from by user from the drop down. But it can combination of both slash and comma.
Anyways i guess there isn't a solution for the question i asked, as macro's will be replaced with their respective value a compile time. Now what i wanted was reading the name of the macro at run time from console window and get it's respective value. Is that possible Sam? If not probably we should end this post here.
Sam HobbsPosted Nov 10, 2011, 4:32 AM
I know very well that software sometimes gets very large and that management does not want to invest the time to re-write it.
I see that your data consists of two pairs and between the pairs is a comma and between the ids in the pairs is a slash; so in other words:
id/id,id/id
So what is the significance of the slash and what is the significance of the comma? If there is no significance other than being a delimiter between ids then an explanation is not so important but if the ids are to be considered as pairs then that affects the logic.
Also note that Vulpes said "I'm assuming here that the total number of macros is so small that access speed would not be an issue". You should have answered the implied question; how many of these ids (macros) are there? Not in the data itself; how many unique values, such as how many #defines are there.
Karthik AgarwalPosted Nov 10, 2011, 12:58 AM
If you remember one day when you posted about the usage of the void pointers, i said that if you guide me i am ready to take up the challenge. If i won't have trust on you i would not have said that word. And one more thing is, if you ask me to change the whole structure i am following and change the language from c to c++ which i can never ever do as it's being very early stage for me to propose something new being a fresher.
Sam HobbsPosted Nov 9, 2011, 9:30 PM
Also see My Profile in the MSDN forums; most of the "answers" I provided there was for C++.
I say that because you seem to think I can't help. It is very frustrating for me to not be able to help.
The first Windows program I wrote was a device driver (back in the days before 32-bit Windows existed) that sent data from a SCSI host adapter to a HP LaserJet printer. Most of it was written in C, except I used assembler to rotate a bitmap. I could have gotten rich from that but about three months after I started, HP introduced the LaserJet IV and that made our software unnecessary. My program definitely did some bit-twiddling (working at the bit level) to send the data.
A few years ago I wrote an application that uses Detours to hook into an accounting package so management could audit what their employees were doing. I could have gotten rich from that except the person that I was working with at the time is a fool (I could use other terms but there is no need for that here). I used at least one technique that the CodeGuru C++ experts considered to be too difficult to do.
I can do C and C++. I can do bit-twiddling. I can work with pointers when appropriate. I know that it is better to avoid pointers. The one thing I don't know how to do is to explain to you that you don't need to do the things you are doing.
Please explain why you cannot use C++ to do what you need to do to read the data. C++ has classes to make it easier. If there is a good reason why you cannot use C++ then we can use C, but I don't see a reason why.
VulpesPosted Nov 9, 2011, 11:34 AM
if (index == -1)
VulpesPosted Nov 9, 2011, 10:12 AM
http://www.google.co.uk/#hl=en&sugexp=ppwl&cp=4&gs_id=e&xhr=t&q=c+tutorial&pf=p&sclient=psy-ab&site=&source=hp&pbx=1&oq=c+tu&aq=0&aqi=g4&aql=&gs_sm=&gs_upl=&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=b037f854561b5e39&biw=1366&bih=643
Karthik AgarwalPosted Nov 9, 2011, 9:46 AM
Also refer me a site where i can learn pointers may be whole C also will do the job. These days i lost control on whole C.
Also tell me how to read a string from console window and convert to UINT32 as well. i will try that out.