Friday, October 30, 2020

,

Example program for slicing in python - python __slice__

  •  slice() function in python is used to sliced a given sequence or given object 
  • There are three parameters the first one is an starting point which is optional.
  • second parameter for slice function tells about the ending point.
  • Third one will tell the increment between each index.
Syntax:- 
slice(start, stop, step)
  • Create a slice object for slicing

result1 = slice(3)
print(result1)

)
result2 = slice(1, 5, 2)
print(slice(1, 5, 2))
slicing program in python
Example program for slicing


Program for slicing sub-string

py_string = 'Python'

slice_object = slice(3) 
print(py_string[slice_object])  # Pyt

slice_object = slice(1, 6, 2)
print(py_string[slice_object])   # yhn


  • Example program for negative slicing in python

py_str = 'Python'


slice_object = slice(-1, -4, -1)

print(py_str[slice_object])   # noh

In this program we use negative value for slicing the object
  • Example program for sublist and sub-tuple

py_l = ['P', 'y', 't', 'h', 'o', 'n']
py_t = ('P', 'y', 't', 'h', 'o', 'n')


slice_object = slice(3)
print(py_l[slice_obj]) # ['P', 'y', 't']

slice_object = slice(1, 5, 2)
print(py_t[slice_object]) # ('y', 'h')    

,

Program for perror function in c - library function

 perror is a c library function in c this function is used to print a descriptive error message in c.

Program for perror library function in c
Program for perror function

 

Syntax:-

void perror(const char *str)

  • str in c contain a custom message that is printed before the error message itself.
  • perror function in c doesn't return any value

Example Program for perror function in c

#include <stdio.h>

int main () {
   FILE *f;

 
   /* first rename the file */
   rename("file.txt", "updatefile.txt");

   /* now let's open same file */
   f = fopen("file.txt", "r");
   if( fp == NULL ) {
      perror("Error: ");
      return(-1);
   }
   fclose(fp);
      
   return(0);
}

Output

Error: : No such file or directory

Tuesday, October 27, 2020

,

Example program for tmpfile() function in c programming

  •  tmpfile() function in c programming is used to created the temporary file in program and this temporary file will be deleted after  termination of c program.
  • wb+ mode is used to opens file in binary update 
Example program for tmpfile() in c
Example program for tmpfile() function in c programming

 

Syntax :- 

FILE *tmpfile(void) 
  • this function return the file after creating the temporary file.

Program example for tmpfile() function in c


#include <stdio.h>
int main() 
{ 
    char str[] = "Hello function in c"; 
    int i = 0; 
  
    FILE* tmp = tmpfile(); 
    if (tmp == NULL) 
    { 
        puts("Unable to create temp file"); 
        return 0; 
    } 
  
    puts("Temporary file is created\n"); 
    while (str[i] != '\0') 
    { 
        fputc(str[i], tmp); 
        i++; 
    } 
  
    // rewind() function sets the file pointer 
    // at the beginning of the stream. 
    rewind(tmp); 
  
    while (!feof(tmp)) 
        putchar(fgetc(tmp)); 
} 
OUTPUT : -

Temporary file is created
Hello function in c

Monday, October 26, 2020

, ,

Example Program for setbuf Function in c

 setbuf() function in c programming is used to define how stream should be buffered and this function should be call when a file associated with the stream and a already open file but any input or output shouldn't take place.

Example Program for setbuf Function in c
Example Program for setbuf Function in c

 

Parameter for setbuf() function program

steam is an pointer in this program and a allocated buffer is used.

This function doesn't return any thing.

Example program for setbuf() :-

#include <stdio.h>

int main () {
   char buf[BUFSIZ];

   setbuf(stdout, buf);
   puts("This is functioinc.in");

   fflush(stdout);
   return(0);
}

OUTPUT

This is functioinc.in

Sunday, October 25, 2020

,

Example program for ftell function in c

 ftell() function in c is used to get the total size of file after move the file pointer to the end of the file and it return the current position in long type.

File can have more than 32767 bytes of data

Syntax : -

long int ftell(FILE *stream)


Let's Check out the Program for ftell() function in c programming.

 

