gerard4143 371 Nearly a Posting Maven

please help..
can u give me the codes... :(

Could you give money?

gerard4143 371 Nearly a Posting Maven

I modified your code somewhat to display the intial/final values of c1/c2....What do you notice?

#include <stdio.h>

int main()
{
	char c1 = 0, c2 = 0;
	
	fprintf(stdout, "our initial values c1->%d, c2->%d\n", c1, c2);

	printf("Enter a character: ");
	scanf("%c", &c1);
	printf("Enter another character: ");
	scanf("%c", &c2);

	fprintf(stdout, "\nour final values c1->%d, c2->%d\n", c1, c2);

	return 0;
}
gerard4143 371 Nearly a Posting Maven

Here's some code to demonstrate what happens when you cast an unsigned long to unsigned char...

#include <stdio.h>
#include <stdlib.h>

unsigned long myint = 1234567890;

int main(int argc, char**argv)
{
	int i = 0;
	unsigned char *cptr = (unsigned char*)&myint;/*cast myint to unsigned char*/

	for (i = 0; i < sizeof(unsigned long); ++i)
		fprintf(stdout, "char->%x\n", cptr[i]);/*display hex value of pointer as it walks along myint*/

	fprintf(stdout, "myint->%p\n", (void*)myint);/*dislay hex value of myint*/
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

I am trying to input a text file with twenty unsorted names in it into an array and then perform a bubblesort and output to a new file. The filename for the txt file is names.txt
Here is what I have so far....not much...I would appreciate some help the right direction. I have read quite a bit on the subject but it is not quite fitting together in my head yet.

#include<stdio.h>
#include<stdlib.h>

int main (void)
{
    
  FILE *fptr;
  
  
  fptr1=fopen("names.txt", "r");

The next step - did fopen return a valid FILE pointer...

gerard4143 371 Nearly a Posting Maven

I'm pretty sure you don't have a memory leak...an array out of bounds yes but memory leak no...

gerard4143 371 Nearly a Posting Maven

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);
}
gerard4143 371 Nearly a Posting Maven
void dfs(graph g)
{
	printf("whats wrong with me\n");
	for (int i = 0;i < g.number_vertices;i++)
	{
		g.vertices[i].color = WHITE;
		g.vertices[i].p = NIL;
	}
	time = 0;
	printf("in dfs\n");
	for (int i = 0;i < g.number_vertices;i++)
	{
		if (g.vertices[i].color == WHITE)
		{
//			dfs_visit(g, i);
			printf("i: %d\n",i);
		}
	}
}

Are you sure this is C...Look at the for loop

for (int i = 0;i < g.number_vertices;i++)

gerard4143 371 Nearly a Posting Maven

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);
}
gerard4143 371 Nearly a Posting Maven

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;
}
gerard4143 371 Nearly a Posting Maven

I guess there isn't very many 'leisurely bored' Linux programmers at Daniweb...

gerard4143 371 Nearly a Posting Maven

what constitutes "appearing to work" for you? not having the computer crash? if so, then yeah, it "appeared to work".

other than that, it was full of compiler warnings and gives nonsensical output.

Boo.

Your probably running a 32 bit machine...

gerard4143 371 Nearly a Posting Maven

Would it have taken long to give the code a quick test?

And there is no reason to call exit() every time you need to stop a program. A simple return will exit the program from main()

It was written/run once and appeared to work...that's quickly in my books.

gerard4143 371 Nearly a Posting Maven

When you want to left or right shift some bits...or splice some addresses together like below..So when would you need this skill? Hardware programming comes to mind..

#include <stdio.h>
#include <stdlib.h>

unsigned long two = 3345;

int main(int argc, char**argv)
{
	unsigned long one = 1234;
	void *addr = (void*)NULL;

	fprintf(stdout, "one addr->%p\n", (void*)&one);
	fprintf(stdout, "two addr->%p\n", (void*)&two);

	/*splice the first 12 bits of two onto the first 12 bits of one*/
	/*and save the result in addr*/

	addr = (void*)(((unsigned long)&one>>12<<12) + ((unsigned long)&two<<52>>52));	

	fprintf(stdout, "result addr->%p\n", addr);
	exit(EXIT_SUCCESS);
}

This was written quickly...I hope its correct..

jephthah commented: ridiculously confusing. -1
gerard4143 371 Nearly a Posting Maven

