gerard4143 371 Nearly a Posting Maven

Could you please post code using the proper code tags and please format your code.

As for problems, the main function should return an integer and why are you using the getch() function when the standard provides fgetc().

gerard4143 371 Nearly a Posting Maven

When you have a pointer defined like so

char *ptr = "hello world";

It creates a read only character pointer. If you require a char array that's writable then use.

char ptr[] = "hello world";
gerard4143 371 Nearly a Posting Maven
gerard4143 371 Nearly a Posting Maven

It creates a dynamic shared library that can be loaded and unloaded when the program is running, meaning you don't have to compile/link them during the compile/link cycle. Try looking up the functions in the library dlfcn.h - dlopen(), dlclose() and dlsym().

gerard4143 371 Nearly a Posting Maven

You can use the == operator to compare the elements of the character array which is what your doing in the above code. If you want to compare c-strings then you could use strcmp().

gerard4143 371 Nearly a Posting Maven

Try replacing the problems line with this.

(*ptrL)->ptrNext = ptrInput;
gerard4143 371 Nearly a Posting Maven

Here's a hint, I'll let you figure out the last loop.

#include <stdio.h>

int main()/*main returns an int*/
{
  int i, j, k;
  
  for( i = 1; i <= 3; i++)
  {
    for( j = 1; j <= 3 - i; j++)
      printf(" ");
      for( k = 1; k <= (2 * i - 1); k++)
	printf("*");
    printf("\n");
  }
  for( i = 1; i <= 2; i++)
  {
    for( j = 1; j <= i; j++)
      printf("-");
      /*you can figure out the last loop*/
    printf("\n");
  }
  return 0;/*main returns an int*/
}
gerard4143 371 Nearly a Posting Maven

I ran valgrind on your program and it produced this.

==10491== HEAP SUMMARY:
==10491==     in use at exit: 0 bytes in 0 blocks
==10491==   total heap usage: 4 allocs, 4 frees, 96 bytes allocated
==10491== 
==10491== All heap blocks were freed -- no leaks are possible

Which is good, no leaks possible.

Your second question, free allocated memory when its no longer needed.

gerard4143 371 Nearly a Posting Maven

Try calling the capacity() member function.

gerard4143 371 Nearly a Posting Maven

What I want to know is, what are you doing with the signed bit? Are you truncating it, are you toggling it? What are doing with it? If your truncating it then your function should be returning an unsigned int.

If you pass -64, 0 what's the result? If you pass 64, 0 what's the result?

gerard4143 371 Nearly a Posting Maven

Why don't you give a complete example of what you want...

If you pass a signed value, what's the value before and after the shift.
If you pass an unsigned value, what's the value before and after the shift.

gerard4143 371 Nearly a Posting Maven

Are you trying to toggle the signed bit and shift? Meaning, if the signed bit 0 then toggle it to 1 and if the signed bit is 1 then toggle it to 0.

gerard4143 371 Nearly a Posting Maven

The reason it fails for negative numbers is, your shifting the signed bit off. Try masking and saving the signed bit and then shift and then mask the signed bit back on.

gerard4143 371 Nearly a Posting Maven

Here's a tighter version.

#include <iostream>
#include <string>
#include <algorithm>
#include <locale>

std::string lower_string (const std::string& a_string, const std::locale& a_locale )
{
    char *str = new char[a_string.size() + 1];
    std::string::const_iterator begin = a_string.begin();
    std::string::const_iterator end = a_string.end();

    copy(begin, end, str);

    std::use_facet< std::ctype<char> > ( a_locale ).tolower ( str + 1, str + a_string.size() );

    std::string ans(str);
    delete [] str;

    return ans;
}

int main(int argc, char *argv[])
{
    std::locale loc("");
    std::string name("A nAME tO PASS aLONG");

    std::string ans = lower_string(name, loc);

    std::cout << ans << std::endl;
    return 0;
}

Its been a while since I look at C++ code.

gerard4143 371 Nearly a Posting Maven

I didn't take the time and read your post completely..Ooops

Try something like below.

#include <iostream>
#include <string>
#include <algorithm>
#include <locale>


std::string lower_string (const std::string& a_string, const std::locale& a_locale )
{
    char *str = new char(a_string.size());
    std::string::const_iterator begin = a_string.begin();
    std::string::const_iterator end = a_string.end();

    copy(begin, end, str);

    std::string ans;

    std::use_facet< std::ctype<char> > ( a_locale ).tolower ( str + 1, str + a_string.size() );

    ans = str;
    
    return ans;
}

