Solved

Question in C++

Posted on 2006-11-19
14
329 Views
Last Modified: 2010-04-16
Hello ..

I'm working on a program in C++ and I've faced some problems in the coding
I need you to help me find some codes , so I can finish my program correctly :

 

1 ) The code for reading from a text file .txt


2 ) The code for writing the result in a text file , which has the format : result_X.txt
where X is the name of input file , it should be named at run time.

for example : for input1.txt -> output file name: result_input1.txt


3 ) A code to implement a Wait For Graph (WFG) , which shows nodes and arrows that connet the nodes together
the WFG may show a cycle between these node in a specific condition.


Regards ..

 

0
Comment
Question by:MOHEXPERT
  • 5
  • 5
  • 4
14 Comments
 
LVL 8

Expert Comment

by:YoungBonzi
ID: 17975417
Hi, I can help you, but what version of C++ are using, is this a console program, and can you explain what a Wait for Graph is?
0
 
LVL 8

Expert Comment

by:YoungBonzi
ID: 17975419
Can I also see what you've got so far? This sounds like HW, and people won't let me help you unless you show your code.
0
 
LVL 8

Expert Comment

by:adg080898
ID: 17983466
Is the "wait for graph" for detecting deadlock conditions?
0
 

Author Comment

by:MOHEXPERT
ID: 17986559
YoungBonzi

I'm using C++.Net , and this is my code so far :

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

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;

int main(int argc, char* argv[])
{
    // TODO: Please replace the sample code below with your own.
    Console::WriteLine("Hello World");
      return 0;
}
char inFileName[100];
if (argc < 2)
{
Console::WriteLine("Please enter an input file name");
exit(1);
}      
strcpy(inFileName,argv[1]);


Here , I'll need to write a code for reading from a text file , after that I'll write the code for the (WFG) , and finally the code for writing in a text file ...

Thanks for the reply ...

----------------------------

 adg

YES ..!!
you got my point
The "wait for graph" is for detecting deadlock conditions

0
 

Author Comment

by:MOHEXPERT
ID: 17986572
I forgot to mention that my program is a ' Windows 32 console application '
0
 
LVL 8

Accepted Solution

by:
YoungBonzi earned 250 total points
ID: 17992909
Hello, I'm not sure what a WFG is...perhaps another expert can help you there...or maybe you can show me an example.

Here is the file handling though:

// Q_22066030.cpp : main project file.

#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main(int argc, char* argv[])
{
    // TODO: Please replace the sample code below with your own.
      String^ strFileName;
      String^ strFileText;
      String^ strResultText;

      Console::WriteLine("Enter the file name:");

      //read the filename
            strFileName = Console::ReadLine();
      
      Console::WriteLine("Opening file"+strFileName);
      strFileText = System::IO::File::ReadAllText(strFileName);
      
      //uncomment this to see what has been read in
      //Console::WriteLine(strFileText);
      

      //do your WFG here
      //...
      //end efg code
      

      //prep new file name
      strFileName = "result_"+strFileName;

      //save results
      //not sure what your WFG does. so you'll have to modify this next line
      strResultText = strFileText;
            
      Console::WriteLine("Saving new file as "+strFileName);

      //create and save "strFileText" to "strFileName"
      System::IO::File::WriteAllText(strFileName, strFileText);
      
      //just to add a pause...comment out if not neccessary
      Console::ReadLine();
      
      return 0;
}
0
 
LVL 8

Expert Comment

by:YoungBonzi
ID: 17992919
note: if you just specify a filename the the exe will look for that file either in the folder that it's running in, or in it's VS Project folder. If you get an error in debug mode, it should tell you where it was looking.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Expert Comment

by:adg080898
ID: 17998428
Microsoft published a long article about detecting deadlocks using invariant analysis. It is actually a series of three articles which go into much depth about detecting deadlocks and modelling synchronization primitives.

http://msdn2.microsoft.com/en-us/library/ms810303.aspx

http://msdn2.microsoft.com/en-us/library/ms810382.aspx

http://msdn2.microsoft.com/en-us/library/ms810421.aspx

Don't forget to download the sample, it contains a runnable (albeit minimal) MFC application which you can use to experiment with detecting deadlocks. I studied these articles for quite a while before I realized how it works. Basically, it builds a matrix which represents the invariants of the graph. This way, you can see if a set of signalled (dotted) nodes meets the invariants (is possible, given the invariants).

Perhaps there is a more modern method than this, but hopefully this will help.
0
 

Author Comment

