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 30-Aug-2006, 16:42
Unit1 Unit1 is offline
Awaiting Email Confirmation
 
Join Date: Aug 2006
Posts: 16
Unit1 is on a distinguished road

How can I read a *.txt file from disk and display the values as bars in TChart


Hi,

Can anyone help me with the following problem:

I have to read values from a DetectorLowEnergyGainMap.txt file on the disk that is filled with values like: -128,-10,-30,0, -200 etc. I can read the file with the following code:

CPP / C++ / C Code:
char LowEnergyGainMap[300]; 


bool TMainForm::LeesDetectorLowEnergyGainMap()
{
fstream inputfile;

          inputfile.open("DetectorLowEnergyGainMap.txt"); // 

          	FileName = ExtractFileName(OpenDialog->FileName);
          	SourceFileDir = ExtractFileDir(OpenDialog->FileName);

          		if(!inputfile) {
                	UpDateStatusLine(StatusLineRight,"Detector data file niet geladen !!!");
                	return FALSE;
          		}

          		while(!inputfile.eof()) {
                	inputfile.getline(LowEnergyGainMap, sizeof(LowEnergyGainMap));
          		}
	
          inputfile.close();

          UpDateStatusLine(StatusLineRight,FileName + "  data file geladen !!!");
       return TRUE;
}



So far, everything works fine. If I display the data e.x. with ShowMessage(LowEnergyGainMap) I can see that my array LowEnergyGainMap is filled now with the correct data.

However, if I try to display the data in a TChart with the code below, all the bars are displayed as positive bars and not with the data that is in the LowEnergyGainMap.

CPP / C++ / C Code:

if(WhichRange == "NR"){
 for (t=0; t<NumberOfTubes; t++) {  // Normal Range
      PMTGainNRDet1->Add(LowEnergyGainMap,t+1,GetDefaultColor(16));
}



I know that in the call: Add(LowEnergyGainMap,t+1,GetDefaultColor(16)) LowEnergyGainMap should be declared const double. I think that my problem resides here.

So, in general, how can I read the datavalues from my text file and display them as bars in the TChart.

Any suggestions are very welcome. Thanks in advance

Johan Ditzel
  #2  
Old 31-Aug-2006, 10:29
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: How can I read a *.txt file from disk and display the values as bars in TChart


I have no experience with TChart...but you are reading LowEnergyGainMap as a char array, i.e. a string containing the numbers in the file. You will have to use strtol() or something similar to convert them to doubles.
  #3  
Old 31-Aug-2006, 11:50
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: How can I read a *.txt file from disk and display the values as bars in TChart


Quote:
Originally Posted by ubergeek
I have no experience with TChart...but you are reading LowEnergyGainMap as a char array, i.e. a string containing the numbers in the file. You will have to use strtol() or something similar to convert them to doubles.
Or better yet write your own function that splits the line into it's components.

Also, this loop
CPP / C++ / C Code:
while(!inputfile.eof()) 
{
    inputfile.getline(LowEnergyGainMap, sizeof(LowEnergyGainMap));
}
has 2 problems:
1) it reads each line of the file but does nothing with it. You therefore exit the loop with only the last line of the file as data. You need to process the line after reading.
2) you use .eof() to exit the loop. This is the same as using feof() in C. This won't work as you expect, and here's why
__________________

Age is unimportant -- except in cheese
  #4  
Old 01-Sep-2006, 02:17
Unit1 Unit1 is offline
Awaiting Email Confirmation
 
Join Date: Aug 2006
Posts: 16
Unit1 is on a distinguished road

Re: How can I read a *.txt file from disk and display the values as bars in TChart


Thanks for the reply and help. Now I am able to read my text file and display the values as bars in TChart. Here is the way I modified my code.

I did not change annything in the code below.

CPP / C++ / C Code:
char LowEnergyGainMap[300];
int PMTGainDataNR[60]; 


