LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

write in a text file

i want to write some text to a .csv file and then i want to generate a CVI call back to prompt the user to enter a filename for that report.
0 Kudos
Message 11 of 44
(2,463 Views)

Let me try to summarize all the suggestions in this project:

1. scomack system relies on an external file to hold the last file written permitting you to automatically generate a name with a progressive index. It can be easily customized to permit creation of a filename significant to you

2. CreateAndOpenTemporaryFile does the same thing without need for an external file to hold the index of the last file created; it creates a different filename, though, maybe in a slightly less rigid way than in scomack one

3. FileSelectPopup permits the user to navigate in system resources and choose both filename and directory (even on remote systems or remvable drives)

4. Finally, a simple PromptPopup can be enough to permit the user to set the name of the output file

You may choose the system that better fits to the desired level of flexibilty and/or complexity your application requires.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 12 of 44
(2,460 Views)
hi...
i wanted to write this into a .csv file...

if (userResponse == TRUE) {
   
       fH = OpenFile (REPORT_NAME, VAL_WRITE_ONLY, VAL_TRUNCATE, VAL_ASCII);
       fH1 = OpenFile (REPORT_NAME, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
       
    sprintf (msg, "Report \nUser ID: 123456\n Date,Time,Notes \n");
   
        while(n==20){
       
        sprintf (msg1,"%s,[%s] ,%f,%f,This is the test comment under notes",DateStr (), TimeStr (),CO,BV);   
         n++;
       
    }      

    WriteLine (fH,msg,-1);
    WriteLine (fH1,msg1,-1);
    CloseFile (fH);
    CloseFile (fH1);
                                                                                                                      
            }
           

so i want the output to be sent to a .csv file  with the columns being report, user id , date ,time and notes and the data goes into the columns in and there are 20 such data items.

So i thought i'd need to put the second msg1 in a loop..but maybe there's something wrong with my syntax here...

can you please help me asap
0 Kudos
Message 13 of 44
(2,436 Views)

Here your function rewritten to write in one file all your data. Error checking must be added looking at return values for OpenFile and WriteLine.

if (userResponse == TRUE) { 

    int    fH = 0;
    char   msg[512];

    fH = OpenFile (REPORT_NAME, VAL_WRITE_ONLY, VAL_TRUNCATE, VAL_ASCII);
        
    WriteLine (fH,"Report \nUser ID: 123456\n Date,Time,Notes",-1);
    
    while (n < 20) {
       
        sprintf (msg,"%s,[%s] ,%f,%f,This is the test comment under notes",DateStr (), TimeStr (),CO,BV);    

// NOTE: Where are the differences between the lines? There is no data dependind on "n" here!   <=====

        WriteLine (fH,msg,-1);
        n++;
       
    }      

    WriteLine (fH,msg,-1);
    CloseFile (fH);
                                                                                                                       
}



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 14 of 44
(2,436 Views)
Hi

It worked...i can see that i made some elementary errors in the code... the < was a no brainer...sorry about that...

the thing is that i've got the data...but i wanted to randomize the variables CO and BV....i've done that..between min =0.5 and max =3...but the values remain the same at every loop cycle


how do i change that value ? do i use a new seed everytime?
0 Kudos
Message 15 of 44
(2,436 Views)

To generate rando numbers you can use SetRandomSeed and Random from the Programmer's toolbox. This tool (toolbox.fp) is located in <cvidir>\toolslib\toolbox and can be loaded either in the Instrument menu or in the Library menu. I personally prefere the second option since I use extensively its function throughout my programs, but you can decide to operate in either way.

Look at online help for these functions and execute the following lines in the interactive window. Note that these functions return a pseudo-random series of numbers, depending on the random seed used. On slow processes normally you can randomize every time against Timer () value which is changing, but on tight loops as yours this is not relevant (Timer returns milliseconds, while your loop should terminate in less than one msec).

#include <utility.h>
#include "toolbox.h"
static int  i;

SetRandomSeed (Timer ());
for (i = 0; i < 20; i++) {
    
        DebugPrintf ("%s,[%s] ,%f,%f,This is the test comment under notes\n",DateStr (), TimeStr (),
         Random (0.5, 3.0),Random (0.5, 3.0));   
       



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 16 of 44
(2,433 Views)
hi...there seems to be something wrong with the SetRandomSeed function...the compiler gives an errors sayin

SetRandomSeed(Timer ());

  50, 21   syntax error; found '(' expecting ')'.


0 Kudos
Message 17 of 44
(2,432 Views)
i want to generate a callback to make a FileSelectPopup option...i'm not able to figure out the syntax for this command from the help file..


should i just assign these paths via a #define or just do a strcpy for the individual parameters of the FileSelectPopup option?
0 Kudos
Message 18 of 44
(2,424 Views)
The error you are seeing is most commonly a result of trying to put code at the top level of a .c file. If you took Roberto's sample and just placed it in a .c file, it will not work. You have to place the code within a function, or main().

ex:

#include <utility.h>
#include "toolbox.h"

int main(void)
{
    int i;

    SetRandomSeed (Timer ());
    for (i = 0; i < 20; i++)
    {
        DebugPrintf ("%s,[%s] ,%f,%f,This is the test comment under notes\n",DateStr (), TimeStr (),
        Random (0.5, 3.0),Random (0.5, 3.0));   
    }
    return 0;
}


I suspect that Roberto was testing his code using the interactive window, which does not impose this requirement (for convenience).

Hope this helps,

-alex
0 Kudos
Message 19 of 44
(2,424 Views)
thanks...that solve d the problem somewhat...
but the thing's not yet complete...i want to know how to use the FileSelectPopup command to write this file data to a particular file taht's of type .csv and is located in a particular directory...

could you  please give me some sorta idea...the examples in the help file just give definitions...there aren't any real examples of usage...

0 Kudos
Message 20 of 44
(2,419 Views)