Here's an example of dup2, its was for someone who posted a pipe question here at daniweb...I forgot to post it so you can use it as an example

What does the program do? It forks for each row in the array and each child adds the array elements of its row and then reports the values back to the parent which sums them.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

#define ARR_ROW 5
#define ELEMENTS_PER_ROW 3

unsigned long longarray[ARR_ROW][ELEMENTS_PER_ROW] = 	{
								{1, 2, 3},
								{4, 5, 6},
								{7, 8, 9},
								{10, 11, 12},
								{13, 14, 15}
							};

enum PIPES {READ, WRITE};

int main(int argc, char**argv)
{
	unsigned long i = 0, tot = 0; 
	int hpipe[2];
	char ch[10];

	signal(SIGCHLD, SIG_IGN);
	pipe(hpipe);

	for (i = 0; i < ARR_ROW; ++i)
	{
		if (fork())
		{
		
		}
		else
		{
			int j = 0; 
			unsigned long ans = 0;
			close(hpipe[READ]);
			dup2(hpipe[WRITE], 1);
			close(hpipe[WRITE]);
			for (j = 0; j < ELEMENTS_PER_ROW; ++j)
				ans += longarray[i][j];
				
			fprintf(stdout, "%lu\n", ans);

			exit(EXIT_SUCCESS);
		}
	}

	close(hpipe[WRITE]);
	dup2(hpipe[READ], 0);
	close(hpipe[READ]);

	for (i = 0; i < ARR_ROW; ++i)
	{
		fgets(ch, 10, stdin);
		tot += strtol(ch, NULL, 10);
	}

	fprintf(stdout, "parent received a total of->%lu\n", tot);
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

Try closing the write end of your pipe...

Actually I think you may be better off looking into the function dup2().

gerard4143 371 Nearly a Posting Maven

You should check each file opening like below:

if (!(fptr1 = fopen("input.txt", "r")))
	{
		fputs("could not open input.txt!\n", stderr);
		exit(EXIT_FAILURE);
	}

Plus your loop can be simplified...get rid of

fscanf(fptr1, "%s", &check);

Its not required for this to work.

Also comments should be
/*comments are here*/
not
//comments are here

gerard4143 371 Nearly a Posting Maven

Actually a good place to start is investigating the options available with GCC(GNU compiler) and ld(GNU linker). With these two utilities you can build just about anything you want...

gerard4143 371 Nearly a Posting Maven

You know it took Linus Torvalds years to make the very first 'very simple' Linux kernel.

If your interested in this area ckeck out Minix.

gerard4143 371 Nearly a Posting Maven

Here's a very simple shell of a program...notice the closing of the read/write ends of the pipe..

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int mystrlen(char *s)
{
	int i = 0;
	while (*s++)
		++i;
	return i;
}

char ch[] = "this is the message to pass along to the other end!\n";

enum PIPES {READ, WRITE};

int main(int argc, char**argv)
{
	char ans;
	int result = 0;
	int hpipe[2];
	pipe(hpipe);

	result = write(hpipe[WRITE], ch, mystrlen(ch));
	close(hpipe[WRITE]);
	
	while (read(hpipe[READ], (char*)&ans, sizeof(char)))
	{
		fputc(ans, stdout);
	}
	close(hpipe[READ]);
	exit(EXIT_SUCCESS);
}
gerard4143 371 Nearly a Posting Maven

Yup...have some suggestions...

1. use read and write instead of fread and fwrite

gerard4143 371 Nearly a Posting Maven

Please check the result of this action:

fptr1=fopen("input.txt", "r");
gerard4143 371 Nearly a Posting Maven
gerard4143 371 Nearly a Posting Maven

Right from my linux/GNU man file

BUGS
Never use gets(). Because it is impossible to tell without knowing the data in advance
how many characters gets() will read, and because gets() will continue to store charac‐
ters past the end of the buffer, it is extremely dangerous to use. It has been used to
break computer security. Use fgets() instead.

Salem commented: Indeed, it is so. +19
gerard4143 371 Nearly a Posting Maven

Yeah that line was the conflict but it was actually because I forgot to use the value as an index in the catArray that I defined earlier.

But I am still perplexed why everyone else's compilers are throwing warnings but my school's isn't... I would have caught this 5 hours ago! I suppose my school is running an older version of gcc or something.th

If your using gcc try:

gcc filename.c -Wall -asni -pedantic-errors -o filename

gerard4143 371 Nearly a Posting Maven

You had many minor errors/warnings in your code but the culprit was a print function on line 151 where you had %s instead of %d...

/*#define _GNU_SOURCE*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HLIMIT 120
#define MLIMIT 50
#define LLIMIT 6
#define DATE_SIZE 18
#define OPTIONS 4
#define NUM_CATS 7


typedef struct mediaItem_ {
	char doc[DATE_SIZE];
	int  size;
	char desc[MLIMIT];
	int category;
	char location[HLIMIT];
	char creator[MLIMIT];
} mediaItem;

mediaItem *items = NULL;
int	num_items = 0;
int num_allocated = 0;
char *catArray[] ={"OS", "APP", "FILM", "MUSIC", "MUSIC VIDEO", "VIDEO GAME", "OTHER"};


void bad_selection(char *value){
	if(value[0] == '\n')
        printf("No selection detected. Please try again.\n");
	else
		printf("\n\'%s\' is not a valid response. Please try again.\n", value);
}

int addItems (mediaItem item){
	if(num_items == num_allocated)
	{
		void *_tmp;
		if(num_allocated == 0)
			num_allocated = 8;
		else
			num_allocated += num_allocated/2;

		_tmp = realloc(items, (num_allocated * sizeof(mediaItem)));

		if(!_tmp){
			fprintf(stderr,
				"ERROR: Unable to allocate more memory for additonal items\n");
			return(-1);
		}

		items = (mediaItem*)_tmp;
	}
	items[num_items++] = item;

	return num_items;
}

int remItems(int itemNum){
	if(num_items == 0 || itemNum >= num_items || itemNum < 0)
		return -1;
	if(itemNum < num_items -1){
		int i = itemNum;
		for(;i < num_items; ++i){
			if(i < num_items - 1)
				items[i] = items[i+1];
		}
	}
	return --num_items;
}

void intro(){
    printf("Hello and welcome to Media Map\n\n");
}

void bye(){
    printf("\nThank you for using Media Map!\n\n");
}

void show_menu(){
    printf("\nPlease select from the following choices:\n");
    printf("1. List Media\n");
    printf("2. Add Media\n");
    printf("3. Delete Media\n");
    printf("4. Exit\n");
}

void show_cats(){
	int i;
	printf("Choose …
gerard4143 371 Nearly a Posting Maven

You need to post 255 lines of code to ask a question about an array of structures?

gerard4143 371 Nearly a Posting Maven

This is a Linux brainteaser for the C comfortable and leisurely bored. I'm not looking for the answer..

Note the compile lines are commented at the bottom of the program - Ubuntu and derivatives please use the second compile line or it will not work.

So what does this program do? This program, when compiled and run, will display the addresses and values of the array myint[] plus the addresses and values of my calculated addresses...So why does this work? Why does two different memory regions appear to have the same data?

Disclaimer – I can't guarantee this will work on every Linux distro out there but it should work on most Intel/AMDs.

#include <stdio.h>
#include <stdlib.h>

unsigned long myint[] = 	{
					111111, 
					222222, 
					333333, 
					444444, 
					555555, 
					666666, 
					777777, 
					888888, 
					999999
				};

int main(int argc, char**argv)
{
	int i = 0;

	/*calculate our address from main and myint*/
	void *textaddr = (void*)((unsigned long)&main & ((0UL - 1UL) ^ 0xfff));
	void *dataoff = (void*)((unsigned long)&myint & 0xfff);

	/*our new address which has the values of myint*/
	unsigned long *ans = (unsigned long*)((unsigned long)dataoff + (unsigned long)textaddr);

	for (i = 0; i < 9; ++i)
	{
		fprintf(stdout, "ans->%lu addr->%p\n", *(ans + i) ,(void*)(ans + i));
	}

	fprintf(stdout, "The values/address of the array myint!\n");

	for (i = 0; i < 9; ++i)
	{
		fprintf(stdout, "ans->%lu addr->%p\n",myint[i] , (void*)&myint[i]);
	}	
	exit(EXIT_SUCCESS);
}

