Loading lang_c_08_io...
#include <stdio.h>
void test_output(const char *s) { printf("\n~~~~ %s() ~~~~\n", __func__); int i=1234; double d=12.34; char c='A'; printf("i=%d d=%g c=%c s=%s", i, d, c, s); }
int main(void) { test_output("this is a sentence"); return 0; }
{ ... char line[100]; sprintf(line, "i=%d d=%g c=%c s=%s", i, d, c, s); printf("<%s>\n", line); ... }
{ ... char line[30]; snprintf(line, sizeof(line), "i=%d d=%g c=%c s=%s", i, d, c, s); ... }
<stdio.h>déclare à ce propos deux variables globales : stdout pour la sortie standard et stderr pour la sortie d'erreurs standard.
{ ... fprintf(stdout, "stdout: <%s>\n", line); fprintf(stderr, "stderr: <%s>\n", line); ... }
>sert à signifier que la sortie standard désignée par stdout n'est plus reliée au terminal mais à un fichier pour y écrire.
2>fait de même pour la sortie d'erreurs standard désignée par stderr.
void test_txtWrite(const char *fileName) { printf("\n~~~~ %s(%s) ~~~~\n", __func__, fileName); FILE *f=fopen(fileName, "w"); if(f==NULL) { fprintf(stderr, "cannot open %s\n", fileName); return; } int i=1234; double d=12.34; char c='A'; char s[]="hello"; fprintf(f, "an integer: %d\n", i); fprintf(f, "a real: %g\n", d); fprintf(f, "a char: %c\n", c); fprintf(f, "a string: %s\n", s); fclose(f); }
bool // success Netpbm_saveP3(const char *fileName, int width, int height, uint8_t *contents) { FILE *f=fopen(fileName, "w"); if(f==NULL) { return false; } fprintf(f, "P3\n%d %d\n255\n", width, height); int byteCount=3*width*height; for(int i=0; i<byteCount; ++i) { fprintf(f, "%d\n", contents[i]); } fclose(f); return true; }
void test_saveP3(const char *fileName, int width, int height) { printf("\n~~~~ %s(%s, %d, %d) ~~~~\n", __func__, fileName, width, height); int byteCount=3*width*height; uint8_t *contents=(uint8_t *)malloc(byteCount); if(contents==NULL) { abort(); } for(int y=0; y<height; ++y) { for(int x=0; x<width; ++x) { int red=255*x/(width-1); int green=255*y/(height-1); int blue=255-(red+green)/2; int idx=3*(y*width+x); contents[idx+0]=(uint8_t)red; contents[idx+1]=(uint8_t)green; contents[idx+2]=(uint8_t)blue; } } if(!Netpbm_saveP3(fileName, width, height, contents)) { fprintf(stderr, "cannot save %s\n", fileName); } free(contents); }
<stdio.h>rend accessible, par la déclaration de la variable globale stdin, un troisième flux implicitement ouvert sur l'entrée du terminal.
void test_input(void) { printf("\n~~~~ %s() ~~~~\n", __func__); char fileName[100]=""; int width=0, height=0; printf("file name?\n"); scanf("%s", fileName); printf("image size (width x height)?\n"); scanf("%d x%d", &width, &height); if(fileName[0]&&(width>0)&&(height>0)) { test_saveP3(fileName, width, height); } }
{ ... char line[100]; do { printf("file name?\n"); if(!fgets(line, sizeof(line), stdin)) { return; } } while(sscanf(line, "%s", fileName)!=1); do { printf("image size (width x height)?\n"); if(!fgets(line, sizeof(line), stdin)) { return; } } while(sscanf(line, "%d x%d", &width, &height)!=2); ... }
{ ... const char *input_line_1="X 1234"; char my_char; int my_int; if(sscanf(input_line_1, "%c %d", &my_char, &my_int)==2) { printf("a char <%c> and an integer <%d>\n", my_char, my_int); } else // 2 successful extractions were expected { fprintf(stderr, "cannot extract a char and an integer\n"); } const char *input_line_2="12.34 56.78e300 hello"; float my_float; double my_double; char my_word[100]; if(sscanf(input_line_2, "%f %lf %s", &my_float, &my_double, my_word)==3) { printf("a float <%g>, a double <%g> and a word <%s>\n", my_float, my_double, my_word); } else // 3 successful extractions were expected { fprintf(stderr, "cannot extract a float, a double and a word\n"); } ... }
uint8_t * // loaded contents or NULL Netpbm_loadP3(const char *fileName, int *out_width, int *out_height) { FILE *f=fopen(fileName, "r"); if(f==NULL) { return NULL; } int width, height, range; if((fscanf(f, "P3 %d %d %d", &width, &height, &range)!=3)|| (width<=0)||(height<=0)||(range!=255)) { fclose(f); return NULL; } int byteCount=3*width*height; uint8_t *contents=(uint8_t *)malloc(byteCount); if(contents==NULL) { abort(); } for(int i=0; i<byteCount; ++i) { int value; if(fscanf(f, "%d", &value)!=1) { free(contents); fclose(f); return NULL; } contents[i]=(uint8_t)value; } fclose(f); //-- store out-parameters -- *out_width=width; *out_height=height; return contents; }
void test_loadP3(const char *inputFileName, const char *outputFileName) { printf("\n~~~~ %s(%s, %s) ~~~~\n", __func__, inputFileName, outputFileName); int width, height; uint8_t *contents=Netpbm_loadP3(inputFileName, &width, &height); if(contents==NULL) { fprintf(stderr, "cannot load %s\n", inputFileName); return; } printf("width=%d height=%d\n", width, height); int byteCount=3*width*height; for(int i=0; i<byteCount; i+=3) { uint8_t red=contents[i+0], green=contents[i+1], blue=contents[i+2]; contents[i+0]=blue; contents[i+1]=red; contents[i+2]=green; } if(!Netpbm_saveP3(outputFileName, width, height, contents)) { fprintf(stderr, "cannot save %s\n", outputFileName); } free(contents); }
void test_binWrite(const char *fileName) { printf("\n~~~~ %s(%s) ~~~~\n", __func__, fileName); int iArray[5]={10, 20, 30, 40, 50}; double dArray[4]={10.01, 20.02, 30.03, 40.04}; for(int i=0; i<5; ++i) { printf("%d ", iArray[i]); } printf("\n"); for(int i=0; i<4; ++i) { printf("%g ", dArray[i]); } printf("\n"); FILE *f=fopen(fileName, "wb"); if(f==NULL) { fprintf(stderr, "cannot open %s\n", fileName); return; } if(fwrite(iArray, sizeof(int), 5, f)!=5) { fprintf(stderr, "cannot write to %s\n", fileName); fclose(f); return; } if(fwrite(dArray, sizeof(double), 4, f)!=4) { fprintf(stderr, "cannot write to %s\n", fileName); fclose(f); return; } fclose(f); }
void test_binRead(const char *fileName) { printf("\n~~~~ %s(%s) ~~~~\n", __func__, fileName); int iArray[5]; double dArray[4]; FILE *f=fopen(fileName, "rb"); if(f==NULL) { fprintf(stderr, "cannot open %s\n", fileName); return; } if(fread(iArray, sizeof(int), 5, f)!=5) { fprintf(stderr, "cannot read from %s\n", fileName); fclose(f); return; } if(fread(dArray, sizeof(double), 4, f)!=4) { fprintf(stderr, "cannot read from %s\n", fileName); fclose(f); return; } fclose(f); for(int i=0; i<5; ++i) { printf("%d ", iArray[i]); } printf("\n"); for(int i=0; i<4; ++i) { printf("%g ", dArray[i]); } printf("\n"); }
bool // success Netpbm_saveP6(const char *fileName, int width, int height, uint8_t *contents) { FILE *f=fopen(fileName, "wb"); if(f==NULL) { return false; } fprintf(f, "P6\n%d %d\n255\n", width, height); int byteCount=3*width*height; fwrite(contents, byteCount, 1, f); fclose(f); return true; }
void test_saveP6(const char *inputFileName, const char *outputFileName) { printf("\n~~~~ %s(%s, %s) ~~~~\n", __func__, inputFileName, outputFileName); int width, height; uint8_t *contents=Netpbm_loadP3(inputFileName, &width, &height); if(contents==NULL) { fprintf(stderr, "cannot load %s\n", inputFileName); return; } printf("width=%d height=%d\n", width, height); int byteCount=3*width*height; for(int i=0; i<byteCount; i+=3) { uint8_t red=contents[i+0], green=contents[i+1], blue=contents[i+2]; contents[i+0]=green; contents[i+1]=blue; contents[i+2]=red; } if(!Netpbm_saveP6(outputFileName, width, height, contents)) { fprintf(stderr, "cannot save %s\n", outputFileName); } free(contents); }
uint8_t * // loaded contents or NULL Netpbm_loadP6(const char *fileName, int *out_width, int *out_height) { FILE *f=fopen(fileName, "rb"); if(f==NULL) { return NULL; } int width, height, range; if((fscanf(f, "P6 %d %d %d\n", &width, &height, &range)!=3)|| (width<=0)||(height<=0)||(range!=255)) { fclose(f); return NULL; } int byteCount=3*width*height; uint8_t *contents=(uint8_t *)malloc(byteCount); if(contents==NULL) { abort(); } if(fread(contents, byteCount, 1, f)!=1) { free(contents); fclose(f); return NULL; } fclose(f); //-- store out-parameters -- *out_width=width; *out_height=height; return contents; }
void test_loadP6(const char *inputFileName, const char *outputFileName) { printf("\n~~~~ %s(%s, %s) ~~~~\n", __func__, inputFileName, outputFileName); int width, height; uint8_t *contents=Netpbm_loadP6(inputFileName, &width, &height); if(contents==NULL) { fprintf(stderr, "cannot load %s\n", inputFileName); return; } printf("width=%d height=%d\n", width, height); int byteCount=3*width*height; for(int i=0; i<byteCount; i+=3) { uint8_t red=contents[i+0], green=contents[i+1], blue=contents[i+2]; contents[i+0]=blue; contents[i+1]=red; contents[i+2]=green; } if(!Netpbm_saveP6(outputFileName, width, height, contents)) { fprintf(stderr, "cannot save %s\n", outputFileName); } free(contents); }
//----------------------------------------------------------------------------
#ifndef NETPBM_H #define NETPBM_H
#include <stdbool.h> #include <stdint.h>
bool // success Netpbm_saveP3(const char *fileName, int width, int height, uint8_t *contents);
uint8_t * // loaded contents or NULL Netpbm_loadP3(const char *fileName, int *out_width, int *out_height);
bool // success Netpbm_saveP6(const char *fileName, int width, int height, uint8_t *contents);
uint8_t * // loaded contents or NULL Netpbm_loadP6(const char *fileName, int *out_width, int *out_height);
#endif // NETPBM_H
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include "netpbm.h" #include <stdio.h> #include <stdlib.h>
bool // success Netpbm_saveP3(const char *fileName, int width, int height, uint8_t *contents) { FILE *f=fopen(fileName, "w"); if(f==NULL) { return false; } fprintf(f, "P3\n%d %d\n255\n", width, height); int byteCount=3*width*height; for(int i=0; i<byteCount; ++i) { fprintf(f, "%d\n", contents[i]); } fclose(f); return true; }
uint8_t * // loaded contents or NULL Netpbm_loadP3(const char *fileName, int *out_width, int *out_height) { FILE *f=fopen(fileName, "r"); if(f==NULL) { return NULL; } int width, height, range; if((fscanf(f, "P3 %d %d %d", &width, &height, &range)!=3)|| (width<=0)||(height<=0)||(range!=255)) { fclose(f); return NULL; } int byteCount=3*width*height; uint8_t *contents=(uint8_t *)malloc(byteCount); if(contents==NULL) { abort(); } for(int i=0; i<byteCount; ++i) { #if 1 // first version int value; if(fscanf(f, "%d", &value)!=1) { free(contents); fclose(f); return NULL; } contents[i]=(uint8_t)value; #else // second version if(fscanf(f, "%hhu", contents+i)!=1) { free(contents); fclose(f); return NULL; } #endif } fclose(f); //-- store out-parameters -- *out_width=width; *out_height=height; return contents; }
bool // success Netpbm_saveP6(const char *fileName, int width, int height, uint8_t *contents) { FILE *f=fopen(fileName, "wb"); if(f==NULL) { return false; } fprintf(f, "P6\n%d %d\n255\n", width, height); int byteCount=3*width*height; fwrite(contents, byteCount, 1, f); fclose(f); return true; }
uint8_t * // loaded contents or NULL Netpbm_loadP6(const char *fileName, int *out_width, int *out_height) { FILE *f=fopen(fileName, "rb"); if(f==NULL) { return NULL; } int width, height, range; if((fscanf(f, "P6 %d %d %d\n", &width, &height, &range)!=3)|| (width<=0)||(height<=0)||(range!=255)) { fclose(f); return NULL; } int byteCount=3*width*height; uint8_t *contents=(uint8_t *)malloc(byteCount); if(contents==NULL) { abort(); } if(fread(contents, byteCount, 1, f)!=1) { free(contents); fclose(f); return NULL; } fclose(f); //-- store out-parameters -- *out_width=width; *out_height=height; return contents; }
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "netpbm.h"
void test_output(const char *s) { printf("\n~~~~ %s() ~~~~\n", __func__); int i=1234; double d=12.34; char c='A'; printf("i=%d d=%g c=%c s=%s\n", i, d, c, s); #if 0 char line[100]; // large enough #else char line[30]; // a bit too small #endif snprintf(line, sizeof(line), "i=%d d=%g c=%c s=%s", i, d, c, s); printf("<%s>\n", line); fprintf(stdout, "stdout: <%s>\n", line); fprintf(stderr, "stderr: <%s>\n", line); // const char *input_line_1="X 1234"; char my_char; int my_int; if(sscanf(input_line_1, "%c %d", &my_char, &my_int)==2) { printf("a char <%c> and an integer <%d>\n", my_char, my_int); } else // 2 successful extractions were expected { fprintf(stderr, "cannot extract a char and an integer\n"); } const char *input_line_2="12.34 56.78e300 hello"; float my_float; double my_double; char my_word[100]; if(sscanf(input_line_2, "%f %lf %s", &my_float, &my_double, my_word)==3) { printf("a float <%g>, a double <%g> and a word <%s>\n", my_float, my_double, my_word); } else // 3 successful extractions were expected { fprintf(stderr, "cannot extract a float, a double and a word\n"); } }
void test_txtWrite(const char *fileName) { printf("\n~~~~ %s(%s) ~~~~\n", __func__, fileName); FILE *f=fopen(fileName, "w"); if(f==NULL) { fprintf(stderr, "cannot open %s\n", fileName); return; } int i=1234; double d=12.34; char c='A'; char s[]="hello"; fprintf(f, "an integer: %d\n", i); fprintf(f, "a real: %g\n", d); fprintf(f, "a char: %c\n", c); fprintf(f, "a string: %s\n", s); fclose(f); }
void test_saveP3(const char *fileName, int width, int height) { printf("\n~~~~ %s(%s, %d, %d) ~~~~\n", __func__, fileName, width, height); int byteCount=3*width*height; uint8_t *contents=(uint8_t *)malloc(byteCount); if(contents==NULL) { abort(); } for(int y=0; y<height; ++y) { for(int x=0; x<width; ++x) { int red=255*x/(width-1); int green=255*y/(height-1); int blue=255-(red+green)/2; int idx=3*(y*width+x); contents[idx+0]=(uint8_t)red; contents[idx+1]=(uint8_t)green; contents[idx+2]=(uint8_t)blue; } } if(!Netpbm_saveP3(fileName, width, height, contents)) { fprintf(stderr, "cannot save %s\n", fileName); } free(contents); }
void test_input(void) { printf("\n~~~~ %s() ~~~~\n", __func__); char fileName[100]=""; int width=0, height=0; #if 0 // first version printf("file name?\n"); scanf("%s", fileName); printf("image size (width x height)?\n"); scanf("%d x%d", &width, &height); #else // second version char line[100]; do { printf("file name?\n"); if(!fgets(line, sizeof(line), stdin)) { return; } } while(sscanf(line, "%s", fileName)!=1); do { printf("image size (width x height)?\n"); if(!fgets(line, sizeof(line), stdin)) { return; } } while(sscanf(line, "%d x%d", &width, &height)!=2); #endif if(fileName[0]&&(width>0)&&(height>0)) { test_saveP3(fileName, width, height); } }
void test_loadP3(const char *inputFileName, const char *outputFileName) { printf("\n~~~~ %s(%s, %s) ~~~~\n", __func__, inputFileName, outputFileName); int width, height; uint8_t *contents=Netpbm_loadP3(inputFileName, &width, &height); if(contents==NULL) { fprintf(stderr, "cannot load %s\n", inputFileName); return; } printf("width=%d height=%d\n", width, height); int byteCount=3*width*height; for(int i=0; i<byteCount; i+=3) { uint8_t red=contents[i+0], green=contents[i+1], blue=contents[i+2]; contents[i+0]=blue; contents[i+1]=red; contents[i+2]=green; } if(!Netpbm_saveP3(outputFileName, width, height, contents)) { fprintf(stderr, "cannot save %s\n", outputFileName); } free(contents); }
void test_binWrite(const char *fileName) { printf("\n~~~~ %s(%s) ~~~~\n", __func__, fileName); int iArray[5]={10, 20, 30, 40, 50}; double dArray[4]={10.01, 20.02, 30.03, 40.04}; for(int i=0; i<5; ++i) { printf("%d ", iArray[i]); } printf("\n"); for(int i=0; i<4; ++i) { printf("%g ", dArray[i]); } printf("\n"); FILE *f=fopen(fileName, "wb"); if(f==NULL) { fprintf(stderr, "cannot open %s\n", fileName); return; } if(fwrite(iArray, sizeof(int), 5, f)!=5) { fprintf(stderr, "cannot write to %s\n", fileName); fclose(f); return; } if(fwrite(dArray, sizeof(double), 4, f)!=4) { fprintf(stderr, "cannot write to %s\n", fileName); fclose(f); return; } fclose(f); }
void test_binRead(const char *fileName) { printf("\n~~~~ %s(%s) ~~~~\n", __func__, fileName); int iArray[5]; double dArray[4]; FILE *f=fopen(fileName, "rb"); if(f==NULL) { fprintf(stderr, "cannot open %s\n", fileName); return; } if(fread(iArray, sizeof(int), 5, f)!=5) { fprintf(stderr, "cannot read from %s\n", fileName); fclose(f); return; } if(fread(dArray, sizeof(double), 4, f)!=4) { fprintf(stderr, "cannot read from %s\n", fileName); fclose(f); return; } fclose(f); for(int i=0; i<5; ++i) { printf("%d ", iArray[i]); } printf("\n"); for(int i=0; i<4; ++i) { printf("%g ", dArray[i]); } printf("\n"); }
void test_saveP6(const char *inputFileName, const char *outputFileName) { printf("\n~~~~ %s(%s, %s) ~~~~\n", __func__, inputFileName, outputFileName); int width, height; uint8_t *contents=Netpbm_loadP3(inputFileName, &width, &height); if(contents==NULL) { fprintf(stderr, "cannot load %s\n", inputFileName); return; } printf("width=%d height=%d\n", width, height); int byteCount=3*width*height; for(int i=0; i<byteCount; i+=3) { uint8_t red=contents[i+0], green=contents[i+1], blue=contents[i+2]; contents[i+0]=green; contents[i+1]=blue; contents[i+2]=red; } if(!Netpbm_saveP6(outputFileName, width, height, contents)) { fprintf(stderr, "cannot save %s\n", outputFileName); } free(contents); }
void test_loadP6(const char *inputFileName, const char *outputFileName) { printf("\n~~~~ %s(%s, %s) ~~~~\n", __func__, inputFileName, outputFileName); int width, height; uint8_t *contents=Netpbm_loadP6(inputFileName, &width, &height); if(contents==NULL) { fprintf(stderr, "cannot load %s\n", inputFileName); return; } printf("width=%d height=%d\n", width, height); int byteCount=3*width*height; for(int i=0; i<byteCount; i+=3) { uint8_t red=contents[i+0], green=contents[i+1], blue=contents[i+2]; contents[i+0]=blue; contents[i+1]=red; contents[i+2]=green; } if(!Netpbm_saveP6(outputFileName, width, height, contents)) { fprintf(stderr, "cannot save %s\n", outputFileName); } free(contents); }
int main(int argc, char **argv) { if(argc<2) { fprintf(stderr, "usage: %s test_numbers...\n", argv[0]); return 1; } for(int i=1; i<argc; ++i) { int minTest=1, maxTest=10; if(strcmp(argv[i], "all")!=0) { if(sscanf(argv[i], "%d", &minTest)==1) { maxTest=minTest; } else { fprintf(stderr, "ignoring %s\n", argv[i]); continue; } } for(int test=minTest; test<=maxTest; ++test) { printf("\n#### test %d ####", test); switch(test) { case 1: { test_output("this is a sentence"); break; } case 2: { test_txtWrite("output_text.txt"); break; } case 3: { test_txtWrite("/output_forbidden.txt"); break; } case 4: { test_saveP3("output_A.ppm", 320, 200); break; } case 5: { test_input(); break; } case 6: { test_loadP3("output_A.ppm", "output_C.ppm"); break; } case 7: { test_binWrite("output_data.bin"); break; } case 8: { test_binRead("output_data.bin"); break; } case 9: { test_saveP6("output_A.ppm", "output_D.ppm"); break; } case 10: { test_loadP6("output_D.ppm", "output_E.ppm"); break; } default: { fprintf(stderr, "unexpected test number %d\n", test); break; } } } } return 0; }
//----------------------------------------------------------------------------