bool TMainForm::LeesDetectorLowEnergyGainMap()
{
fstream inputfile;

          inputfile.open("DetectorLowEnergyGainMap.txt"); // 

          	FileName = ExtractFileName(OpenDialog->FileName);
          	SourceFileDir = ExtractFileDir(OpenDialog->FileName);

          		if(!inputfile) {
                	UpDateStatusLine(StatusLineRight,"Detector data file niet geladen !!!");
                	return FALSE;
          		}

          		while(!inputfile.eof()) {
                	inputfile.getline(LowEnergyGainMap, sizeof(LowEnergyGainMap));
          		}
	
          inputfile.close();

          UpDateStatusLine(StatusLineRight,FileName + "  data file geladen !!!");
       return TRUE;
}

In the following function I added a loop that reads the gainmap with chars and converts it to a map with integers so that Tchart can display it.

CPP / C++ / C Code:
void __fastcall TMainForm::DetNRClick(TObject *Sender)
{

long getal;
char * pEnd;
char * s;
int z;

          OpenDialog->Filter = "Normal Range|*LowEnergy*.txt";
          if (OpenDialog->Execute()) {


          if(LeesDetectorLowEnergyGainMap()) {
                ShowMessage(LowEnergyGainMap);  // for debug only
                }

                s =  LowEnergyGainMap;

                for (z=0; z<NumberOfTubes+1; ++z) {
                        PMTGainDataNR[z] = strtol (s,&pEnd,0);  //strtol(nptr, (char **)NULL, 10);
                        s = pEnd+1;
                        }

          DisplayDetectorGainBars("NR");
          GainChart->Visible = TRUE;
          NRLoaded = TRUE;
          DetNR->Enabled = FALSE;

          }
          if(NRLoaded && ERLoaded){
                PMTGainDifference->Visible = TRUE;
                DisplayDetectorGainDifferenceMap();
                }
}

Thanks,

Johan
  #5  
Old 01-Sep-2006, 03:39
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: How can I read a *.txt file from disk and display the values as bars in TChart


Your formatting needs an overhaul. Indenting just to indent is not a good idea. Check out this info for formatting ideas.

I still want to know what's going on here in this (reformatted) function:
CPP / C++ / C Code:
char LowEnergyGainMap[300];
int PMTGainDataNR[60]; 


bool TMainForm::LeesDetectorLowEnergyGainMap()
{
    fstream inputfile;

    inputfile.open("DetectorLowEnergyGainMap.txt"); // 

    FileName = ExtractFileName(OpenDialog->FileName);
    SourceFileDir = ExtractFileDir(OpenDialog->FileName);

    if(!inputfile) 
    {
        UpDateStatusLine(StatusLineRight,"Detector data file niet geladen !!!");
        return FALSE;
    }

    while(!inputfile.eof()) 
    {
        inputfile.getline(LowEnergyGainMap, sizeof(LowEnergyGainMap));
    }
	
    inputfile.close();

    UpDateStatusLine(StatusLineRight,FileName + "  data file geladen !!!");
    return TRUE;
}
Since you didn't change this function, what is its purpose? It reads the file, does nothing with any of the data read, and exits the read loop wrong (as my previous post explains). Am I missing something?
__________________

Age is unimportant -- except in cheese
  #6  
Old 01-Sep-2006, 04:24
Unit1 Unit1 is offline
Awaiting Email Confirmation
 
Join Date: Aug 2006
Posts: 16
Unit1 is on a distinguished road

Re: How can I read a *.txt file from disk and display the values as bars in TChart


Hi WaltP,

Thks for the quick response. Since I am a beginner in C/C++ I apreciate yr comments and help. I will check yr info about formatting ideas.

In the function LeesDetectorLowEnergyGainMap() I only read the contens of the file on disk and put it in an array of chars named LowEnergyGainMap.

In the next function DetNRClick I read the contens of LowEnergyGainMap and convert the chars into int's so that I can call TChart.

Now, I tried to read another file from disk and putting it in an array but if I read the array and tried to output its contens nothing is displayed. It has probably to do with explanation of yr previous post.

I will tried to change it and let you know.

Thanks

Johan
  #7  