by:MOHEXPERT
ID: 18003421
Thanks guys
I'm working on it
but my program couldn't identify the string datatype here :

String^ strFileName;
String^ strFileText;
String^ strResultText;

I tried to make it an array of char , but still it has error

char strFileName[]; //error
0
 
LVL 8

Expert Comment

by:YoungBonzi
ID: 18003527
Ohh...I thought you made a CLR console application.

If you make a CLR console application and put that code in your main .cpp file, it should work. Let me know if that's not an option.
0
 
LVL 8

Expert Comment

by:adg080898
ID: 18009616
Are you sure it isn't C#? In C#, an array is a reference type. Should look like this:

char[] strFilename;

If it is C++, the standard string is string (note small s).

You might need "using namespace std", or use std::string.
0
 
LVL 8

Expert Comment

by:adg080898
ID: 18009641
Ah, using the .NET libraries in C++?
0
 

Author Comment

by:MOHEXPERT
ID: 18012501
Hi again ..
the string has gone well , with no errors
I forgot the "using namespace std"

here's my program with the WFG idea that dectects cycles :

===========================================
// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include "stdafx.h"
#include<stdio.h>

#using <mscorlib.dll>

using namespace System;
using namespace std;

class Line
    {
      public:
          string OP ;
            int TransNum ;
            int D_item;
      };

   // EX: read 1 3
   // OP : Operation Name -> read
   // TransNum : Transaction Number -> 2
   // D_item : Data item -> 3

int main()
{
      Line T[50] ; // array of object
      int n=1;
      int i=1;
      int TransNum_Seg; // store the Transaction Number
      int D_item_Seg;  // store the Data item
      
            while (!EOF)
      {

      // read from text file "input1.txt" which has the format:
/*
begin 1
read 1 3
write 1 3
begin 2
read 2 4
begin 3
read 3 6
commit 3
read 2 3
write 2 3
read 1 4
write 1 4
commit 1
commit 2
*/

      // Here , we will read the file " line by line " ,
      // And each line will be stored in an element in an array of object T[]
    } // end while
   
      while  (!EOF)
      {
            if T[n].OP == "Read"   // error
            {
                  TransNum_Seg = T[n].TransNum;
                  D_item_Seg = T[n].D_item;

                        for (i= n+1, i<= n, i++)
                        {
                              while (!T[n].TransNum == TransNum_Seg)
                              {
                              if T[i].D_item == D_item_Seg // compaire D_item in the next element in the array with the previous
                                    if T[i].OP == "Write"
                  // Function Link TransNum_Seg to  T[n].TransNum when : Read -> Write
                              } // end while

                        } // end for loop

            } // end if

            if T[n].OP == "Write"
            {
                  TransNum_Seg = T[n].TransNum;
                  D_item_Seg = T[n].D_item;

                        for (i= n+1, i<= n, i++)
                        {
                              while (!T[n].TransNum == TransNum_Seg)
                              {
                              if T[i].D_item == D_item_Seg // compaire D_item in the next element in the array with the previous
                                    if ( T[i].OP == "Write" ) || ( T[i].OP == "Read" )
      // Function Link TransNum_Seg to  T[n].TransNum  , when : Read -> Write OR Write -> Read
                              } // end while

                     }  // end for loop

            } // end if

            // if T[n] is linked to T[i] & T[n] is linked to T[i] , then we have a cycle , and we must exit while loop
            // and write to text file : not conflict & not recoverable
            // NOTE : we just need to have ONE cycle between TWO transaction to exit while loop


      n= n+1;

      } // end while loop
// if no cycle found , write to txt : conflict & recoverabe

return 0;

} // end main


===========================================

but I still have errors , one of then is in using the arrary of object:

if T[n].OP == "Read"   // error

and can some one help me with the idea of making the Function that  Links TWO object of class line together
EX: T[n] to T[i]

and finally , how to add the code  that YoungBonzi wrote in my program ..

Best Regards ...
0
 

Author Comment

by:MOHEXPERT
ID: 18012523
Sorry , the last program notes are wrong , here's the correct ones :



// if TransNum_Seg is linked to T[n].TransNum & T[n].TransNum is linked to TransNum_Seg ,then we have a cycle , and we must exit while loop , and write to text file : not conflict & not recoverale

// NOTE : we just need to have ONE cycle between TWO transaction to exit while loop


====================

the Question will be :

and can some one help me with the idea of making the Function that  Links TWO object of class line together
EX: TransNum_Seg to T[n].TransNum  

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now