jephthah 1,888 Posting Maven

Line 22: is that the one with perror()? I don't see anything wrong with it.

reading the context of the error, you can see that line 22 on his side doesn't match up with line 22 as they are numbered here. his line 22, the one with the error, is :

res = sem_init(&A,B,C 0, 0);

obviously, you are not passing in the expected argument types to sem_init. the first argument must be a pointer to a structure of type sem_t. the second an int. the third an unsigned int.

all of your parameters are void pointers. and you've got an extraneous fourth (and maybe fifth??) argument, not sure why the extra stuff is there.

you have other problems as well, but you need to fix this first, then recompile.

you should review sem_init(), as well as the rest of the <sempaphore.h> library. working through an example will help you tremendously.


.

jephthah 1,888 Posting Maven

okay, thanks :$

jephthah 1,888 Posting Maven

How about stopping the loop without the !eof() ? eof() is not a good solution as the exit control of a loop. EOF would be true only after it is read and not when it is reached. char oneByte; must be an int, since fgetc() returns an int, and it is not a guarantee that char would be signed.

you're right about the byte should be type int, not char. it may possibly fail for some compilers. i don't know why i keep doing that, i know better.

however, i disagree with you about the eof() .. i think that's a valid way to exit the loop. unless you can remind me why it's not.

jephthah 1,888 Posting Maven

following his posted .TXT file example, i can see that Des_Moines and Los_Angeles will work just fine.

:P

jephthah 1,888 Posting Maven

NIIT course programs are weak, their faculty is mediocre and lacking in research and scholarship. the 4 year programs keep you there for 2 years longer than you need to be, at overpriced tuition rates to jack up your bill. of course they don't care that you'll run yourself into debt with student loans... they get paid.

make no mistake, NIIT is a for-profit business. like all for-profits, they are focused their bottom line -- which is selling their product to you at the best profit margin they can manage.

meanwhile you'll be in debt and working at a call center trying to scramble your way out from the masses who are all competing against you for the better design jobs.

what kind of job you get is going to be based solely on your effort, not the overpriced and overrated 4-year tech degree from this college, when a 2 year tech degree will teach you everything you need to know to get an entry-level job in IT.

IT is not rocket science. It's not medicine. It's not even engineering. so don't waste an additional 2 years mucking around with dance classes and development of eastern philosophy in between programming "hello world" on an antiquated Turbo C compiler.

what you need to learn is how to configure network switches and splice CAT-6 ethernet cable, write shell scripts and set up a LAMP environment. et cetera. this is stuff that you will learn at any reputable …

jephthah 1,888 Posting Maven

copying byte by bye using loop...uh...sounds like a complected process...:|

uh... not so much :)

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

int main(int argc, char **argv)
{
   FILE *fp_source, *fp_dest;
   char oneByte;

   /* WARNING:  this does ZERO error checking!! */

   fp_source = fopen(argv[1], "rb"));
   fp_dest   = fopen(argv[2], "wb"));

   while(!feof(fp_source)) {
      oneByte = fgetc(fp_source);
      fputc(oneByte, fp_dest);
   }

   fclose(fp_source);
   fclose(fp_dest);

   return 0;
}

.

jephthah 1,888 Posting Maven

you can't say values[x].city = s ... you must do something like strcpy(values[x].city, s) ... also dont put the "&s" when getting a string. just put "s"

while (fscanf(inp,"%s %d",s, &n) != EOF) 
	{
		strcpy(values[x].city, s);
		values[x].temp = n;
		x++;
	}

the reason is that "s" and "values[x].city" are actually pointers to a character array, and you can't simply make an assignment to copy the array.

jephthah 1,888 Posting Maven

if anyone is there whom i trust blindly, it's you

um, please don't do that... :P


trained by some of best programmers in the globe in this language

the sentiment is appreciated, but far from reality. Narue is probably the closest this forum has to a guru. the rest of are still in various stages of learning.

speaking only for myself, i'm still trying to master the basics.

the point is, be ready to question everything. websites, textbooks, and especially "experts"

.

jephthah 1,888 Posting Maven

This is my last reply for this thread. Google it up you come to know everything!!!!

says the guy who knows everything through Google. yeah, Turbo C. rock on, dude. :icon_rolleyes:

it's clear why you spend your time in here giving "Google advice" rather than in any structured programming language forums solving code problems .