int main(int argc, char *argv[])
{
    std::locale loc("");
    std::string name("A nAME tO PASS aLONG");

    std::string ans = lower_string(name, loc);

    std::cout << ans << std::endl;
    return 0;
}
gerard4143 371 Nearly a Posting Maven

So what you want is a function that creates a new string which is a copy of the string being past... Why not change the 'const std::string& a_string' parameter to 'std::string& a_string' and change original string or is that outside the requirements?

Your question, get the iterators for the string's begin and end and then increment the begin value by one.

gerard4143 371 Nearly a Posting Maven

Sorry Sir, but I am truly very depressed from redirection. I ve read many books, searched in google...I know what is redirection, but I ve nt understood yet how to do it ,what changes in the program shud be made. What is the relation between my compiled exe nd the function of those two fils used in redirection.

O.K. here's a small program that will redirect(from the command line) stdin to a data file.

testit.c

#include <stdio.h>

#define ARR_SIZE 200

int main()
{
  char ch[ARR_SIZE];
  
  while (fgets(ch, ARR_SIZE, stdin))/*read data file*/
    fputs(ch, stdout);
  return 0;
}

So if call this program with a data file say data1, it should display its contents.

testit < data1

How does it work? Well the operating system will redirect stdin to the open file descriptor of data1, so any read from stdin will read the file data1..Simple right?

Try running this program on your system and tell us if it works or fails.

gerard4143 371 Nearly a Posting Maven

You have this on line 104 - 110

static char* posielam = NULL;
/*...*/
do {
printf("Napis cestu:");
} while ((readd = getline(&posielam, &len, stdin)) == -1);

Your reading len characters(it should be len and not &len) into a c-string that points to NULL.

gerard4143 371 Nearly a Posting Maven

oh sorry,i did post this in c thread....i have a cpp file ....i tried both..cpp and c compiler but not progress.

You have a C++ library in your includes '#include <list>' so you have to use the C++ compiler.

gerard4143 371 Nearly a Posting Maven

Are you using a C compiler or C++. Your error message indicates a .cpp file?

gerard4143 371 Nearly a Posting Maven

First question, are you writing a C++ program or a C program? I ask because this is the C section and your error indicates a .cpp file.

gerard4143 371 Nearly a Posting Maven

and I have not linux in my system..
In windows,How to do it..

The code I posted lies within the C standard, so it should run on any machine that supports a C standard compiler. As for the redirection, that's operating system specific and I'll assume your operating system does support it..

The moral - This code should work on your system.

Thanks 4 the code..but ARRAY SIZE IS NOT defined in the program question

Your confused, this isn't a homework completion site its a site that guides programmers to answers with pointers.

gerard4143 371 Nearly a Posting Maven

Could you post the code you have so far, so we can see exactly where your having problems?

gerard4143 371 Nearly a Posting Maven

Read

The C Programming Langauge by Brian Kernighan and Dennis Ritchie
C Traps and Pitfalls by Andrew Koeing - Note I never read this particular book but have read others authored by Koeing and found the quality high.

If really want to understand C, then learn how the machine works. You can achieve this by learning some fundamental assembly programming which explains some of the whys you'll come across programming in C.

gerard4143 371 Nearly a Posting Maven

You could do it this way in Linux.

#include <stdio.h>

#define ARR_SIZE 200

int main()
{
  char ch[ARR_SIZE];
  
  fgets(ch, ARR_SIZE, stdin);/*read data1*/
  fputs(ch, stdout);/*write data2*/
  fgets(ch, ARR_SIZE, stdin);
  fputs(ch, stdout);
  fgets(ch, ARR_SIZE, stdin);
  fputs(ch, stdout);
  return 0;
}

Console line

./testfile < data1 > data2

gerard4143 371 Nearly a Posting Maven

Has anyone checked the date of this posting...Apr 6th, 2007

gerard4143 371 Nearly a Posting Maven

Here's your code cleaned up a little, please reads the comments.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

