C file input/output: Difference between revisions

Content deleted Content added
LordDimwit (talk | contribs)
No edit summary
Tags: Reverted Mobile edit Mobile web edit
Line 231:
The [[POSIX]] standard defines several extensions to {{mono|stdio}} in its Base Definitions, among which are a {{mono|readline}} function that allocates memory, the {{mono|fileno}} and {{mono|fdopen}} functions that establish the link between {{mono|FILE}} objects and [[file descriptor]]s, and a group of functions for creating {{mono|FILE}} objects that refer to in-memory buffers.<ref>{{man|bd|stdio.h|SUS}}</ref>
 
Write a c program to display the following 12 series by using for loops:
==Example==
i)1 2 3 4 }5
The following C program opens a binary file called ''myfile'', reads five bytes from it, and then closes the file.
ii)10 11 12 13 ....20
 
iii)21 22 23 24 ...30
<syntaxhighlight lang="c">
iv)31 32 33 34....50
v)1 2 3 4.....99 100
vi)2 4 6 8 10
vii)10 12 14 16.....60
viii)1 3 5 7....59
ix)51 53 55.....99
x)2 4 6 8.....96 98
xi) A B C D...Z
xii)a b c d....z
******************************************/
#include <stdio.h>
#include <stdlib.h>
 
int main(void)
{
charint buffer[5]i;
FILE* fp = fopenprintf("myfile",Series "rb1:\n--------\n");
for(i=1;i<=5;i++){
 
if printf(fp == NULL"%d\t",i) {;
perror("Failed to open file \"myfile\"");
return EXIT_FAILURE;
}
printf("\n\nSeries 2:\n--------\n");
 
for (int i = 010; i < 5=20; i++) {
int rc = getcprintf(fp"%d\t",i);
}
if (rc == EOF) {
printf("\n\nSeries 3:\n--------\n");
fputs("An error occurred while reading the file.\n", stderr);
for(i=21;i<=30;i++){
return EXIT_FAILURE;
printf("%d\t",i);
}
}
printf("\n\nSeries 4:\n--------\n");
buffer[i] = rc;
for(i=31;i<=50;i++){
printf("%d\t",i);
}
printf("\n\nSeries 5:\n--------\n");
for(i=1;i<=100;i++){
printf("%d\t",i);
}
printf("\n\nSeries 6:\n--------\n");
for(i=2;i<=10;i=i+2){
printf("%d\t",i);
}
printf("\n\nSeries 7:\n--------\n");
for(i=10;i<=60;i=i+2){
printf("%d\t",i);
}
printf("\n\nSeries 8:\n--------\n");
for(i=1;i<=59;i=i+2){
printf("%d\t",i);
}
printf("\n\nSeries 9:\n--------\n");
for(i=51;i<=99;i=i+2){
printf("%d\t",i);
}
printf("\n\nSeries 10:\n--------\n");
for(i=2;i<=98;i=i+2){
printf("%d\t",i);
}
printf("\n\nSeries 11:\n--------\n");
char j;
for(j='A';j<='Z';j++){
printf("%c\t",j);
}
printf("\n\nSeries 12:\n--------\n");
char k;
for(k='a';k<='z';k++){
printf("%c\t",k);
}
 
fclose(fp)return 0;
 
printf("The bytes read were... %x %x %x %x %x\n", buffer[0], buffer[1],
buffer[2], buffer[3], buffer[4]);
 
return EXIT_SUCCESS;
}
</syntaxhighlight>
 
==Alternatives to stdio{{anchor|Sfio}}==