compile lines

Most linux distros Mandriva, Arch, Slackware etc can use this compile line

gcc linuxmem.c -Wall …
gerard4143 371 Nearly a Posting Maven

You realy should google C functions and C control statements....You'll find the answers in these queries...

gerard4143 371 Nearly a Posting Maven
gerard4143 371 Nearly a Posting Maven

Shouldn't this end with a NULL

execve(argv[0], argv, envp, NULL);

Ooops my mistake.

gerard4143 371 Nearly a Posting Maven

what i meant is the data files is on another hardrive...
my partition table is:
/dev/sda3 -this is my C: drive
/dev/sda4 - this is my / partition
/dev/sda5 - my D: drive
/dev/sda6 - my swap partition

my data is in in sda5.
Thanks for the reply mate!

your only showing sda# so this indicates only one drive with many partitions..

gerard4143 371 Nearly a Posting Maven

You said you'd like to write a program - well what do you have so far.

gerard4143 371 Nearly a Posting Maven

Try this:

int main(int argc, char* argv[])
{
         printf("%lf\n",(0.99F - 0.90F - 0.09F));

}
gerard4143 371 Nearly a Posting Maven

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...

gerard4143 371 Nearly a Posting Maven

You should get a table of ascii characters and look up the character equivalent for 10.

