473,503 Members | 12,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

unknown character in output

hello friends,
in a code i am trying to retrive the machine bit the code. The code
is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main(){
FILE *stream;
char sys[8],*sysptr;
int bit;
sysptr = &sys[0];
sys[6] = 0x0;
stream=popen("file /sbin/init|awk '{print $3}'|cut -c0-2","r");
fread(sysptr,1,2,stream);
pclose(stream);
printf("bit=%s",sysptr);
}
and the output is

bit=32�

why the irritating symbol after 32 is coming? can you suggest me
something?
Aug 1 '08 #1
8 2821
In article <1d**********************************@a21g2000prf. googlegroups.com>,
rudra <bn********@gmail.comwrote:
>fread(sysptr,1,2,stream);
printf("bit=3D%s",sysptr);
fread does not add a binary 0 after the data read in, so you
are attempting to print a string that is not necessarily NUL terminated.

--
"No sincere artist was ever completely satisfied with his labour."
-- Walter J. Phillips
Aug 1 '08 #2
well, i am lest experienced with C. can you plz show me how i can di
that?
Aug 1 '08 #3
rudra wrote:
hello friends,
in a code i am trying to retrive the machine bit the code. The code
is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main(){
Implicit int return is no longer legal. The most portable forms of main
are:

int main(void)

and

int main(int, char**)

as they are required to be supported by all conforming implementations.
FILE *stream;
char sys[8],*sysptr;
Walter Roberson has already addressed your problem, but you might want
to format your code before posting again. Unformatted code is difficult
to read and expecting respondants to do the formatting themselves is
rather optimistic, to say the least.
int bit;
sysptr = &sys[0];
sys[6] = 0x0;
If you want to store a null character a more readable altenative might
be:

sys[6] = '\0';
stream=popen("file /sbin/init|awk '{print $3}'|cut -c0-2","r");
fread(sysptr,1,2,stream);
pclose(stream);
popen and pclose, if you want help on them in future, are topical on
comp.unix.programmer.
printf("bit=%s",sysptr);
}
and the output is

bit=32?

why the irritating symbol after 32 is coming? can you suggest me
something?
Aug 1 '08 #4
rudra wrote:
well, i am lest experienced with C. can you plz show me how i can di
that?
Please quote relevant portions of the post you are responding to. This
makes it easier for people to respond to you.

To manually terminate an array of char with a null character, just
assign one at the desired position. For example if an array contains
seven characters and you want to null terminate them and thus create a
string you can do this:

arr[7] = '\0';

It is equivalent to

arr[7] = 0;

but the former form makes the character nature of 'arr' explicit.

You do not need to add a null character to strings as they are already
so terminated by definition. Almost all the Standard library string
functions recognise and preserve the null terminator. One notable
exception is strncpy for certain values.

Aug 1 '08 #5
rudra <bn********@gmail.comwrites:
stream=popen("file /sbin/init|awk '{print $3}'|cut -c0-2","r");
fread(sysptr,1,2,stream);
The error has been explained, and it was on-topic for comp.lang.c but
I encourage you to ask in comp.unix.programmer so that you don't have
to fire up a shell, run three programs (one of them a full-blown
programming language) just to find this out.

If you post there, try to pin down what you really need to find out.

--
Ben.
Aug 1 '08 #6
rudra <bn********@gmail.comwrites:
hello friends,
in a code i am trying to retrive the machine bit the code. The code
is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main(){
FILE *stream;
char sys[8],*sysptr;
int bit;
sysptr = &sys[0];
sys[6] = 0x0;
stream=popen("file /sbin/init|awk '{print $3}'|cut -c0-2","r");
fread(sysptr,1,2,stream);
pclose(stream);
printf("bit=%s",sysptr);
}
and the output is

bit=32�

why the irritating symbol after 32 is coming? can you suggest me
something?
Please properly indent any code you post here. Run it through the
"indent" program (I recommend "indent -kr"; read the documentation to
see what that means).