int main()
{

  FILE *open = NULL;/*all variables should defined at the start of the block*/
  int i = 0;
  char lines[300];
  
  if (!(open = fopen("vitranc.txt","r")))/*check to see if fopen failed*/
  {
    fputs("could not open file!\n", stderr);
    exit(EXIT_FAILURE);
  }

  while( true )
  {
    fgets(lines,300,open);
    
    if (feof(open)) break;
    
    for (i = 0; i < strlen(lines); ++i)
      lines[i] = tolower(lines[i]);
    
    printf("%s",lines);
  }
  printf("\n\n");

  fclose(open);
  
  return 0;/*main returns an int*/
	
}
gerard4143 371 Nearly a Posting Maven

The reason is failing to compile is this line of code.

lines = tolower(atoi(lines));

which should be

lines[index_value] = tolower(lines[index_value]);
gerard4143 371 Nearly a Posting Maven

so you do not need to read in the html file ?

Line 4 reads the file contents into a buffer and then line 6 writes that buffer into a socket.

gerard4143 371 Nearly a Posting Maven

Use the toupper() function.

gerard4143 371 Nearly a Posting Maven

Line 6 is wrong, it should be

n = write(newsock_filedesc,buffer, 256) ;
gerard4143 371 Nearly a Posting Maven

I tried the code below and it worked.

#include <iostream>
#include <cstdio>

using namespace std;

void writefile()
{
	char writefile[1000000];
	
	char openfile [20];
	
         FILE * pFile;
	
         cout << "\nFile name to open: ";
	 cin.getline(openfile, 20);
	
         pFile = fopen (openfile ,"w");

                   if (pFile == NULL) perror ("Error opening file");
		 else {
	
        cout<<"Enter text->";
	cin.getline(writefile, 1000000);

	fputs(writefile, pFile);
	fclose (pFile);
}
}

int main()
{
  writefile();
  return 0;
}
gerard4143 371 Nearly a Posting Maven

You posted code that doesn't contain a loop.

gerard4143 371 Nearly a Posting Maven

Memory pages on my system are 4096 bytes which is 0xfff so that line of code is getting the address of the memory page that contains the array mya. Why you ask? The mprotect() function requires that the address past to it be aligned on a page boundary.

gerard4143 371 Nearly a Posting Maven

Here's an example that displays 'Hello, World!'. Note this will only work on a Linux 64 bit PC.

#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>

unsigned char mya[] = 	{
			  0x50,0x57,0x56,0x52,0xe8,
			  0x00,0x00,0x00,0x00,0x5e,
			  0x48,0x81,0xc6,0x24,0x00,
			  0x00,0x00,0x48,0xc7,0xc0,
			  0x01,0x00,0x00,0x00,0x48,
			  0xc7,0xc7,0x01,0x00,0x00,
			  0x00,0x48,0xc7,0xc2,0x0e,
			  0x00,0x00,0x00,0x0f,0x05,
			  0x5a,0x5e,0x5f,0x5a,0xc3,
			  0x48,0x65,0x6c,0x6c,0x6f,
			  0x2c,0x20,0x57,0x6f,0x72,
			  0x6c,0x64,0x21,0x0a,0x00
			};
			
int main(int argc, char**argv)
{
  void *addr = (void*)((unsigned long)mya & ((0UL - 1UL) ^ 0xfff));/*get memory page*/
  int ans = mprotect(addr, 1, PROT_READ|PROT_WRITE|PROT_EXEC);/*set page attributes*/
  if (ans)
  {
    perror("mprotect");
    exit(EXIT_FAILURE);
  }
  
  ((void(*)(void))mya)();/*execute array*/
  
  return 0;
}
pseudorandom21 commented: Absolutely good, hard to find this information too. +1
ebrahim.amq commented: Hello dear , Please tell me how did you get the raw code of the function? +0
gerard4143 371 Nearly a Posting Maven

You'll have to change the memory attributes of the array to read and execute and then point the instruction pointer at it.

gerard4143 371 Nearly a Posting Maven

Yes a code snippet would be helpful, lets start by seeing yours.

gerard4143 371 Nearly a Posting Maven

On line 23, why won't you use isalpha().

gerard4143 371 Nearly a Posting Maven

Can we see what you have done so far?

gerard4143 371 Nearly a Posting Maven

@sourabh17

Your freeing y and then returning the free'd pointer to be used which is a big no no, plus you haven't freed the addition memory allocated in your for loop... Please read the previous postings for a better explanation.

gerard4143 371 Nearly a Posting Maven

