LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Choosing a name when saving to a file

Hi,  I was wondering if someone could help me with my problem. I am tryin to saving data to a file. On my uir i hv a option for you to choose where and what file name you want. but incase one doesnt go and set it up but instead just starts the program. I have it set so that it automatically by default saves it in the folder and a default name, below is the code for it:
 
   if(file_sel==0)
   {  //if file not selected, saves to data.log by default.
          file_sel=1;
          file_opened=1;
          file_handle = OpenFile ("Data.log", VAL_READ_WRITE, VAL_TRUNCATE,
                                                       VAL_BINARY);
   }
 
But i was wondering if i can hv the file name followed by the time it was saved....well i know windows itself puts a tag....i would like to have my own after say Data....perphaps date and time....or something of that sort! because currently my problem is that say i start the program n then stop it....if i start again without quiting it appends......but say i quit and the next time the user starts it will erase the previous log but where as if he chose the file then it will ask him if he wants to over write it...so that part is fine...its only if the user doesnt select the file.
 
I would apprecaite if someone can help me or give an idea to go round this.
 
Thanks
 
k1_ke
0 Kudos
Message 1 of 15
(4,812 Views)

Hi k1_ke,

The library functions DataStr() and TimeStr() can help you out:

char filename[MAX_FILENAME_LEN];

if (file_sel == 0){

    file_sel = 1;

    file_opened = 1;

    Fmt (filename, "Data_%s_%s.log", DataStr(), TimeStr());

    file_handle = OpenFile (filename, VAL_READ_WRITE, VAL_TRUNCATE,
                                                       VAL_BINARY);
   }

0 Kudos
Message 2 of 15
(4,804 Views)
Sorry, it's DateStr() instead of DataStr() Smiley Happy
0 Kudos
Message 3 of 15
(4,799 Views)
Hi Wim,
 
Thanks for your reply. I tried what you mentioned but i get a error...Missing Prototype
 
   if(file_sel==0)
   {  //if file not selected, saves to data.log by default.
        file_sel=1;
       file_opened=1;
       Fmt (filename, "Data_%s_%s.log", DataStr(), TimeStr());                                  
       file_handle = OpenFile (filename, VAL_READ_WRITE, VAL_TRUNCATE,
                                                       VAL_BINARY);

   }
I would appreciate if you could look at it again.
 
Thanks
 
k1_ke

Message Edited by k1_ke on 01-18-2006 11:49 AM

0 Kudos
Message 4 of 15
(4,800 Views)

Hello k1_ke

Pull down the Library menu in the menu bar and select 'Utility...'. In the 'Select Function Panel' window that pops up, select 'Time/Date...'. You should find the functions DateStr() and TimeStr() there.

Greetings,

Wim

0 Kudos
Message 5 of 15
(4,793 Views)
I see now you typed DataStr() instead of DateStr(), that was my mistake Smiley Sad . Just adjust that and it should work.
0 Kudos
Message 6 of 15
(4,789 Views)

Hi Wim,

Sorry to keep bothering you, yeah i found that typo just after i emailed you. After making the changes it still doesnt work...well it passes that line now. My program compiles with no errors...i start it and when it reaches the file to openfile it crashes n get the following error

NON-FATAL RUN-TIME ERROR

Library function error (return value == -1)

Invalid argument

Also when i read the help file for OpenFile it says CAUTION: The Windows SDK also contains a OpenFile function. If you include windows.h and do not include formatio.h, you will get compile errors if you call the OpenFile function. but i need formatio.h for other things.

Let me know what i should do

Thanks

k1_ke

0 Kudos
Message 7 of 15
(4,794 Views)

Hello k1_ke,

 

If the function OpenFile returns the value -1, an error occurred (read the OpenFile help page).

If you want to know what goes wrong, use the following code:

int error_nr;

  if(file_sel==0)
   {  //if file not selected, saves to data.log by default.
        file_sel=1;
       file_opened=1;
       Fmt (filename, "Data_%s_%s.log", DataStr(), TimeStr());                                  

       DisableBreakOnLibraryErrors();
       file_handle = OpenFile (filename, VAL_READ_WRITE, VAL_TRUNCATE,
                                                       VAL_BINARY);
       EnableBreakOnLibraryErrors();
       if (filehandle == -1 and (error == GetFmtIOError()))
      
 

   }
0 Kudos
Message 8 of 15
(4,769 Views)

Hello k1_ke,

 

If the function OpenFile returns the value -1, an error occurred (read the OpenFile help page).

If you want to know what goes wrong, use the following code:

int error_nr;

  if(file_sel==0)
   {  //if file not selected, saves to data.log by default.
        file_sel=1;
       file_opened=1;
       Fmt (filename, "Data_%s_%s.log", DataStr(), TimeStr());                                  

       DisableBreakOnLibraryErrors();
       file_handle = OpenFile (filename, VAL_READ_WRITE, VAL_TRUNCATE,
                                                       VAL_BINARY);
       EnableBreakOnLibraryErrors();
       if (filehandle == -1 and (error == GetFmtIOError()))
       
 

   }
0 Kudos
Message 9 of 15
(4,770 Views)

Smiley Mad Sorry, I accidentally pressed the Tab Key and then kept hitting the space bar, causing 100 messages to be posted... Smiley Mad

I planned to write the following:

 

Hello k1_ke,

 

If the function OpenFile returns the value -1, an error occurred (read the OpenFile help page).

If you want to know what goes wrong, use the following code:

int error_nr;

  if(file_sel==0)
   {  //if file not selected, saves to data.log by default.
        file_sel=1;
       file_opened=1;
       Fmt (filename, "Data_%s_%s.log", DataStr(), TimeStr());                                  

       DisableBreakOnLibraryErrors();
       file_handle = OpenFile (filename, VAL_READ_WRITE, VAL_TRUNCATE,
                                                       VAL_BINARY);
       EnableBreakOnLibraryErrors();
       if (filehandle == -1 and (error == GetFmtIOError()))
              MessagePopup ("Error",  GetFmtIOErrorString(error));
   }
Greetings,
 Wim
0 Kudos
Message 10 of 15
(4,768 Views)