|
Message Closed
modified 15-Feb-13 1:10am.
|
|
|
|
|
If there is still a problem in this code then you need to run it through your debugger and isolate the point of failure. We cannot guess what is happening without more information.
|
|
|
|
|
|
|
Hi,
I'm hitting a snag as I try to read back an array which has been written to a text file. Here's the relevant part of the code:
if((fp=fopen("myfile","r"))==NULL)
{
printf("Unable to open file.\n");
exit(1);
}
for(j=0;j<6;j++)
{
for(l=0;l<7;l++)
{
k=fgetc(fp);
if(k==",") break;
str2[j][l]=k;
}
}
fclose(fp);
for(j=0;j<6;j++)
{
for (l=0;l<7;l++)
{
printf("%c", str2[j][l]);
}
}
This is generating an error:
[Warning] comparison between pointer and integer.
Though it runs, it is revealing that the break code which checks for a comma is not functioning as the compiler warns. Why is it claiming this is a pointer? Why is it saying anything about an integer when I've deliberately cast k as a character?
Thanks for any input.
------
Update: The problem was solved by: (a) changing the double quotes to single quotes (apostrophes) and (b) switching
if(k==",") break; below the assignment. In order to match the file derived string with one defined in code I also had to modify it:
str2[j][strlen(str2[j]-1]='\0'; as there is something foreign added in the file transfer process.
Thanks, and if there is a mark as solved button I couldn't find it
modified 14-Feb-13 16:08pm.
|
|
|
|
|
Try this:
k=fgetc(fp);
if(k==',') break;
Hope that will work.
|
|
|
|
|
Hi,
Thanks for the reply.
As far as I can see that's what I have except without the assignment to the array:
str2[j][l]=k
but that is basically what I'm trying to accomplish. The printing at the end is just to confirm that the file was successfully written to the array.
|
|
|
|
|
Replace the double-quotes in your comparison with single-quotes. In C and C++, double-quotes mean a pointer to a string of characters, but single-quotes mean a character literal.
|
|
|
|
|
Hi, thanks for your response. That helped. I also changed the control in the inside printf loop to
for(l=0;*(str2[j]+l);l++)
Now it's compiling with no warnings and is reading back pretty good text, though there is some stray characters after some of the words in the test text.
|
|
|
|
|
Why are you making life so complicated for yourself? Use a tokeniser such as strtok() [^].
|
|
|
|
|
Thanks Richard,
I'm going to try to track down the errors in this code first and then experiment with this tokenizer. At this point I'm trying to get a really granular understanding of C which even means reinventing the wheel at times, just for my own edification
|
|
|
|
|
Just glancing at your code I would suggest using proper names for variables. Also, read up on the difference between a character and a string.
|
|
|
|
|
Thanks.
This is just a test of concept program so I'm not doing 'real' variable names. As a former VB person I'm still in the CCAC (code cuss and cry) phase of learning C
modified 14-Feb-13 16:13pm.
|
|
|
|
|
Jeffrey Webster wrote: At this point I'm trying to get a really granular understanding of C which even means reinventing the wheel at times, just for my own edification A good strategy.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
Assume that there are 8 Modules which one of them is Supervisor. They are implemented in C++ classes, so we have 1 Object of Supervisor and more than one Object of other module.
The Supervisor assign Tasks to each other module, and whenever they finished their tasks or faced with a problem, they should inform the Supervisor.
I think that the best way to implement this behavior is Interruption or something like that.
Now, I want to know how to implement it or is there any solution for this problem?
|
|
|
|
|
Messaging[^] suits this well and is the native Windows way of handling such tasks... but I'm sure there are many options.
|
|
|
|
|
Using interrupts is something you do when your program needs to interact with hardware components that trigger these interrupts. Software can trigger only one type of interrupt, and that is by setting a timer. Your problem is not timed, it is controlled by demand, so setting a timer does not fit that problem. using a timer in this situation is like telling a home owner that instead of reacting to the door bell, he should rather check the door every 5 minutes.
I suggest you fire up your preferred search engine and search for the keywords "master slave pattern". This should provide you with lots of interesting links.
The master slave pattern is extensively used for organizing the parallelization of complex tasks. Each slave works (potentially) on its own thread, allowing it to work in parallel with its coworkers. That means telling a slave to work requires sending a message to a different thread or process. Since inter-process communication is rather slow, you should prefer the former.
In most cases it is not necessary for the slaves to live beyond the scope of their tasks. Therefore, the easiest way is to create a new thread for each slave, whenever you need one. The problem you describe assumes a fixed number of slaves however, implying that these slaves' livetimes are not linked to the tasks they work on.
In that case you need a messaging system that allows the supervisor to inform the slaves if and when they are needed, and the slaves to inform the master after they're done. The windows messaging system - as suggested above - should work well for that purpose.
The disadvantage of messages is that the master needs to keep track which of his slaves are busy. Also he'll need to queue the tasks if all slaves are busy, until one becomes available.
Since you need the ability to queue your tasks anyway, a better solution would be to just push your tasks there, always, and let your slaves pick up work items from there whenever they're idle. That way the master doesn't need to know if or who is available for work. He wouldn't even need to be informed every time a slave is done with his work item; instead, the queue could notify him when it is empty, i. e. the entire workload completed.
The only reason not to do that would be if your slaves are specialized to different tasks, but not able to decide for themselves which these are. (e. g. this may happen when the data associated with the tasks come in different formats that the slaves cannot interpret)
|
|
|
|
|
Hi, you need too Google the "Observer Pattern" to see a very well explained answer to your question. Hope this helps.
|
|
|
|
|
Hi,
I have an idea to develop one MSDEV (VC6) addin that shows the line numbers in editor (like the VS2008 IDE).
Could you please give me the basic idea that I should follow (i mean how to modify the existing editor window)?
Krishnakumar TG
|
|
|
|
|
Krishnakumartg wrote: I have an idea to develop one MSDEV (VC6) addin
Why, oh, why do you want to do an add-in for an antiquated IDE ?
Nihil obstat
|
|
|
|
|
I agree with the previous post... why would you want to develop something new for something so old?
If you just want a project, I'm sure you can find other more useful and productive things to work on. Line numbers are hardly ever useful either, I've never had the need for them (I guess if you're having a need to discuss a file with another coder they'd be useful but I've never needed them).
|
|
|
|
|
Krishnakumartg wrote: Could you please give me the basic idea that I should follow (i mean how to
modify the existing editor window)?
Have you already researched whether that VS supported Addins at all?
If it does then finding examples would be one way to approach it.
|
|
|
|
|
Hi,
I am trying to prepare a test application that combines two windows. I visualize that if one window is dragged, the other one also drags automatically.
For this,
1. I created a dialog based app
2. In button click created a new window (other than dialog apps window). And the new window is attached with the dialog apps window.
But after the button click whole the app hangs. Unable to touch the windows.
The code is as follows. Could you please help?
CWnd NewWindow;
void CAttachDlg::OnButton1()
{
CRect crect( 0, 0, 500, 500 );
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = 100;
rect.bottom = 100;
CString csClassName = AfxRegisterWndClass( 0 );
BOOL bReturn = NewWindow.Create( csClassName, "Test", WS_POPUP | WS_VISIBLE, crect, this, 0 );
RECT wRECT;
GetWindowRect( &wRECT );
wRECT.right = wRECT.left - 1;
wRECT.left = wRECT.left - 100;
NewWindow.MoveWindow( &wRECT, TRUE );
this->Attach( NewWindow.operator HWND());
}
Krishnakumar TG
|
|
|
|
|
Hello Krishnakumartg,
Use CreateEx method insted of Create. Because WS_POPUP cannot be used with the Create method.
|
|
|
|
|
How to shut down computer using c programming?
|
|
|
|