seriously, Chichiro, you'd do well to stay away from NIIT unless you're looking for a career in telephone tech support

.

jephthah 1,888 Posting Maven

i'm not seeing how there's any fundamental difference between a mercedes, ferrari, or a bentley.

jephthah 1,888 Posting Maven

you can copy any file, whatever the type or extension.

just open the source file as a binary file and use a while loop to copy each byte one at a time to the destination, until the EOF charater is found.

see fopen(), fgetc(), fputc(), and feof() in the standard C library <stdio.h>. that's all the functions you'll need, though you'll probably want to include error checking with ferror().


.

jephthah 1,888 Posting Maven

in the ... link they are saying, user should definitely NOT [use fflush(stdout) to clear the output buffer.]

that webpage is a joke. a bad joke. they actually instruct you to use gets() as an example of "safe code". this right there tells me they're talking out their ass.

for example here jephthah gave a solution where he used fflush after printf and mentioned // printf did not have newline, need to flush

if you don't print a newline, but want the line to display to the terminal, you should use fflush(stdout);. Your system may not need this, but some systems will not print text to the output buffer until a newline is found or the buffer is otherwise flushed.

Narue has explained it better than i can. i'm only commenting here because my name was dropped :icon_wink:

jephthah 1,888 Posting Maven

no loops, no library functions, no pointers, no recursion ...

well, i don't know what else is left. you've got to use something

jephthah 1,888 Posting Maven

Adak,

such a generous welcome you've given arshad and aliakseis to our humble forum.

i know they've been waiting patiently for 3-1/2 years for someone like you to come along, address their needs, and see that they are comfortable.

my only concern is that you've been around here since June of 2008. now tell me what, exactly, has taken you so long to see to these people?? :icon_frown: you really should apologize for making them wait

WaltP commented: Stop being an ahole. ADAK was responding to Lucy who resurrected the thread. Maybe you should stick to posting aid rather than your rude opinions. -2
jephthah 1,888 Posting Maven

gcc supports an extended precison "long double" type typically 10 or 12 bytes

but Windows XP always maps the "long double" to the standard double (typically 8 bytes)

MinGW on windows XP thinks it has a 12-byte "long double" but in fact it only has the 8 byte support on Windows XP. any long double math only has 8 byte precision and must be recast and printed with the standard double floating point format specifier (%f)

does anyone know if Windows Vista or Win 7 has extended precision for long doubles, or does it merely map them to the standard double?

jephthah 1,888 Posting Maven

"isPrime" is just a flag that gets set or cleared to indicate whether or not the current number being tested is prime.

if isPrime = 0, then it is not prime.
if isPrime = 1, then it is prime.

here is the process by which it works:

the outer for loop for (number=lower;number<=upper;number++) tests each number within the range desired. before we start testing each number, we set isPrime = 1; which is to say that we will assume each number is prime until we later find otherwise.

so then we go into the second (inner) loop and test each of the appropriate divisors against the number. IF the number is found to be divisible by any one of the divisors, the number is "not prime" and so we clear isPrime = 0 and break; out of the inner loop -- there's no point in testing any more divisors, since the number is not prime.

once we're out of the second (inner) loop -- whether it's because we (a) found a divisor that evenly divided the number and broke out early, or (b) because we checked all the divisors and none of them evenly divided the number -- the determination of the number being prime or not will be reflected by the value of "isPrime".... if "isPrime" is 1, then the number is prime. if "isPrime" is 0, then the number is not prime.

we then index the outer loop to the next …

jephthah 1,888 Posting Maven

