Solved

Question in C++

Posted on 2006-11-19
14
368 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

738 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