Old 06-Sep-2006, 09:03
Unit1 Unit1 is offline
Awaiting Email Confirmation
 
Join Date: Aug 2006
Posts: 16
Unit1 is on a distinguished road

Re: How can I read a *.txt file from disk and display the values as bars in TChart


Hi all,

I have the following question:

With fgets(EnergyMap, sizeof(EnergyMap), fp), I am reading a line from a Text file from disk.

The line of chars (a string) in the textfile look like this:

e.g.

5 \t 923.000000 \t 10.000000 \n

So 5 a TAB 923.000000 a TAB 10.000000 a carrage return.

Now I have to decode the string so that I can fill an array with the first number (in this case the 5), an array with the second number (923.000000 ) and an array with the last number (10.000000 ). The file contains 1000 lines.

So far I am able to decode the first number (5) and the last number (10.000000), however how can I decode (isolate) the second number, in this case 923.000000 from the string.

Here is a part of the code I use to decode the string:

CPP / C++ / C Code:

while ( fgets(EnergyMap, sizeof(EnergyMap), fp) != NULL) // ReturnCode == NULL wanneer er en fout ontstaat. The EOF is considered to be an ERROR!
              {
                 ch=strchr(EnergyMap,'\t');
                 TabPos = (int)(ch-EnergyMap+1);

                 if ( TabPos == 2 ) // Read the lines 1-9
                    {
                       while (ch != NULL)         // next two tabs
                          {
                              if((int)(ch-EnergyMap+1) == 2) // first tab
                                {
                                  ShowMessage((ch)); // Debug only
                                }

                               if((int)(ch-EnergyMap+1) > 10) // next tab
                                 {
                                   ShowMessage((ch)); // Debug only
                                   dbl = strtod (ch,&pEnd);
                                   ShowMessage((dbl)); // Debug only
                                 }
                            ch=strchr(ch+1,'\t');
                         }

                     positie[TabIndex] = TabIndex;
                     TabIndex++;
                  }



Thanks in advance,

Johan
  #8  
Old 06-Sep-2006, 09:30
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: How can I read a *.txt file from disk and display the values as bars in TChart


If there are only two tabs on the line, you could do something much simpler.
Code:
while (fgets...) { process first field get first tab position (as you did) process second field get second tab position process third field }
If your data lines may contain errors, you'll have to add error processing.
If some lines are more complex than this, you may have to adjust your processing and this idea may not work well.
__________________

Age is unimportant -- except in cheese
  #9  
Old 07-Sep-2006, 04:24
Unit1 Unit1 is offline
Awaiting Email Confirmation
 
Join Date: Aug 2006
Posts: 16
Unit1 is on a distinguished road

Next basic question


I have to read and convert individual charcters to an int and double, like this:

EnergyMap contains characters like:

EnergyMap[0]contains a 1
EnergyMap[1]contains a 2
EnergyMap[2]contains a 3
EnergyMap[3]contains a 4

I have to read/convert the contens of EnergyMap[0 - 3]to an integer or double value like 1234.

The idea was first concatenate the 4 characters to a string and then convert it to an integer or double.


CPP / C++ / C Code:
char EnergyMap[4];
char str[10];

strcpy (str,EnergyMap[0]);
strcat (str,EnergyMap[1]);
strcat (str,EnergyMap[2]);
strcat (str,EnergyMap[3]);


But, the compiler is generating the error:

[C++ Error] Main.cpp(791): E2034 Cannot convert 'int' to 'const char *'

So, how can I read the individual characters from the array of chars and convert it to one integer or double value.

Any idea how to solve this??

Thanks in advance.

Johan
  #10  
Old 07-Sep-2006, 15:12
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: How can I read a *.txt file from disk and display the values as bars in TChart


strcat takes a whole string as the second parameter; you only have a single character. in this case, you can just assign str (which only needs to have 5 elements) the elements of EnergyMap directly.
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

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
Apache2 config issues monev Apache Web Server Forum 2 28-Jun-2004 06:19

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

All times are GMT -6. The time now is 15:42.


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