i hate it when i dream in VGA mode. :(

jephthah 1,888 Posting Maven

look i'm just going to give this to you, because it's obvious you're trying, and you really need to get some better coding practices.

study how i've modified your code, noting a few key points:

-- the second (inner) for loop should not be testing divisors across the entire range, but only up to a number less than the number being divided. in fact, this "maximum divisor" should not be more than half the number being tested, because it's meaningless to test divisors larger than half of the number being tested, and will just add unnecessary computations.

-- you need to test *each* of the appropriate divisors, and *none* of those divisors should be evenly divisible into the number in order for that number to be considered prime.

-- name your variables with meaningful names

-- comment your code heavily, explaining each fundamental block.

-- quit using <conio> functions like "clrscr" and "getche"

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

int main(void)
{
   int lower, upper, number, divisor, maxDivisor, remainder, isPrime;

   printf("\nEnter Lower Number of Range: ");
   fflush(stdout);  // in case lack of newline blocks buffer
   scanf ("%d",&lower);

   printf("Enter Upper Number of Range: ");
   fflush(stdout);  // in case lack of newline blocks buffer
   scanf ("%d",&upper);

   printf("\nPrimes Numbers in this Range are:\n\n");

   // cycle through each number within the range of values requested

   for (number = lower ; number <= upper; number++)
   {
      isPrime = 1;  // assume each is a prime unless found otherwise

      // for …
Xufyan commented: ummaah... +0
jephthah 1,888 Posting Maven

it wasnt working to begin with. try to run your original code with the same range.

AD just fixed some of the blatant loop error to keep it from repeat printing. the problem is your choice of variables in which to base your loops, and the fact that you print the number as a prime on the first instance of it not being divisible by some number. just because 9 is not divisible by 2, doesnt mean that it's prime. obviously, it's divisible by 3. yet, as soon as it's found to not be divisible by 2, you print it as being prime.

your problem is also the way you name your variables. a, b, c, d, e, f... could you be any more cryptic? this is bad practice that lends itself to unintelligible code that is difficult to debug. and it will be full of bugs, because the programmer himself cant understand what is going on.

.

jephthah 1,888 Posting Maven

this doesnt work. try to find primes from 2 to 30:

2
30
3 5 7 9 11 13 15 17 19 21 23 25 27 29

Process returned 0 (0x0)   execution time : 5.015 s
Press any key to continue.
jephthah 1,888 Posting Maven

hi....everyone


i am new here and welcome daniweb forums....

great! Welcome! Im glad youre here!

Because we need more people to bump 6 year old threads to promote their spammy sig links!

:) :) :) :)

jephthah 1,888 Posting Maven

because we sure do need all these new users posting half-assed, me too, or completely wrong "solutions" to multi-year-old threads that had already long since been resolved.

like this
and this
and this
and this
and this
and this

I could go on, because that's just a sample from the past 10 days or so in C. i havent even started on other forums.

Salem commented: Careful, you might trigger the non-existant link-spam detection s/w when you post too many links ;) +0
jephthah 1,888 Posting Maven

Hi friends... This works.. Try out..
Its the corrected one

so, when you say "works" ... you really mean "crashes with a fatal runtime error", right?

thanks for resurrecting a 4 year old thread to post your non-working crap code.

jephthah 1,888 Posting Maven

what Banfa said about your character array's size and the number of characters read by fgets() must match.

but you don't necessarily have to allocate your "numbers" array dynamically. you can, for simplicity's sake, just declare it to be some arbitrarily large size.

double numbers[100];

since these are "doubles" (double floating point) you can not use "atoi" but must use "atod". you must also use the format specifier "%f" when printing.

numbers[i]=atod(line); /* convert string to double float*/

you also can not use "sizeof(numbers)" in your for loop like you think you can, for two reasons. first reason is that (at least with a static array size) the number of elements used will vary. the second reason, even if you were to dynamically allocate the array, is that the size of the array is equal to the number of bytes used and not the number of values contained. each double value uses at least 8 bytes.

so if you were to dynamically allocate your array size with malloc, you could use sizeof to determine the number of elements if you divide the array size by the size of each element sizeof(numbers)/sizeof(double) , but i think a simpler way is to just use the count of the number of elements that you read, since you are already counting them anyhow

totalNums = i;
for (i=0 ; i<totalNums ; i++) { 
    printf("\nThe number %d of %d is: %f", i+1, totalNums, numbers[i]); 
}

.

jephthah 1,888 Posting Maven

my mistake once again. the first way i showed is correct:

while ( fgets(readbuffer, MAX_LINE_LENGTH, readhandle) != NULL )

fgets will return NULL if the end of file is reached. i got mixed up with another function that returns EOF, which is a negative number, so i mistakenly thought i needed to change the condition to > 0 .

that second way will work, also, because the NULL pointer evaluates as zero. but the first way is technically the correct way.

sorry for the confusion. I'm at work, and i often post things in a hurry without taking care to verify that my memory is accurate.


.

jephthah 1,888 Posting Maven

sounds like you should go for a 2-year certificate program or AAS degree rather than a BS, since you seem more of a hands-on guy who wants to get busy right away. the entrepreneurial aspects of what you want to do aren't going to be helped by a 4 year degree, especially in the IT/IS arena