@ Gerard:
urs is exactly the same as mine. the only difference is that you change all the my pointers to []. anyway it doesnt work

@nezachhem:
Wow, can u explain more in depth I dont get what ur saying, but I tried to flush the filePtr anyway. And it WORKSSSS

and how come I dont need to flush in my ubuntu system, but need to flush it in the university's server?

It probably has to do with the SSH connection...You should be doing it on your Ubuntu system as well.

gerard4143 371 Nearly a Posting Maven

Try this version which is mostly like yours..This works on my system.

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

int main()
{
  char buffer[256];
  char charTemp1[40];
  char charTemp2[40];
  int intTemp;
  fpos_t position;
  
  FILE *fd = NULL;
  
  if (!(fd = fopen("employee.dat", "r+")))
  {
    fputs("Could not open file!\n", stderr);
    exit(EXIT_FAILURE);
  }
  
  fgetpos(fd, &position);
  
  while (fgets(buffer, 256, fd) != NULL)
  {
    sscanf(buffer, "%d %s %s", &intTemp, charTemp1, charTemp2);
    
    charTemp1[0] = toupper(charTemp1[0]);
    charTemp2[0] = toupper(charTemp2[0]);
    
    fprintf(stdout, "%d %s %s\n", intTemp, charTemp1, charTemp2);
    fsetpos(fd, &position);
    fprintf(fd, "%d %s %s\n", intTemp, charTemp1, charTemp2);
    fgetpos(fd, &position);
  }
  
  fclose(fd);
  return 0;
}
gerard4143 371 Nearly a Posting Maven

The file would be in the same form

ID FIRSTNAME LASTNAME
10 efron berlian
20 jim smith
30 What Ever

the first line is just trash really and the rest is always number |space| string |space| string

Did you open the data files in nano or vim to verify the contents of the files are the same? I ask because the program seems to work in both cases and maybe its a case of the data files are not the same.

gerard4143 371 Nearly a Posting Maven

Are you sure you working on identical data files?

gerard4143 371 Nearly a Posting Maven

1.Is this step must? (since you havnt shown this in first post)
2.After freeing a in that way, why is

free(a)

needed?

You have to understand that each call to malloc returns an individual area of memory that has no association to other calls to malloc, so for each call to malloc you have to call free.

gerard4143 371 Nearly a Posting Maven

After line 45 put this

fgetc(stdin);

This is a simple but non-rigorous solution.

gerard4143 371 Nearly a Posting Maven

ah tyvm ^^

You'll have to translate that.

gerard4143 371 Nearly a Posting Maven

Line 42 only gets the character entered, its still leaves the new-line character in the stream so when you loop line 42 scanf's the new-line character and then produces the menu again.

gerard4143 371 Nearly a Posting Maven

You have a name collision with the standard library.

#include<iostream>

template <typename T> inline const T& max (const T & w,const T & x) { return w > x ? w : x; }

int main()
{
	int a = 10, b = 343;
	std::cout << max(a, b) << std::endl;
	std::cout << std::max(a, b) << std::endl;
	return 0;
}
gerard4143 371 Nearly a Posting Maven

A few pointers, the main function returns an integer and you shouldn't use magic numbers you should use a descriptive variable or macro..See below.

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

#define DIM_1 3/*don't use magic numbers, instead use a descriptive name*/
#define DIM_2 3/*don't use magic numbers, instead use a descriptive name*/

double **memalloc(int M,int N);

int main()/*main returns an int*/
{
	int i,j;  
	double **a;

	a = memalloc(DIM_1, DIM_2);

	for(i = 0; i < DIM_1; i++)
	{
		printf("\n");

		for(j = 0; j < DIM_2; j++)
		{
			a[i][j]=i+j;
			printf("%f\t",a[i][j]);	  
		}
	}

	fputc('\n', stdout);

	for(i = 0; i < DIM_1; i++)
	{
		free(a[i]);
	}

	free(a);

	return 0;
}

double **memalloc(int M,int N)
{
	int i;
	double **y;

	y = malloc(M* sizeof(double *));

	if(y == NULL)
	{
		printf("out of memory\n");
		return 0;
	}

	for(i = 0; i < M; i++)
	{
		y[i] = malloc(N * sizeof(double));
		if(y[i] == NULL)
		{
			printf("out of memory\n");
			return 0;
		}
	} 
	return y;
}