#include <stdio.h>
#include <conio.h>
void main () {
   FILE *f;
   int len;
   f = fopen("one.txt", "r");
   if(f == NULL) {
      perror(“Error opening file”);
      return(-1);
   }
   fseek(f, 0, SEEK_END);
   len = ftell(f);
   fclose(f);
   printf("Size of file: %d bytes", len);
   getch();
}


OUTPUT For ftell() function program

ftell() function
ftell() in c

Saturday, October 24, 2020

, ,

Example program for fsetpos() function in C programming

 In c handling, fsetpos() function will be used to set the position of input file with the help of fsetpos() access that point.

If you use to fix the file indicator position at any time in the given file then we need to use the fsetpos() function.

 Return Value :- fsetpos() function return zero at the time of success or non zero at failure. 

Example program for fsetpos()
Example program for fsetpos() function in C programming

 

Example program for fsetpos() function

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

int main(){
	
	FILE *f;
	
	char ch[100];
	
	fpos_t pos;

	
	f=fopen("functioninc.txt","w+");
	
	fgetpos(f,&pos);
	printf("Enter any five strings\n");
	for(int i=0;i<4;i++){
		
		scanf("%[^\n]",&ch);
		//write back to the file
		fputs(ch,f);
		
		fputs("\n",f);
		
		fflush(stdin);
	}
	//take the strings from the users
	scanf("%[^\n]",&ch);
	fputs(ch,f);
	
	fsetpos(f,&pos);
	printf("\n...............print the strings..............\n\n");
	while(!feof(f)){
		
		fgets(ch,100,f);
		
		printf("%s",ch);
	}

	//close the file
	fclose(f);

	return 0;
}<4 0="" 100="" a="" after="" and="" array="" back="" buffer="" ch="" character="" clear="" close="" don="" entry.otherwise="" entry="" every="" except="" f="" fclose="" feof="" fflush="" fgets="" file="" first="" for="" fputs="" from="" fsetpos="" i="" if="" in="" indicator="" initial="" is="" last="" line="" n...............print="" n="" new="" of="" or="" pos="" position="" pre="" print="" printf="" return="" s="" scanf="" set="" space="" stdin="" stream="" string="" strings..............="" strings="" t="" take="" takes="" taking="" the="" then="" this="" time="" to="" twice="" users="" waiting="" we="" while="" white="" write="">



,

Example Program for FSEEK() function in c programming

 fseek() function in c programming is used to write data into desired file with the help of pointer which help to find the location into the given file.

Example Program for FSEEK()
Example Program for FSEEK() function in c programming

 

Syntax :-

int fseek(FILE *stream, long int offset, int whence)  

There are three constants are used in fseeks() function in c  SEEK_SET, SEEK_CUR and SEEK_END


 #include <stdio.h>
void main(){  
   FILE *fp;  
  
   fp = fopen("myfile.txt","w+");  
   fputs("This is functioinc", fp);  
    
   fseek( fp, 7, SEEK_SET );  
   fputs("Adam stiffman", fp);  
   fclose(fp);  
} 

Now let see the Example Program for FSEEK() function in c

#include <stdio.h>
int main ()
{
   FILE *f;
   char data[60];
   fp = fopen ("test.c","w");
   fputs("functioinc.in is a programming tutorial website", f);
   fgets ( data, 60, fp );
   printf(Before fseek - %s", data); 
 
   // To set file pointet to 21th byte/character into the file
   fseek(f, 21, SEEK_SET);
   fflush(data);
   fgets ( data, 60, fp );
   printf("After SEEK_SET to 21 - %s", data);
 
   // To find backward 10 bytes 
   fseek(f, -10, SEEK_CUR);
   fflush(data);
   fgets ( data, 60, fp );
   printf("After SEEK_CUR to -10 - %s", data);
 
   // To find 7th byte before the end of file
   fseek(f, -7, SEEK_END); 
   fflush(data);
   fgets ( data, 60, f );
   printf("After SEEK_END to -7 - %s", data);
 
   // now set file pointer to the start of the file
   fseek(f, 0, SEEK_SET); 
 
   fclose(f);
   return 0;
}