BS degrees will get you bogged down in theory and extraneous stuff. now this is great if you wnat to be an engineer, or have plans to carry on to a Masters degree, but for the IT/IS field, all the 4 year degrees really get you is deeper in debt for student loans. all you really need to know for IT/IS can be taught in a couple years anyhow. the rest is on-the-job experience

be careful of the online programs. a lot of people have bad experiences.

jephthah 1,888 Posting Maven

just go to a used computer parts and accessories store near you and buy an old ZIP drive.. remember the Iomega ZIP drives from the late 90s? you can still get them, and drivers are available for both Win98 and WinXP

use it to transfer all your important files from the old machine to the new.

the drawback is the zip drives are typically limited to 100MB or so, so it may take a few rounds depending on what you've got.

but overall, it's probably the easist and cheapest way to do it, and a helluva lot faster than using 1.44 MB floppy disks

jephthah 1,888 Posting Maven

all computer arithmetic is binary. base 10 output is but an illusion. desire only leads to suffering. you must transcend it if you are to become boddhisattva.


.

jephthah 1,888 Posting Maven

and i agree with you. :)

jephthah 1,888 Posting Maven

So database management would be suitable? How about unix? what do they do there? and isn't unix used for managing too? Is database management a good edge on job applications?

oh, yes. absolutely.

and when i speak of managment, i mean you should consider getting an MBA.

tell your potential employers your 5-year goal is to start an MBA program.

you'll do just fine.

jephthah 1,888 Posting Maven

youre compiling a C++ file.

jephthah 1,888 Posting Maven

Create the header file func.n and include all the function declarations in this file.
In the other C file, use extern followed by the name of the function.

no, that is entirely incorrect. please do not post wrong answers after someone has already correctly answered the question.

jgehlot, what Walt posted is correct, just do what he said

jephthah 1,888 Posting Maven

oops i always get this damn thing wrong... sorry, i never get user input from terminal screen at work, its always through guis.

the while statement should continue as long as return of fgets() is >0, not for just != NULL ... so try this instead:

readhandle = fopen(readfilename,"r")
writehandle = fopen(writefilename,"w")

while ( fgets(readbuffer, sizeof(readbuffer), readhandle) > 0 )
   fprintf(writehandle, "%d %s", strlen(readbuffer), readbuffer);

fclose(readhandle);
fclose(writehandle);
jephthah 1,888 Posting Maven

lol

jephthah 1,888 Posting Maven

As noted, ncurses isn't a relative of conio; it's said to share a common ancestor (pcurses) with PDCurses.

I said "I beleive conio was basically a windows port of pcurses. ". You said ncurses "share[s] a common ancestor (pcurses) with PDcurses.". Conio is so almost identical to PDCurses, it would be hard to believe it was not a direct port.

I never claimed it was a direct descendant, but that sure sounds related to me. So when i said "ncurses is the unix counterpart to DOS's conio." that might be an anecdotal assessment of the current environment, but I don't think my statements are incorrect.


All of these do line-drawing, using characters (pseudographics).

well, this is a bit of semantics. I don't consider ascii blocks to be "graphics", any more than i consider pseudocode to be "code". but i suppose there's room for argument, and the letter of the law may be on your side. Perhaps.


Obsolete means that it's not used anymore (not correct).

Obsolete: no longer in general use; fallen into disuse. of a discarded or outmoded type; out of date.

this is again semantics, but i still say conio is obsolete. and if it's not, then it should be. It's definitely deprecated, as it's clearly something "programmers are discouraged from using" because it is "highly inefficient" and "encourages bad coding practices" and "a better alternative exists"

I admit ncurses is neither deprecated nor obsolete because linux/unix …

jephthah 1,888 Posting Maven

glad it helped.

side note, you can write any kind of file you want, the question is will application "X" understand it.

.CSV file just means "comma separated values" and this extension is typically opened by Excel on default. ally comma-separated values (strings or numbers) will appear across the columns, newlines will move down to the next row.

A true Excel file is an .XLS file and you can not very easily write a valid .XLS file without going through a lot of contortions with the Windows API and OLE Automation.

you could have also easily written a .TXT file and just opened it in any basic text editor such as WordPad or TextPad.


.

jephthah 1,888 Posting Maven

thread hijack. this thread is a year old.

read the rules.

make your own thread.

use code tags.

jephthah 1,888 Posting Maven

change printf to fprintf and write to a .csv file

