LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

insert null caracter

Hello,

 

I'm trying ti read a text file and put it into a buffer. This text contains a null caracters. how to read it correctly.

i wrote this code that doesn't give the good result.

 

Thanks for helping me.

Good morning!!

Download All
0 Kudos
Message 1 of 10
(5,328 Views)

It seems to me that the problem you are facing is not in reading the file but in displaying text.

fgets should read the file correctly (provided you pass a char array instead of an int one) but PrintTextBuffer, like any other string-related function, will stop at the first nul character found (as you can read in the help, An ASCII NUL byte marks the end of the buffer).

 

What are you exactly trying to do? Do you want to print the ascii code of each byte in the buffer?



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 2 of 10
(5,319 Views)

hello

yes, i need to send the file (logo.txt) to the printer. I used to utilize a .bat file ( copy logo.txt lpt1). But this method need a sharing of printer which causes problems sometimes.

So, i thought about read the file into a buffer then sending it with "PrintTextBuffer (tompon, " ");". Here, i need to construct the tompon with the same content as the file. 

the file contains a null caracters in between, so i read it in binary mode. fread reads correctely the null carater but when i try to add it into the buffer i can not. so the buffer result is not the file.

 

Sorry for making the response so long.

 

Thanks

0 Kudos
Message 3 of 10
(5,311 Views)

You could something like that: it loads the file, replaces NUL characters with spaces and prints it.

 

#include "toolbox.h"
static int		i, size;
static char	*msg = NULL;
static FILE	*fH = NULL;
static int		error;

GetFileSize ("logo.txt", &size);

msg = malloc (size + 1); memset (msg, 0, size + 1);
fH = fopen ("logo.txt", "rb");
fread (msg, size, 1, fH);
fclose (fH);

for (i = 0; i < size; i++) {
	// Replace NUL bytes with spaces
	if (!msg[i]) msg[i] = 32;
}
error = PrintTextBuffer (msg, "");

if (msg) free (msg);

 

 



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 4 of 10
(5,305 Views)

this is the result of the code that you sent it to me. Plese try to open the two file with notepad++. it's not the same

0 Kudos
Message 5 of 10
(5,287 Views)

Well, the output on my machine is different from yours too!

I suppose this has to do with the nature of PrintTextbuffer output: this command produces a file that can be directed to the printer to have the output on paper, i.e. it includes all necessary formatting codes needed by the printer. Additionally, non-printable character can be treated differently by the printer drivers.

Which is the default printer in your system? I have a HP LaserJet 5000N and the output file starts with 400 bytes of formatting codes before the first informations from the source file.

 

What are you aiming to? If you want the output file to be the same as the input one, why don't you use fwrite instead of PrintTextBuffer?



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 6 of 10
(5,276 Views)

i use a generic only printer which does not format the input. My aim is to read the file (logo.txt) and put it in memory in the same format of the file input.

 

0 Kudos
Message 7 of 10
(5,270 Views)

FibiIA ha scritto:

i use a generic only printer which does not format the input. My aim is to read the file (logo.txt) and put it in memory in the same format of the file input.

 


Isn't this what fread does? Why do you planned to use PrintTextBuffer which outputs to file or to the printer but not in memory?



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 8 of 10
(5,265 Views)

Good Morning,

ok i wanna answer.(i should clarify from the begenning):

 

My application controls a CAB printer to print labels. These labels contain logos. So, i generated these logos in text format file with a program called Codesoft. I have to send to the printer this logo through a CVI application with PrintTextBuffer(bueffer_name, port);

this is why I feed my buffer with the logo contained file. Except that the file contains null characters knowing that I must have contained the same file. So I opened the file in binary then with fread I get byte as an integer.And now, i have to convert it into char because PrintTextBuffer take a buffer with string format.

 

I wrote PrintTextBuffer(buffer,"file") just for debugging, to see the output and that's all. But normaly i write PrintTextBuffer(buffer,""). And the second parameter will be the default port (the default printer).

 

Thanks.

0 Kudos
Message 9 of 10
(5,232 Views)

Ok, now it makes sense. Unfortunately I cannot give you a solution to this.

 

It seems to me that what it is happening is that PrintTextBuffer operates some transformation on the input file before sending it to the printer or output file. As an example, the command 0x0D character in a unexpected way, so that the sequence "0x0D 0x0D 0x0D 0x0D 0x0D 0x0D" at offset 0x34 in the source file becomes "0x0D 0x0A 0x0A 0x0A 0x0A 0x0A 0x0A" (longer and different) in the output file. There are also some modifications in the control characters (initial part of the ASCII table) so that for example 0x08 becomes 0x2E. I haven't examined all the files so there may be other differences.

The point is that all this is happening inside the command, that is in a section of code where we can do nothing.

 

If I were you I'd look into printer documentation to see if there some another method to print images. I'm not experienced in VISA so I don't know if it can offer viable alternatives even though it is able to handle parallel port.

 

Which problems do you find in using the batch file? In a situation similar to yours in the past I used this method:

	sprintf (msg, "command.com /c copy myfile.txt lpt1", prj);
	error = LaunchExecutableEx (msg, LE_HIDE, NULL);

 

It was however several years ago on WinXP and I was printing text only lables.



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 10
(5,216 Views)