LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

write in a text file

Hallo everybody,

I have to write in unregular disctances one text line with a timestamp in a text file, so I get an Logfile.

Is there an example or something else?
Can anybody give me perhaps some example functions which I will probably need!

Thanks
Florian
0 Kudos
Message 1 of 44
(11,605 Views)
 fH = OpenFile ("logfile.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
 sprintf (msg, "%s [%s]: Your line of data", DateStr (), TimeStr ());
 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?
Message 2 of 44
(11,593 Views)
Thanks a lot again Roberto!

Florian
0 Kudos
Message 3 of 44
(11,596 Views)
hi Roberto, this might seem silly...but how do you declare msg?

i tried somethign like

char *msg

char report [MAX_PATHNAME_LENGTH]

fH=openfile( report,VAL_WRITE_ONLY,VAL_APPEND,VAL_ASCII);

msg = &report ;

writeline(fH,msg,-1);

it gave me errors after this...am i doing something wrong?


0 Kudos
Message 4 of 44
(11,371 Views)

declare msg at a fixed length - just make sure it is long enough.  ( char msg[256]  or 128 or 100).

Also, the first param to openfile should be the file's name (and directory path if needed).

for example:

fH=openfile( "c:\Reports\Debug.txt", VAL_WRITE_ONLY,VAL_APPEND,VAL_ASCII);

 

You are not giving it a name at all in your statement.  You have only dimension-ed an array when you define 'report'.

0 Kudos
Message 5 of 44
(11,367 Views)
hi,

i tried what you told me...but it doesn't seem to be working...i want to write some text to the file and i want the format to be like this

---------------------------
NAme
Date, Time, etc...

-----------------------


how do i enter the text into the file? should i just do something like this...
char msg[MAX_PATHNAME_LEN]="Name";
char msg1[MAX_PATHNAME_LEN]="Date, Time ";

fH = OpenFile ("C:\\path\\file.csv", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);

WriteLine (fH,msg,-1);
WriteLine (fH,msg1,-1);


is there a better way to write this?

would appreciate a reply

thanks..
0 Kudos
Message 6 of 44
(11,365 Views)
hi...i sorta solved the problem by using sprintf ...so it kinda worked...

thanks..


0 Kudos
Message 7 of 44
(11,364 Views)
the thing that i do want is for the user to be able to select the filename ...i want to generate a new text file on every write...so i need to change that variable...that's why i declared

char report[MAX_PATH_LENGTH]

and then tried a strcpy of the path...but can you suggest a better approach in terms of the syntax?
0 Kudos
Message 8 of 44
(11,364 Views)

Here is a way I have done it in the past.  Note that this keeps 20 copies (you can change it to keep more) of a file names Debug_Out01.dat, Debug_Out02.dat    through   Debug_Out19.dat, and Debug_Out20.dat.

It creates these files in a directory called "Debug" within the directory from which it is run.

The "File*  Debug_File"  is defined globally.  Anything else not defined locally to this routine was also defined globally.

A routine to return a pointer to the file was written so that multiple codes in multiple files could all call the same debug file.

Message Edited by scomack on 02-13-2007 02:38 PM

0 Kudos
Message 9 of 44
(11,352 Views)

Hi nadster,

it's not clear to me what's your intention, but I can give you two suggestion: I hope one at least can help you a little...

If you want the user to be able to select its own file you can use FileSelectPopup, a function that opens standard "Open file..." dialogue box permitting the user to navigate up to the directory he wants and to select an existing file or type a new filename for the file to be created.

On the other hand, if you want to programmatically and progressively increase a file count you could rely on CreateAndOpenTemporaryFile, that permits you to input prefix and extension for a given file, automatically creating and opening a new file with the name "prefixNNN.ext" where NNN is a index for the file immediately available in the series. With this command you can automatically create up to 1000 files with a given prefix+extension pattern. The function automatically checks for duplicates and creates a surely unique filename (it returns -91 too many files on the 1001st filename).



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 10 of 44
(11,340 Views)