FILE * fp = fopen("mydata.csv", "w");  // opens file to write ("w"), using the FILE pointer "fp"

//...

printf("16 Bit:\n\n");
  for (n=0;n<numberOfValues;n++){
    fprintf(fp, "%.4x\n", rand16());   // prints to the file, using the file pointer "fp"

//...

fclose(fp);  // close the file after you're done

}

oggiemc commented: straight to the point once again, very helpful poster +1
jephthah 1,888 Posting Maven

i, too, hate the geico commercials. it's stale and unfunny and plain annoying. the radio spots, at least. i don't watch tv.

jephthah 1,888 Posting Maven

hey, didn't we just have this conversation over in C, like, a day ago?

basically it's three simple things:

-- post your code when you want specific help.
-- use [code] tags so we can read your code.
-- don't hijack other people's threads.

bumping a 2 year old thread is considered hijacking. or simply resurrecting dead threads. either way... cut it out.

jephthah 1,888 Posting Maven

I'm totally confused. Why is BestJew getting blamed for digging up an old thread? and why is BestJew apologizing? i don't see where any of this is his fault....

jephthah 1,888 Posting Maven

I didn't try it because by the looks of it, the code continually prints out a fixed stream of 80 bytes until the end of the file

No. It doesn't do that at all.

so what's to prevent it from printing past the end of a file?

Oh, just maybe the function prototype. Or that thing called the C standard library. Thing like that. However you prefer to think of it.


Maybe you should stop ignoring what other people say, ie substituting in "blah blah" for my posts.

i didn't substitute "blah blah". i summarized the context.

Now maybe YOU should stop ignoring the free help you get and first understand the functions -- or at least look them up -- before summarily dismissing them by what it "looks like"

:icon_rolleyes:


.

jephthah 1,888 Posting Maven

Gerard, won't that code possibly print out bytes past the end of the file?

no it doesnt. yes, his code works. do you even bother to try anything?

Jephthah, I'm asking you for a method, not for you to write my program.

I've given you a method. on further review, though, it does need a small change.

while (fgets(buffer, sizeof(buffer), fileHandle1) > 0)
fprintf(filehandle2, "%s", buffer);

oops. my bad.


.

jephthah 1,888 Posting Maven

I guess I just have to make up [a bunch of silliness] How does this sound?

sounds like you haven't even tried what i suggested. Go do what you want.

You could try something like below:

Gerard, you just typed up a fully-working code that uses practically the exact same method I've been saying all thread with the single exception that you use fwrite and fread instead of fgets and fprintf.

so how does this add to work ive been doing here, other than to "me too" a thread and give away full code to someones homework assignment?

jephthah 1,888 Posting Maven

n/m.

jephthah 1,888 Posting Maven

How would it handle a file pointer different from a char pointer? I can't use fgets because of binary files though, but fread and maybe 80 bytes will work fine.

EDIT - it has to be an exact number of bytes output...*sigh* so 80 fixed won't work.

okay, you're getting out in the weeds now.

fgets will read binary, you just have to open the file for a binary read FILE * fp = fopen(filename,"rb"); .

and fgets(buffer, numbytes, filestream) will read up to (numbytes - 1) or a newline or EOF character whichever comes first.

read the prototype for fgets


EDIT: you dont need to add 1 to the size of the buffer. fgets() will get one less bytes than the number of bytes argument, and tag the terminating NULL after that.


.

jephthah 1,888 Posting Maven

it doesnt work because file pointers and char pointers are two different things.

short answer: if they weren't different, they wouldn't have different names.

long answer: a char pointer is just a memory address that points to a location in memory of a group of characters. a file pointer is an integer "handle" that identifies a file stream and is associated with a structure that describes all the elements of that file stream.

jephthah 1,888 Posting Maven

no. not at all.

it doesnt get any more convenient than the example i handed you.

jephthah 1,888 Posting Maven

man, you're starting to sound helpless. the link i gave you gives every single time.h function and prototype. a basic search will find examples such as this:

/* localtime example */
#include <stdio.h>
#include <time.h>

int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "Current local time and date: %s", asctime (timeinfo) );
  
  return 0;
}
jephthah 1,888 Posting Maven

Yea I know but

Well, if you knew they were there, then why did you ask if there were any functions?


how you would display to the screen. Ex: The current date is 07/04/210.

um, okay...

printf("Ex: The current date is 07/04/210.");