Questions about popen and pclose are topical in comp.unix.programmer.

You assign the value 0x0 (which would be better written as the
equivalent '\0', since it's a null character) to sys[6]. Why 6?
Think about how many characters you're reading into your sys array.

The sysptr variable is unnecessary. You could have replaced sysptr
with sys in the calls to fread and printf.

You should always print a new-line character at the end of your
output, unless you have some specific reason not to do so.

"main()" should be "int main(void)", and you should add "return 0;" at
the end of your program.

The string you pass to popen is system-specific, as is the popen
function itself, but I'll mention in passing your program makes some
very specific and probably unwarranted assumptions about the output of
the "file" command. It also performs no error checking (not all
systems have /sbin/init). For details, consult comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 1 '08 #7

Keith Thompson <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
rudra <bn********@gmail.comwrites:
hello friends,
in a code i am trying to retrive the machine bit the code. The code
is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
main(){
FILE *stream;
char sys[8],*sysptr;
int bit;
sysptr = &sys[0];
sys[6] = 0x0;
stream=popen("file /sbin/init|awk '{print $3}'|cut -c0-2","r");
fread(sysptr,1,2,stream);
pclose(stream);
printf("bit=%s",sysptr);
}
and the output is

bit=32?

why the irritating symbol after 32 is coming? can you suggest me
something?

Please properly indent any code you post here.
Brilliant.

This is what happens when I don't post here for a little
while to keep the "regulars" in line.

The problem is obviously an improperly terminated
string. This has even been pointed out, and yet NOBODY
has managed to provide the simple solution:

sysptr[fread(sysptr,1,2,stream)]='\0';

Although that's not EXACTLY the way I would do it (or
actually do it "in real life"), but you get the idea...you have
to insert the "nul" terminator after the last byte-character
actually read by fread(), and since fread() returns the
number of byte-characters actually read (in this case),
well...there it is...

---
William Ernest Reid

Aug 1 '08 #8
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.orgwrote:
>Please properly indent any code you post here.
Good advice.
Run it through the "indent" program (I recommend "indent -kr"; read
the documentation to see what that means).
That does not always work out well.
The "indent" variant that runs on this computer, for instance,
- has no "-kr" option.
- inserts tab characters (at least in default mode), which makes
the output less readable in some newsreaders.

As long as posted code is readable, it should be okay; it does not
have to be formatted according to the rules of some formatting tool.

If you don't like the format, you can always run it through indent yourself,
using your preferred settings.

Ike
Aug 2 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2199
by: Andreas Prilop | last post by:
http://www.iana.org/assignments/character-sets and RFC 1428 have an encoding (charset) "unknown-8bit". There is also the widely recognized "x-user-defined", which means the same thing, afaik....
7
96255
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
7
7762
by: arnuld | last post by:
i am trying to implement C style strings in C++ (from chapter 4 "C++ Primer 4/e"): // reading from std::cin for a c-string // (a null terminated character array) #include <iostream> #include...
111
19921
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
9
2905
by: jraul | last post by:
1) Am I correct that C++ does not have a defined character set? In particular, a platform might not use the ASCII character set? 2) C++ supports wchar_t types. But again, this has no defined...
2
4238
by: JanDoggen | last post by:
function vldLicense($lic) { echo "called with lic: ". $lic . "<br>"; echo preg_match('', $lic) . "<br>"; if (preg_match('{4}-{4}-{4}-{4}', $lic) == 0) return false; return true; } gives me:
13
3858
by: =?Utf-8?B?YXVsZGg=?= | last post by:
i have come across a situation in my project where i read a text file with some characters greater than hex 0x7f. i need to write character (0xE0) to a new file as an exception. however when i...
33
7129
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
1
3264
by: Gramma2005 | last post by:
I am trying to connect to an access db through php on Windows Server 2003 running XAMPP. The code I am using has worked before on another access db on this server so I am not sure what is causing...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.