Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,
I a function that reads a file and extract the values, how can I extract only the first and the. last values. To read I use this function:

C++
if (csf.Open(fname, CFile::modeRead)) // CStdioFile csf;
  { 
    while (csf.ReadString(buffer)) //CString buffer;

    {
       int out = 0;
       out = swscanf_s(buffer, _T("%[^,],%[^,],%[^,],%lf"),str1, 2046, str2, 2046, str3, 2046, &db1) );
       // my calculations to process the double values here
    }
      csf.Close();
  }


Updated code: Based on KarstenK's code :-
C++
if (csf.Open(fname, CFile::modeRead)) // CStdioFile csf;
  {
    csf.ReadString(buffer);
   //here is first read in the buffer
    int out = 0;
    out = swscanf_s(buffer, _T("%[^,],%[^,],%[^,],%lf"),str1, 2046, str2, 2046, str3, 2046, &db1) );
    pInitialTime = str1;

  while (csf.ReadString(buffer)) {
   //AFTER that is the last readin the buffer
    out = swscanf_s(buffer, _T("%[^,],%[^,],%[^,],%lf"),str1, 2046, str2, 2046, str3, 2046, &db1) );
     pFinalTime = str1; 
 } 
  csf.Close();
  of.WriteString(FmtValues1(pInitialTime, pFinalTime)); // both pInitialTime and pFinalTime values take the last value
    }



Using this I can read each line and do processing, but how can I extract only the first and the last values?
Posted
Updated 15-Jun-12 4:36am
v4

You just missed one semicolon in Karsten's code, the one at the end of the while statement. It is also no good practice to code a while statement like this. Also Karsten's code was not prepared for when the last line of the file is empty.

Here is a revised version that will help you better:

if (csf.Open(fname, CFile::modeRead)) // CStdioFile csf;
  {
    csf.ReadString(buffer);
        //here is first read in the buffer
    int out = 0;
    out = swscanf_s(buffer, _T("%[^,],%[^,],%[^,],%lf"),str1, 2046, str2, 2046, str3, 2046, &db1) );
    pInitialTime = str1;

    // allocate a temp buffer
    CString tempBuffer;

    // now read all remaining lines of the file without decoding them 
    while (csf.ReadString (tempBuffer))
      if (tempBuffer.GetLength() > 0)
          buffer = tempBuffer;

    // now the last non-empty line is in the buffer and we decode it
    out = swscanf_s(buffer, _T("%[^,],%[^,],%[^,],%lf"),str1, 2046, str2, 2046, str3, 2046, &db1) );
     pFinalTime = str1; 
 } 
  csf.Close();
  of.WriteString(FmtValues1(pInitialTime, pFinalTime)); // both pInitialTime and pFinalTime values take the last value
    }
 
Share this answer
 
Comments
Sumal.V 15-Jun-12 11:39am    
This results in both variables taking the same values as well..
Sumal.V 15-Jun-12 11:51am    
And If I add the semicolon, I get run time error. when I checked the code in debug, I found that the program does not move after while();

That's the reason I took it off.
nv3 15-Jun-12 12:07pm    
I suggest you set a breakpoint on line

if (tempBuffer.GetLength() > 0)

and look line by line what is in your tempBuffer. Compare that with the contents of your file as displayed in an editor. Then tell us that happens when you get to the last line of your data file.
Sumal.V 18-Jun-12 4:34am    
Good morning!,I checked the code with a break point but realised that since the variable in both the if and while loops was str1, ie both pinitialTime and pfinalTime were assigned this str1 variable, at the end when the final time was taking the final value, initialtime was assigned the same value. I changed the variable names now as initstr1 and finalstr1 instead of str1 for both and that gives me the right values..
nv3 18-Jun-12 4:41am    
That's what I told you in your previous question about copying strings. Setting a pointer to a string does not mean your make a copy of it.

Another option would have been to define:

CString initialTime, finalTime;
...
initialTime = str1;
...
finalTime = str1;

In this case the two CString object do make a copy of what is in str1 at that time. That is actually what I had assumed you would do, as you didn't show the definition of pInitialTime and pFinalTime in your code.
C#
if (csf.Open(fname, CFile::modeRead)) // CStdioFile csf;
  {
    CString buffer;
    csf.ReadString(buffer);
   //here is first read in the buffer

    while (csf.ReadString(buffer));
   //AFTER that is the last readin the buffer



it looks so easy ;-)
 
Share this answer
 
Comments
Sumal.V 15-Jun-12 8:48am    
Ummm.. it doesn't look easy for me :(
if I try extract one value, that will apply for all the values, as it is in a loop. so everything the loop is run, the particular column value will b printed.
And I must go into the while loop to start reading the values .
KarstenK 15-Jun-12 8:54am    
I hope you noticed the ";" at the end of the line
while (csf.ReadString(buffer));

Show the complete code your talking about....
Sumal.V 15-Jun-12 9:29am    
Hi,I have updated my question.
KarstenK 15-Jun-12 9:33am    
here is my update

if (csf.Open(fname, CFile::modeRead)) // CStdioFile csf;
{
CString buffer;
csf.ReadString(buffer);
//here is first read in the buffer
int out = 0;
out = swscanf_s(buffer, _T("%[^,],%[^,],%[^,],%lf"),str1, 2046, str2, 2046, str3, 2046, &db1) );

while (csf.ReadString(buffer));
//AFTER that is the last readin the buffer
out = swscanf_s(buffer, _T("%[^,],%[^,],%[^,],%lf"),str1, 2046, str2, 2046, str3, 2046, &db1) );
Sumal.V 15-Jun-12 10:22am    
I updated my code, but bothe the first and last values get the same values ie(last ). I have shown my updated code in the question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900