gerard4143 371 Nearly a Posting Maven

Try googling C format specifiers..

gerard4143 371 Nearly a Posting Maven
gerard4143 371 Nearly a Posting Maven

As a quick response, I would suggest initializing these local variables to zero.

int n,teens,thousands,hundreds,tens,ones,tthousands;

gerard4143 371 Nearly a Posting Maven

putting:

gdb program

example stepping through I see switch between assembly and C.

Current language:  auto; currently asm
(gdb) fin
Run till exit from #0  listen () at ../sysdeps/unix/sysv/linux/i386/socket.S:51
0x08049951 in connect_ftp (options_data=0xbffff618) at example.c:429
429                     if (listen(data_sock,MAX_PENDING_REQUESTS) < 0)
Current language:  auto; currently c

want it to show only C code only and not show assembly. Could be system options somewhere(which I can't directly access)? Using Fedora.

The thing is, if you have inline assembler or assembler object files then a C translation may not(probably not) possible...You can do things in assembler that have no valid C representation..

gerard4143 371 Nearly a Posting Maven

In this case where you have Vista - pre-installed - I would clear your hard drive then 1. install Vista from recovery disk(s) 2. run scandisk and 3. install Ubuntu, configuring it to dual boot.

gerard4143 371 Nearly a Posting Maven
gerard4143 371 Nearly a Posting Maven

Well if I had to do this, I would investigate binary trees or get the book "The C Programming Language" which has an example of this code...

gerard4143 371 Nearly a Posting Maven

Is there a way to hide assembly code when my C code references a library function that deals with memory? (Not showing 'asm' code)?

Maybe if you gave us an example of what your trying to accomplish...Because I'm having a hard time understanding what you mean...

gerard4143 371 Nearly a Posting Maven

Well I guess your not completely switch. I would reinstall Vista and run Ubuntu via Virtual Box since you have the install disks for Ubuntu.

But for some reason I want vista in virtaul machine.

Sounds like your unsure of your reasoning here...

gerard4143 371 Nearly a Posting Maven

About

gets(name);

This is what manpages has to says about gets()

BUGS
Never use gets(). Because it is impossible to tell without knowing the
data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it
is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

Salem commented: Damn straight! +19
gerard4143 371 Nearly a Posting Maven

Could we see what you have so far...

gerard4143 371 Nearly a Posting Maven

Volatile tells the compiler that a variable may be changed in ways not explicitly specified by the program - According to C: The complete Reference. This means the volatile variable may be changed by forces outside of the current process(usually the kernel, hardware or another process) so don't make any assumption about its current value...

gerard4143 371 Nearly a Posting Maven

What I can make out from your postings is - your using Windows to run Cygwin. Is that correct? If you are then try reinstalling Cygwin because you seem to be missing some of your libraries.....

gerard4143 371 Nearly a Posting Maven

I did a search on my hard drive and could not find the file sem.h, so I downloaded one. Where would I place it?

Downloading a header file will do nothing, you will need the compiled library or object file so that the main executable can link to it...Are you sure that this is a valid Windows file?

check this windows link that uses semaphores:

http://msdn.microsoft.com/en-us/library/ms686946(VS.85).aspx

gerard4143 371 Nearly a Posting Maven

Sorry. I am on Windows 7 Pro

I know that file exists in Linux but I'm not so sure about Windows...