Download/install qemu and then use it like
qemu bootimage
Download/install qemu and then use it like
qemu bootimage
Actually if your trying to minic a shell this example is better.
example of usage ./testit ps wc
where ps = current processes
and wc = word count
testit.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
enum PIPES {READ, WRITE};
int main(int argc, char**argv)
{
int hpipe[2];
pipe(hpipe);
if (fork())
{
close(hpipe[READ]);
dup2(hpipe[WRITE], 1);
close(hpipe[WRITE]);
execlp(argv[1], argv[1], NULL);
}
else
{
close(hpipe[WRITE]);
dup2(hpipe[READ], 0);
close(hpipe[READ]);
execlp(argv[2], argv[2], NULL);
}
exit(EXIT_SUCCESS);
}
Here's a quick example of a parent receiving from a child which receives from a child...It should be 'mostly' correct.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
enum PIPES {READ, WRITE};
int main(int argc, char**argv)
{
int hpipe[2];
pipe(hpipe);
if (fork())
{
/*parent*/
char ch;
close(hpipe[WRITE]);
dup2(hpipe[READ], 0);
close(hpipe[READ]);
while ((ch = fgetc(stdin)) != EOF)
{
fprintf(stdout, "parent rec->%c\n", ch);
}
}
else
{
/*child*/
char ch;
int hpipe2[2];
pipe(hpipe2);
close(hpipe[READ]);
dup2(hpipe[WRITE], 1);
close(hpipe[WRITE]);
if (fork())
{
close(hpipe2[WRITE]);
dup2(hpipe2[READ], 0);
close(hpipe2[READ]);
while ((write(1, (char*)&ch, read(0, (char*)&ch, sizeof(char)))))
{
fprintf(stderr, "middle child rec->%c\n", ch);
}
}
else
{
/*child2*/
close(hpipe2[READ]);
dup2(hpipe2[WRITE], 1);
close(hpipe2[WRITE]);
fputs("this is from way down the line", stdout);
}
}
exit(EXIT_SUCCESS);
}
Because the compiler padded the structure elements up to whole bytes.
one byte for signed int a:3;
two bytes for unsigned int b:13;
and on byte for unsigned int c:1;
Try using the attribute packed like below and you'll get three bytes because again the compiler will pad the end of the structure up to wholes bytes..
#include <stdio.h>
int main(int argc, char* argv[])
{
struct bitfield
{
signed int a:3;
unsigned int b:13;
unsigned int c:1;
}__attribute__((packed));
struct bitfield bit1={2,14,1};
fprintf(stdout, "size of a->%lu\n", sizeof(bit1));
return 1;
}
You need to post 255 lines of code to ask a question about an array of structures?
Shouldn't this end with a NULL
execve(argv[0], argv, envp, NULL);
Ooops my mistake.
Try this:
int main(int argc, char* argv[])
{
printf("%lf\n",(0.99F - 0.90F - 0.09F));
}
I know this is knit picking...
We all know that we can use the name of an array as the pointer to that array,
The name of an array is a label which is a convenient human readable memory address. Its not a pointer...
Okay, done! Thanks gerard4143!
Now i can go back to my double-link list and DEQUE program.There is also another clarification. How do I fix the memory leak? I've made an attempt inside the program. Please check whether it's correct or not...
void change(struct test **some_ptr) { struct test *new_fresh = (struct test *)malloc(sizeof(struct test)); free(*some_ptr); // SHOULD I BE DOING SOMETHING LIKE THIS? // OR IS THIS REDUNDANT? // PESONAL OPINION - IT SHOULD BE DONE *some_ptr = new_fresh; new_fresh -> link = NULL; new_fresh -> data = 20; printf("\n\n\tInside Function"); printf("\n\n\tData = %d\t\tAddress %x", new_fresh -> data, new_fresh); printf("\n\n\t____________________________________________________________"); }
Yes this will get ride of the memory leak...but it will also get get of the pointer you passed to the function.
The point I'm trying to make is, is this the functionality you want in this function? Dynamic memory runtime errors are very hard to track down so I would take a minute and think about it...
I think this is what you want: - Note your code generates a memory leak on line 43...
# include <stdio.h>
# include <stdlib.h>
struct test
{
int data;
struct test *link;
};
void change(struct test **ptr);
int main()
{
struct test *fresh = (struct test *)malloc(sizeof(struct test));
int some_data = 10;
fresh -> data = some_data;
fresh -> link = NULL;
printf("\n\n\tBefore Going Into Function");
printf("\n\n\tData = %d\t\tAddress %p", fresh -> data, (void*)fresh);
printf("\n\n\t____________________________________________________________");
change(&fresh);
printf("\n\n\tAfter Getting Out Of Function");
printf("\n\n\tData = %d\t\tAddress %p", fresh -> data, (void*)fresh);
printf("\n\n\t____________________________________________________________");
return 0;
}
void change(struct test **some_ptr)
{
int some_new_data = 20;
struct test *new_fresh = (struct test *)malloc(sizeof(struct test));
*some_ptr = new_fresh;
/*(*some_ptr) -> data = some_new_data;
(*some_ptr) -> link = NULL;*/
new_fresh->data = some_new_data;
new_fresh->link = NULL;
printf("\n\n\tInside Function");
printf("\n\n\tData = %d\t\tAddress %p", new_fresh -> data, (void*)new_fresh);
printf("\n\n\t____________________________________________________________");
}
gcc on Debian Lenny
Well the code I gave you was compiled on GCC/Mandriva and it ran without a hitch.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *ch = "this is the 34\00134\n";
int main(int argc, char**argv)
{
int i = 0;
for (i = 0; i < strlen(ch); ++i)
fprintf(stdout, "ans->%x\n", ch[i]);
exit(EXIT_SUCCESS);
}
your telling me when you ran this you got a different output then below:
ans->74
ans->68
ans->69
ans->73
ans->20
ans->69
ans->73
ans->20
ans->74
ans->68
ans->65
ans->20
ans->33
ans->34
ans->1
ans->33
ans->34
ans->a
Hi gerard4143, i tried with your source and did not work too.
Can I ask...What are you using for a compiler.
Try running this program. What was the result
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *ch = "this is the 34\00134\n";
int main(int argc, char**argv)
{
int i = 0;
for (i = 0; i < strlen(ch); ++i)
fprintf(stdout, "ans->%x\n", ch[i]);
exit(EXIT_SUCCESS);
}
I tried like you said.
Look how i did:
char *clients = "35:Jhon\x00135:Lucas";
Each client needs to start with the index 35 and I need separate them with the special char...
See? I need add the special char between the clients, and does not works because of the 35, the compiler thrown an error : "escape sequence out of range"
and now?
please... help me :)
try octal notation
char *clients = "35:Jhon\00135:Lucas";
Try this link
http://msdn.microsoft.com/en-us/library/h21280bw%28VS.80%29.aspx
In your example try using the octal notation.
try this
#include <stdlib.h>
#include <stdio.h>
int main ()
{
char *ca;
setenv("QUERY_STRING", "a=1&b=2&c=3", 1);
ca = getenv ("QUERY_STRING");
printf ("QUERY_STRING = %s\n", ca);
return 0;
}
Actually you can slim down your code even more
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main ()
{
char *ca;
char command[100];
command[0] = '\0';
strcat (command, "export QUERY_STRING=");
strcat (command, "'a=1&b=2&c=3'");
printf ("command = %s\n", command);
system (command);
ca = getenv ("QUERY_STRING");
printf ("QUERY_STRING = %s\n", ca);
return 0;
}
I tried single quotes in your quoted string...it appears to work
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main ()
{
char *ca;
char queryStringSet[100];
char command[100];
strcpy (command, "export QUERY_STRING=");
strcpy (queryStringSet, "'a=1&b=2&c=3'");
strcat (command, queryStringSet);
printf ("command = %s\n", command);
system (command);
ca = getenv ("QUERY_STRING");
printf ("QUERY_STRING = %s\n", ca);
return 0;
}
Try something like below
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char**argv)
{
int ch = 0, i = 0;
FILE *fd;
if (!(fd = fopen("filename", "r")))
{
fputs("could not open file!\n", stderr);
exit(EXIT_FAILURE);
}
while ((ch = fgetc(fd)) != EOF)
{
if (ch == '\n')
++i;
}
fprintf(stdout, "the number of lines->%d\n", i);
fclose(fd);
exit(EXIT_SUCCESS);
}
I'm assuming this is a text file - Why don't you check for the newline character '\n'
shouldn't this
sscanf(buffer, "%lf", tempFinal
be
sscanf(buffer, "%lf", &tempFinal
I'm not sure why you need to force the users to call init()- a well commented header file or docs should suffice or is this part of the assignment requirements..
execve info/man has really good example code towards the bottom of the topic.
Here's what I did:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char *newargv[] = { "/bin/ls", "-l", NULL };
char *newenviron[] = { NULL };
char *exeprog = "/bin/ls";
execve(exeprog, newargv, newenviron);
exit(EXIT_FAILURE);
}
Like I said check the bottom of execve info/man for the example code...
Not sure what you mean by this line - "remove a consecutive occurrence of white space character". Do you mean remove all the spaces in a line of text? Because if that's your intent then try looking up the "C squeeze algorithm".
My question is Y DLL? i know that it saves memory and many process can use 1 DLL file at a time blah blah blah......
but is there any task which can not b achieved without using DLL files?? Please reply in details.
I never understood the logic or lack of it in postings like these...You asked questions like - "My question is Y DLL?" and have statements like "can not b achieved" and "blah blah blah......" and you expect a coherent reply with details.
I would open the file and read three lines into it and then display the results...This sounds like a for loop to me..
Here's a simple hint on code writing...Get one piece working before you move onto the next piece of code...Your code has all the characteristics of someone guessing at the answers
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
void getfname(char *line);
int main(void)
{
char fInput[256];
char fOutput[256];
int fd, fd2;
printf("input file: ");
getfname(fInput);
printf("output file: ");
getfname(fOutput);
if((fd = open(fInput, O_RDONLY)) < 0)
{
perror(fInput);
exit(1);
}
if((fd2 = open(fOutput, O_WRONLY|O_CREAT, 0666)) < 0)
{
perror(fOutput);
close(fd);
exit(1);
}
exit(EXIT_SUCCESS);
}
void getfname(char *line)
{
fscanf(stdin, "%s", line);
}
Very important - Do you know why this works now?
Try changing this line
fgets(line, PATH_MAX, stdin);
to
fscanf(stdin, "%s", line);
Will your program open the files now?
Could we see what you have so far?
Could we see some code...Your array, what type is it?
Try this as a template for starting your code
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#define BLK_SIZE 25
int main()
{
char ch[BLK_SIZE + 1];
int n = 0;
int fd = open("filename", O_RDONLY);
if (fd < 0)
{
fputs("could not open filename!\n", stderr);
exit(EXIT_FAILURE);
}
while ((n = read(fd, ch, BLK_SIZE)) > 0)
{
ch[n] = '\0';
/*if you want to see the read results then uncomment the next line*/
/*fprintf(stdout, "\nread->%d\n", n);*/
write(1, ch, n);
}
close(fd);
exit(EXIT_SUCCESS);
}
hi guy i started learning c recently, and im trying to do exercises from the book the bad thing is they are not showing the solutions.
what i need to do is simply modify the program below so that instead of reading single characters at a time, it reads in blocks of text at a single go, and then displays the block on screen.
im not very sure how to make it work...:/// read the file from comand lien and displys it #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> main(int argc, char *argv[]) { int fd ,numseats, i=0; fd = open(argv[1],O_RDWR); if(fd <0) { printf("file s% not found",argv[1]); exit(1); } while(read(fd, &numseats, sizeof(int)) > 0) printf("flight%4d: %4d seats available\n", i++, numseats); /* finished */ exit(0); }
also it asks me to let the user choose where the output could be sent to whether screen or specified file.
i hope you could help me with this
thanks a lot guys in advance
You are reading blocks of text - the blocks just happen to be the sizeof int
read(fd, &numseats, sizeof(int))
For a choice of output use the write system call
ssize_t write(int fd, const void *buf, size_t count);
Here's a quick and simple version of what your trying to do:
#include <stdio.h> #include <stdlib.h> #include <string.h> struct words { char word[255]; int count; struct words *next; } *pointer, *start; int statistics(); int main(void) { statistics(); return 0; } int statistics() { FILE *fp; int a; char quest[255]; fp=fopen("name", "r"); fscanf(fp, "%s", (char*)&quest[0]); pointer = (struct words *)malloc(sizeof(struct words)); start = pointer; if(pointer == NULL) return 0; strncpy(pointer->word, quest, 255); pointer->next = NULL; ++pointer->count; while(( a = fscanf(fp, "%s", (char*)&quest[0]))!= EOF) { pointer->next = (struct words *)malloc(sizeof(struct words)); /*should check allocation*/ pointer = pointer->next; pointer->next = NULL; ++pointer->count; strncpy(pointer->word, quest, 255); } pointer = start; while (pointer->next) { fprintf(stdout, "word->%s, count->%d\n", (char*)&pointer->word[0], pointer->count); pointer = pointer->next; } fprintf(stdout, "word->%s count->%d\n", (char*)&pointer->word[0], pointer->count); /*free allocated linked list or comment your letting the program's exit routine frre any allocated memory*/ return 0; }
Note it doesn't count the words - I'll leave that part to you
Also its been a while since I looked linked list....you might want to go over this code with a fine tooth comb..
Also - forgot, you should free the allocated linked list in the int statistics() function....the very least you should put in comments indicating that the program is short running(a once through) and your letting the program's exit routine free the linked list
Here's a quick and simple version of what your trying to do:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct words
{
char word[255];
int count;
struct words *next;
} *pointer, *start;
int statistics();
int main(void)
{
statistics();
return 0;
}
int statistics()
{
FILE *fp;
int a;
char quest[255];
fp=fopen("name", "r");
fscanf(fp, "%s", (char*)&quest[0]);
pointer = (struct words *)malloc(sizeof(struct words));
start = pointer;
if(pointer == NULL)
return 0;
strncpy(pointer->word, quest, 255);
pointer->next = NULL;
++pointer->count;
while(( a = fscanf(fp, "%s", (char*)&quest[0]))!= EOF)
{
pointer->next = (struct words *)malloc(sizeof(struct words));
/*should check allocation*/
pointer = pointer->next;
pointer->next = NULL;
++pointer->count;
strncpy(pointer->word, quest, 255);
}
pointer = start;
while (pointer->next)
{
fprintf(stdout, "word->%s, count->%d\n", (char*)&pointer->word[0], pointer->count);
pointer = pointer->next;
}
fprintf(stdout, "word->%s count->%d\n", (char*)&pointer->word[0], pointer->count);
return 0;
}
Note it doesn't count the words - I'll leave that part to you
Also its been a while since I looked linked list....you might want to go over this code with a fine tooth comb..
Here's a list of errors/warnings you should address before we look at the linked list
testit.c: In function ‘statistics’:
testit.c:33: error: ‘name’ undeclared (first use in this function)
testit.c:33: error: (Each undeclared identifier is reported only once
testit.c:33: error: for each function it appears in.)
testit.c:35: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char (*)[255]’
testit.c:46: error: ‘plik’ undeclared (first use in this function)
testit.c:46: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char (*)[255]’
testit.c:54: warning: implicit declaration of function ‘strcasecmp’
testit.c:85: error: ‘prevoius’ undeclared (first use in this function)
Probably a typo:
while((a=fscanf(plik, "%s", &quest))!= EOF)
What's plik?
Try investigating argv[0],,,example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_EXT ".c"
int main(int argc, char**argv)
{
char filename[256];
filename[0] = '\0';
fprintf(stdout, "argv[0]->%s\n", argv[0]);
/*strcat(filename, &argv[0][2]);*//*for linux dot slash ./filename*/
strcat(filename, argv[0]);
strcat(filename, FILE_EXT);
fprintf(stdout, "filename->%s\n", filename);
exit(EXIT_SUCCESS);
}
Thank you..
could you please explain the reason also in detail...
If you want to see the reasoning, try verifying the addresses that your passing.
start with the original address, then verify it in the first function and then verify that its the same in the second function.
I modified your code somewhat so you can see what's going on...
1. Run the code below, what values did you get for A, B, C, D, E ?
2. Now un comment the fgetc(stdin) lines and run the code again what are the values now
This should tell you exactly what's going on with your program and why its skipping every second prompt..
#include<stdio.h>
int main()
{
char A,B,C,D,E ;
int AA,BB,CC,DD,EE,res;
printf("\n1.Who amongst the following is the Head of the RBI at present ?\na.Mr. M.V.Kamath\nb.Mr. Y.V.Reddy\nc.Mr. N.R.Narayanmurthy\nd.Mr.O.P.Bhatt");
scanf("%c",&A);
/*fgetc(stdin);*/
printf("\n2. Mjority of rural people still prefer to go to which of the following for their credit needs ?\na.Money lenders\nb.Foreign Bankers\nc.NABARD\nd.RBI");
scanf("%c",&B);
/*fgetc(stdin);*/
printf("\n3. India has different categories of commercial banks.Which of the following is NOT one such categories ?\na.Private Banks\nb.Commodities Banks\nc.Nationalized Banks\nd.Cooperative Banks");
scanf("%c",&C);
/*fgetc(stdin);*/
printf("\n4.Which of the following countries does not play International Cricket?\na.Russia\nb.England\nc.SouthAfrica\nd.Pakistan");
scanf("%c",&D);
/*fgetc(stdin);*/
printf("\n5.Which is the world’s first credit card?\na.American Express\nb.Visa\nc.Mastercard\nd.Diners Club Card");
scanf("%c",&E);
/*fgetc(stdin);*/
fprintf(stdout, "a->%u\n", A);
fprintf(stdout, "b->%u\n", B);
fprintf(stdout, "c->%u\n", C);
fprintf(stdout, "d->%u\n", D);
fprintf(stdout, "e->%u\n", E);
return 0;
}
So would some compilers know that only freeing the list should free all its indices, or would I always get a memory leak with that? Either way, I want to know if there's an ANSI standard on this... which is probably the second method here if anything.
Thanks in advance for clearing up my confusion!
Number one its the memory manager that handles allocation/freeing of memory not the compiler. The compiler generates the code that calls the memory manager.
Basically you have to call free for each successful malloc you call....or you could exit the program and let the exit procedures clean up the memory for the program...
This should get you started:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAX_SITES 10 /* maximum number of sites allowed */
typedef struct
{
int site_id_num; /* id number of the site */
int wind_speed; /* the wind speed in knots */
int day_of_month; /* the day of the month written as dd */
int temperature; /* temp in celcius */
} measured_data_t;
int main(void)
{
int i = 0;
measured_data_t mydata[MAX_SITES];
for (i = 0; i < MAX_SITES; ++i)
{
fprintf(stdout, "collecting data for %d site\n", i + 1);
fputs("enter site id number->", stdout);
fscanf(stdin, "%d", &mydata[i].site_id_num);
}
for (i = 0; i < MAX_SITES; ++i)
{
fprintf(stdout, "data for %d site\n", i + 1);
fprintf(stdout, "site id->%d\n", mydata[i].site_id_num);
}
exit(EXIT_SUCCESS);
}
Number one its assignment to a structure and you have some major problems with your code...I'm looking at it right now.
Look at how I displayed the binary number...its much easier to read, besides that the code code looks O.K.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int reversebits( unsigned int n ); /* prototype */
int reversebits(unsigned int n)
{
unsigned int temp = n;
int i;
for(i = (sizeof(n) *8-1 ); i ; i--)
{
temp = temp | (n & 1);
temp = temp <<1;
n = n >>1;
}
temp = temp | (n & 1);
return temp;
}
int main()
{
int number;
int reversednumber;
int i;
int temp;
printf("\nEnter the number: ");
scanf("%d", &number);
reversednumber = reversebits(number);
printf("\n\n\nBits in the original number:\n\n");
for( i = 0; i < 32; i++)
{
if (0 == i % 4) fputc(' ', stdout);
temp = number >> i & 1;
printf("%d ",temp);
}
printf("\n\n\n\nBits in the reverse order:\n\n");
for( i = 0; i < 32; i++ )
{
if (0 == i % 4) fputc(' ', stdout);
temp = (reversednumber >> i ) & 1;
printf("%d ",temp);
}
fputs("\n", stdout);
exit(EXIT_SUCCESS);
}
Here's a little hack that'll work
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char ch;
char name[20];
printf("enter your name\n");
scanf("%s%c",name, &ch);
printf("enter single value\n");
ch=getchar();
fprintf(stdout, "name->%s, char->%c\n", name, ch);
exit(EXIT_SUCCESS);
}
That said you probably should write your porgam something like below
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char ch;
char name[20];
printf("enter your name\n");
fgets(name, 19, stdin);
printf("enter single value\n");
ch=getchar();
fprintf(stdout, "name->%s, char->%c\n", name, ch);
exit(EXIT_SUCCESS);
}
Right away you shouldn't use:
char name[20];
scanf("%s",&name);
It should be:
char name[20];
scanf("%s",name);
Wasn't this just posted?
Also main should return a value.
>I'm not arguing with you but global variables must be an accepted misconception..
It's well understood terminology, and everyone knows well enough what you mean when you say "global variable", so there's no point in being strictly correct when talking about them. It's like how dynamic arrays aren't really arrays. But everyone knows that when you say "dynamic array", you mean a simulated array with pointers and dynamic memory, so it's a convenient way to say the same thing with fewer words.However, something unconventional like differentiating between "external variables" and "global variables" is an entirely different matter. It's the first time I've heard that particular distinction, so I felt the need to clarify. ;)
It must be a throw back from Linux/Unix assembler
7.54 `.global SYMBOL', `.globl SYMBOL'
======================================
`.global' makes the symbol visible to `ld'. If you define SYMBOL in
your partial program, its value is made available to other partial
programs that are linked with it. Otherwise, SYMBOL takes its
attributes from a symbol of the same name from another file linked into
the same program.
If we look at this simple program and compile it but do not assemble we get
#include <stdio.h>
#include <stdlib.h>
char ch[] = "Hello, World!\n";
int x = 89;
static int y = 90;
int main(int argc, char**argv)
{
fputs(ch, stdout);
fprintf(stdout, "ans x->%d\n", x);
fprintf(stdout, "ans y->%d\n", y);
exit(EXIT_SUCCESS);
}
.file "testfile.c"
.globl ch
.data
.type ch, @object
.size …